举例示意
讲道理不如举例子,下边结合代码快速说明如何求极值
>>> 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
即可。