72

I have one list of 100 numbers as height for Y axis, and as length for X axis: 1 to 100 with a constant step of 5. I need to calculate the Area that it is included by the curve of the (x,y) points, and the X axis, using rectangles and Scipy. Do I have to find the function of this curve? or not? ... almost all the examples I have read are about a specific equation for the Y axis. In my case there is no equation, just data from a list. The classic solution is to add or the Y points and multiple by the step X distance... using Scipy any idea?

Please, can anyone recommend any book which focusing on numerical (finite elementary) methods, using Scipy and Numpy? ...

4

3 回答 3

81

numpy 和 scipy 库包括复合梯形 ( numpy.trapz ) 和辛普森 ( scipy.integrate.simps ) 规则。

这是一个简单的例子。在trapzsimps中,参数dx=5表示数据沿 x 轴的间距为 5 个单位。

import numpy as np
from scipy.integrate import simps
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 = simps(y, dx=5)
print("area =", area)

输出:

area = 452.5
area = 460.0
于 2012-11-10T16:38:16.787 回答
23

您可以使用辛普森规则梯形规则来计算图形下的面积,给定一个定期间隔的 y 值表。

计算辛普森规则的 Python 脚本:

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是 y 值之间的偏移量(或间隙),并且y_vals是 y 值的数组。

示例(在与上述函数相同的文件中):

y_values = [13, 45.3, 12, 1, 476, 0]
interval = 1.2
area = integrate(y_values, interval)
print("The area is", area)
于 2012-11-10T07:53:12.183 回答
21

如果您安装了 sklearn,一个简单的替代方法是使用 sklearn.metrics.auc

这使用给定任意 x 和 y 数组的梯形规则计算曲线下的面积

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)))

都输出相同的区域:4607.5

sklearn.metrics.auc 的优点是它可以接受任意间隔的 'x' 数组,只要确保它是升序的,否则结果会不正确

于 2018-08-13T19:39:54.307 回答