一千萬個為什麽

搜索

查找5個局部最大值和最小值點以及python中的極值點

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

轉載註明原文: 查找5個局部最大值和最小值點以及python中的極值點