计算数据集范围内积分的最有效方法
我有一个 10 行 x 20 列的数组。每列对应一个数据集,该数据集无法用任何类型的连续数学函数拟合(它是通过实验得出的一系列数字)。我想计算第4行和第8行之间每一列的积分,然后将获得的结果存储在一个新数组(20行x 1列)中。
我尝试过使用不同的 scipy.integrate 模块(例如quad,trpz,...)。
问题是,据我了解, scipy.integrate 必须应用于函数,并且我不确定如何将初始数组的每一列转换为函数。作为替代方案,我考虑计算第 4 行和第 8 行之间每列的平均值,然后将该数字乘以 4(即 8-4=4,x 间隔),然后将其存储到我的最终 20x1 数组中。问题是……嗯……我不知道如何计算给定范围内的平均值。我要问的问题是:
- 哪种方法更有效/更直接?
- 可以在像我所描述的那样的数据集上计算积分吗?
- 如何计算一系列行的平均值?
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:
- Which method is more efficient/straightforward?
- Can integrals be calculated over a data set like the one that I have described?
- How do I calculate the average over a range of rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您只知道数据点,因此最好的选择是使用
trapz
(积分的梯形近似,基于您知道的数据点)。您很可能不想将数据集转换为函数,而使用
trapz
则不需要这样做。所以如果我理解正确的话,你想做这样的事情:
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:
要获取每列中条目 4 到 8(包括两端)的总和,请使用
(第一行只是创建所需形状的示例数组。)
To get the sum of the entries 4 to 8 (including both ends) in each column, use
(The first line is just to create an example array of the desired shape.)