我有一个100个数字的列表,作为 Y 轴的高度,作为 X 轴的长度: 1到100,步长为5。我需要使用矩形和 Scipy 计算(x,y)点和 X 轴的曲线所包含的面积。我需要找到这条曲线的函数吗?还是不要?几乎我读过的所有例子都是关于 Y 轴的一个特定方程式。在我的例子中,没有等式,只有列表中的数据。经典的解决方案是加上或 Y 点和乘以步骤 X 的距离... 使用 Scipy 有什么想法吗?
You can use Simpsons rule or the Trapezium rule to calculate the area under a graph given a table of y-values at a regular interval.
Python script that calculates Simpsons rule:
def integrate(y_vals, h):
i = 1
total = y_vals[0] + y_vals[-1]
for y in y_vals[1:-1]:
if i % 2 == 0:
total += 2 * y
else:
total += 4 * y
i += 1
return total * (h / 3.0)
h is the offset (or gap) between y values, and y_vals is an array of well, y values.
Example (In same file as above function):
y_values = [13, 45.3, 12, 1, 476, 0]
interval = 1.2
area = integrate(y_values, interval)
print("The area is", area)
Here's a simple example. In both trapz and simpson, the argument dx=5 indicates that the spacing of the data along the x axis is 5 units.
import numpy as np
from scipy.integrate import simpson
from numpy import trapz
# The y values. A numpy array is used here,
# but a python list could also be used.
y = np.array([5, 20, 4, 18, 19, 18, 7, 4])
# Compute the area using the composite trapezoidal rule.
area = trapz(y, dx=5)
print("area =", area)
# Compute the area using the composite Simpson's rule.
area = simpson(y, dx=5)
print("area =", area)
If you have sklearn installed, a simple alternative is to use sklearn.metrics.auc
This computes the area under the curve using the trapezoidal rule given arbitrary x, and y array
import numpy as np
from sklearn.metrics import auc
dx = 5
xx = np.arange(1,100,dx)
yy = np.arange(1,100,dx)
print('computed AUC using sklearn.metrics.auc: {}'.format(auc(xx,yy)))
print('computed AUC using np.trapz: {}'.format(np.trapz(yy, dx = dx)))
both output the same area: 4607.5
the advantage of sklearn.metrics.auc is that it can accept arbitrarily-spaced 'x' array, just make sure it is ascending otherwise the results will be incorrect