scipy使用python寻找时间序列的极大值(局部最大值)或极小值(局部最小值),极值点

举例示意

讲道理不如举例子,下边结合代码快速说明如何求极值

>>> from scipy.signal import argrelextrema
>>> x = np.array([2, 1, 2, 3, 2, 0, 1, 0])
>>> argrelextrema(x, np.greater)
(array([3, 6]),)

图片说明

上边的代码是什么意思呢?下边绘图说明一下。运行如下代码,得到下边的图片:

import matplotlib.pyplot as plt
plt.plot(x)
plt.scatter(
    argrelextrema(x, np.greater),
    x[argrelextrema(x, np.greater)],
    c='red'
)

在这里插入图片描述
图中所示的红点即为极大值。如果要求极小值,只需要把np.greater修改为np.less即可。

See also

peak finding in SciPy

猜你喜欢

转载自blog.csdn.net/shiyuzuxiaqianli/article/details/115035819