使用 scipy.signal 的 argrelextrema 函數( API ),簡單方便
import numpy as np import pylab as pl import matplotlib.pyplot as plt import scipy.signal as signal x=np.array([ 0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3, 1, 20, 7, 3, 0 ]) plt.figure(figsize=(16,4)) plt.plot(np.arange(len(x)),x) print x[signal.argrelextrema(x, np.greater)] print signal.argrelextrema(x, np.greater) plt.plot(signal.argrelextrema(x,np.greater)[0],x[signal.argrelextrema(x, np.greater)],'o') plt.plot(signal.argrelextrema(-x,np.greater)[0],x[signal.argrelextrema(-x, np.greater)],'+') # plt.plot(peakutils.index(-x),x[peakutils.index(-x)],'*') plt.show()
[25 15 6 10 13 10 20] (array([ 2, 6, 9, 15, 17, 19, 22]),)
但是存在一個問題,在極值有左右相同點的時候無法識別,但是個人認為在實際的使用過程中極少會出現這種情況,所以可以忽略。
x=np.array([ 0, 15, 15, 15, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3, 1, 20, 7, 3, 0 ]) plt.figure(figsize=(16,4)) plt.plot(np.arange(len(x)),x) print x[signal.argrelextrema(x, np.greater)] print signal.argrelextrema(x, np.greater) plt.plot(signal.argrelextrema(x,np.greater)[0],x[signal.argrelextrema(x, np.greater)],'o') plt.plot(signal.argrelextrema(x,np.less)[0],x[signal.argrelextrema(x, np.less)],'+') plt.show()
[15 6 10 13 10 20] (array([ 6, 9, 15, 17, 19, 22]),)
以上這篇python 尋找離散序列極值點的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
