在数据集范围内计算积分的最有效方法

1 投票
2 回答
5301 浏览
提问于 2025-04-16 09:47

我有一个10行20列的数组。每一列对应一个数据集,这些数据集不能用任何连续的数学函数来拟合(这些数据是通过实验得来的数字)。我想计算每一列在第4行到第8行之间的积分,然后把得到的结果存储到一个新的数组里,这个新数组的大小是20行1列。

我尝试过使用不同的scipy.integrate模块(比如quad、trpz等)。

问题是,按照我的理解,scipy.integrate必须应用于函数,而我不太确定如何把我最初数组的每一列转换成一个函数。作为替代方案,我想到了计算第4行到第8行之间每一列的平均值,然后把这个数字乘以4(也就是8减去4等于4,代表x的区间),然后把这个结果存入我的最终20x1数组。问题是……嗯……我不知道如何在给定范围内计算平均值。我想问的问题是:

  1. 哪种方法更有效/更简单?
  2. 能否对我描述的数据集计算积分?
  3. 我如何在一段行范围内计算平均值?

2 个回答

2

要计算每一列中第4到第8个数的总和(包括第4和第8个数),可以使用下面的代码:

a = numpy.arange(200).reshape(10, 20)
a[4:9].sum(axis=0)

(第一行代码只是为了创建一个符合要求的示例数组。)

5

因为你只知道一些数据点,所以最好的选择是使用 trapz(这是一种基于你已知数据点的梯形积分近似方法)。

你很可能不想把你的数据集转换成函数,而使用 trapz 的话,你就不需要这样做。

所以如果我理解得没错,你想做的事情大概是这样的:

from numpy import *

# x-coordinates for data points
x = array([0, 0.4, 1.6, 1.9, 2, 4, 5, 9, 10])

# some random data: 3 whatever data sets (sharing the same x-coordinates)
y = zeros([len(x), 3])
y[:,0] = 123
y[:,1] = 1 + x
y[:,2] = cos(x/5.)
print y

# compute approximations for integral(dataset, x=0..10) for datasets i=0,1,2
yi = trapz(y, x[:,newaxis], axis=0)
# what happens here: x must be an array of the same shape as y
# newaxis tells numpy to add a new "virtual" axis to x, in effect saying that the
# x-coordinates are the same for each data set

# approximations of the integrals based the datasets
# (here we also know the exact values, so print them too)
print yi[0], 123*10
print yi[1], 10 + 10*10/2.
print yi[2], sin(10./5.)*5.

撰写回答