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

发布于 2024-10-11 15:43:31 字数 443 浏览 9 评论 0原文

我有一个 10 行 x 20 列的数组。每列对应一个数据集,该数据集无法用任何类型的连续数学函数拟合(它是通过实验得出的一系列数字)。我想计算第4行和第8行之间每一列的积分,然后将获得的结果存储在一个新数组(20行x 1列)中。

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

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

  1. 哪种方法更有效/更直接?
  2. 可以在像我所描述的那样的数据集上计算积分吗?
  3. 如何计算一系列行的平均值?

I have an array of 10 rows by 20 columns. Each columns corresponds to a data set that cannot be fitted with any sort of continuous mathematical function (it's a series of numbers derived experimentally). I would like to calculate the integral of each column between row 4 and row 8, then store the obtained result in a new array (20 rows x 1 column).

I have tried using different scipy.integrate modules (e.g. quad, trpz,...).

The problem is that, from what I understand, scipy.integrate must be applied to functions, and I am not sure how to convert each column of my initial array into a function. As an alternative, I thought of calculating the average of each column between row 4 and row 8, then multiply this number by 4 (i.e. 8-4=4, the x-interval) and then store this into my final 20x1 array. The problem is...ehm...that I don't know how to calculate the average over a given range. The question I am asking are:

  1. Which method is more efficient/straightforward?
  2. Can integrals be calculated over a data set like the one that I have described?
  3. How do I calculate the average over a range of rows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔少女心 2024-10-18 15:43:31

由于您只知道数据点,因此最好的选择是使用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.

Since you know only the data points, the best choice is to use trapz (the trapezoidal approximation to the integral, based on the data points you know).

You most likely don't want to convert your data sets to functions, and with trapz you don't need to.

So if I understand correctly, you want to do something like this:

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.
司马昭之心 2024-10-18 15:43:31

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

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

(第一行只是创建所需形状的示例数组。)

To get the sum of the entries 4 to 8 (including both ends) in each column, use

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

(The first line is just to create an example array of the desired shape.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文