在python中找到5个局部最大值和最小值加上极值



我在Stack Overflow找到了一些代码。然而,它不适用于浮点数。我试着找到5个局部最大值和最小值加上极值。如有任何帮助,不胜感激。

from scipy.stats import gaussian_kde
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
y=[ 191.78 ,   191.59,    191.59,    191.41,    191.47,    191.33,    191.25  
  ,191.33 ,   191.48 ,   191.48,    191.51,    191.43,    191.42,    191.54    
  ,191.5975,  191.555,   191.52 ,   191.25 ,   191.15  ,  191.01  ]
x = np.linspace(1 ,20,len(y))

fig, ax = plt.subplots(figsize=(10, 10))
ax.legend(loc='center left', bbox_to_anchor=(1.05, 0.5), frameon=False)
ax.scatter(x, y, color='black', label='data')
ax.plot(x,y,color='red')

sortId=np.argsort(x)
x=x[sortId]
y=y[sortId]
#this way the x-axis corresponds to the index of x
plt.plot(x-1,y)
plt.show()
maxm = argrelextrema(y, np.greater) 
minm = argrelextrema(y, np.less)

我做了更多的研究,答案是使用适当的数组和numpy数组,如下所示:

 def check(self, data):
        dataarr = np.asarray(data)
        extmax = argrelextrema(dataarr, np.greater)
        extmin = argrelextrema(dataarr, np.less)