2011-01-11 96 views
1

我有一个10行乘20列的数组。每列对应于一个数据集,它不能与任何连续的数学函数(这是一系列实验推导出来的数字)进行拟合。我想计算第4行和第8行之间每列的积分,然后将获得的结果存储在一个新数组(20行×1列)中。计算数据集范围中积分的最有效方法

我试过使用不同的scipy.integrate模块(例如quad,trpz,...)。

问题是,据我所知,scipy.integrate必须应用于函数,我不知道如何将我的初始数组的每列转换为函数。作为替代方案,我考虑计算第4行和第8行之间每列的平均值,然后将此数字乘以4(即8-4 = 4,x间隔),然后将其存储到我的最终20x1阵列中。问题是......呃......我不知道如何计算给定范围内的平均值。我问的问题是:

  1. 哪种方法更高效/更简单?
  2. 可以根据我所描述的数据集计算积分吗?
  3. 如何计算一系列行的平均值?
+0

我可能失去了一些东西,而是一个整体的仅仅是区域中的“曲线”下,这样你就可以只添加了值在每一列中。 – 2011-01-11 19:53:38

回答

2

要获取的条目的总和为4〜8(包括两端)中的每一列,使用

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

(第一行是只是以创建所需的形状的示例性阵列。)

3

既然你只知道数据点,最好的选择是使用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. 
相关问题