重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
python中怎么尋找離散序列極值點(diǎn),很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
使用 scipy.signal 的 argrelextrema 函數(shù)(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]),)
但是存在一個問題,在極值有左右相同點(diǎn)的時候無法識別,但是個人認(rèn)為在實(shí)際的使用過程中極少會出現(xiàn)這種情況,所以可以忽略。
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]),)
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。