如何在 Python 中集成两个一维数据数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17602076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I integrate two 1-D data arrays in Python?
提问by user2565770
I have two tabulated data arrays, x and y, and I don't know the function that generated the data. I want to be able to evaluate the integral of the line produced by the data at any point along the x-axis.
我有两个表格数据数组 x 和 y,我不知道生成数据的函数。我希望能够评估数据在 x 轴上的任何点产生的线的积分。
Rather than interpolating a piecewise function to the data and then attempting to integrate that, which I am having trouble with, is there something I can use that will simply provide the integral by evaluating the arrays?
与其将分段函数插入到数据中,然后尝试对其进行积分,我遇到了问题,还有什么可以使用的东西,它可以通过评估数组来简单地提供积分?
When searching for solutions, I have seen references to iPython and Pandas, but I haven't been able to find the parts of those packages that will aid in this task.
在搜索解决方案时,我看到了对 iPython 和 Pandas 的引用,但我一直无法找到这些包中有助于完成此任务的部分。
If there isn't a way to simply integrate the arrays, could you provide some advice on the best way to handle this task?
如果没有一种方法可以简单地集成阵列,您能否就处理此任务的最佳方法提供一些建议?
采纳答案by jh314
Scipy has some nice tools to perform numerical integration.
For example, you can use scipy.integrate.simpsto perform simpson's Rule, and you can pass it the following:
例如,您可以使用scipy.integrate.simpssimpson's Rule 来执行,您可以将其传递给以下内容:
scipy.integrate.simps(y, x=None, dx=1, axis=-1, even='avg')
Parameters :
y : array_like Array to be integrated.x : array_like, optional If given, the points at which y is sampled.
dx : int, optional Spacing of integration points along axis of y. Only used when x is None. Default is 1.
axis : int, optional Axis along which to integrate. Default is the last axis.
even : {‘avg', ‘first', ‘str'}, optional
‘avg' : Average two results:1) use the first N-2 intervals with a trapezoidal rule on the last interval and 2) use the last N-2 intervals with a trapezoidal rule on the first interval.
‘first' : Use Simpson's rule for the first N-2 intervals with a trapezoidal rule on the last interval.
‘last' : Use Simpson's rule for the last N-2 intervals with a trapezoidal rule on the first interval.
scipy.integrate.simps(y, x=None, dx=1, axis=-1, even='avg')
参数:
y : array_like 要集成的数组。x : array_like,可选如果给定,y 被采样的点。
dx : int,可选积分点沿 y 轴的间距。仅在 x 为 None 时使用。默认值为 1。
axis : int,可选的要整合的轴。默认是最后一个轴。
偶数:{'avg', 'first', 'str'},可选
'avg' :平均两个结果:1) 在最后一个间隔上使用前 N-2 个间隔和梯形规则 2) 在第一个间隔上使用最后 N-2 个间隔和梯形规则。
'first' :对前 N-2 个区间使用辛普森规则,对最后一个区间使用梯形规则。
'last' :对最后 N-2 个间隔使用辛普森规则,在第一个间隔上使用梯形规则。
So you can use your two arrays to do numerical integration.
因此,您可以使用两个数组进行数值积分。
回答by Stephan
Scipy has an integrationfeature that can help you.
Scipy 具有可以帮助您的集成功能。
If you want to use the cumulative sum of trapezoidsfor integration, which would probably be best for a series of points.
You can do this:
你可以这样做:
>>> from scipy import integrate
>>> x = np.linspace(-2, 2, num=20)
>>> y = x
>>> y_int = integrate.cumtrapz(y, x, initial=0)
>>> plt.plot(x, y_int, 'ro', x, y[0] + 0.5 * x**2, 'b-')
>>> plt.show()
This will also plot the data and show it to you graphically. This is the integration call integrate.cumtrapz(y, x, initial=0)where x, and y are your two arrays.
这还将绘制数据并以图形方式显示给您。这是集成调用integrate.cumtrapz(y, x, initial=0),其中 x 和 y 是您的两个数组。

