python 尋找離散序列極值點(diǎn)的方法
使用 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]),)
以上這篇python 尋找離散序列極值點(diǎn)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python記錄numpy.empty()函數(shù)引發(fā)的問題及解決
這篇文章主要介紹了Python記錄numpy.empty()函數(shù)引發(fā)的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Python調(diào)用各大機(jī)器翻譯API的實(shí)現(xiàn)示例
本文主要介紹了Python調(diào)用各大機(jī)器翻譯API的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

python 多進(jìn)程隊(duì)列數(shù)據(jù)處理詳解

Python_查看sqlite3表結(jié)構(gòu),查詢語句的示例代碼

python3使用libpcap庫進(jìn)行抓包及數(shù)據(jù)處理的操作方法

從入門到精通:Python項(xiàng)目打包與setup.py實(shí)戰(zhàn)指南

pandas中字典和dataFrame的相互轉(zhuǎn)換

flask實(shí)現(xiàn)驗(yàn)證碼并驗(yàn)證功能