python 尋找離散序列極值點的方法
使用 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]),)
但是存在一個問題,在極值有左右相同點的時候無法識別,但是個人認為在實際的使用過程中極少會出現(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 尋找離散序列極值點的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python記錄numpy.empty()函數(shù)引發(fā)的問題及解決
這篇文章主要介紹了Python記錄numpy.empty()函數(shù)引發(fā)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Python調(diào)用各大機器翻譯API的實現(xiàn)示例
本文主要介紹了Python調(diào)用各大機器翻譯API的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Python_查看sqlite3表結(jié)構(gòu),查詢語句的示例代碼
今天小編就為大家分享一篇Python_查看sqlite3表結(jié)構(gòu),查詢語句的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07python3使用libpcap庫進行抓包及數(shù)據(jù)處理的操作方法
這篇文章主要介紹了python3使用libpcap庫進行抓包及數(shù)據(jù)處理,需要的朋友可以參考下2022-10-10從入門到精通:Python項目打包與setup.py實戰(zhàn)指南
想要將你的Python項目分享給世界嗎?本指南將帶你從零開始,一步步學(xué)習(xí)如何打包你的Python項目,并創(chuàng)建一個專業(yè)的setup.py文件,我們將分享實用的技巧和最佳實踐,幫助你的項目在Python社區(qū)中脫穎而出,跟著我們的步伐,讓你的項目打包變得輕松有趣!2024-03-03pandas中字典和dataFrame的相互轉(zhuǎn)換
有時候需要把dic轉(zhuǎn)換為DataFrame格式,便于查看和存儲,下面這篇文章主要給大家介紹了關(guān)于pandas中字典和dataFrame相互轉(zhuǎn)換的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09