{"id":1165267,"date":"2025-01-15T15:18:18","date_gmt":"2025-01-15T07:18:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165267.html"},"modified":"2025-01-15T15:18:21","modified_gmt":"2025-01-15T07:18:21","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e6%9b%b2%e7%ba%bf%e7%9a%84%e5%b3%b0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165267.html","title":{"rendered":"python\u5982\u4f55\u6c42\u66f2\u7ebf\u7684\u5cf0\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205641\/70e6c89d-342b-4500-a2c0-234b07e40e30.webp\" alt=\"python\u5982\u4f55\u6c42\u66f2\u7ebf\u7684\u5cf0\u503c\" \/><\/p>\n<p><p> <strong>Python\u6c42\u66f2\u7ebf\u7684\u5cf0\u503c\u53ef\u4ee5\u4f7f\u7528scipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570\u3001numpy\u5e93\u4e2d\u7684argrelextrema\u51fd\u6570\u3001\u4ee5\u53capandas\u5e93\u4e2d\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>scipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570<\/strong>\u56e0\u5176\u529f\u80fd\u5f3a\u5927\u4e14\u5bb9\u6613\u4f7f\u7528\uff0c\u901a\u5e38\u662f\u5904\u7406\u5cf0\u503c\u68c0\u6d4b\u7684\u9996\u9009\u65b9\u6cd5\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u5bf9\u4e00\u7ef4\u6570\u7ec4\u8fdb\u884c\u5cf0\u503c\u68c0\u6d4b\uff0c\u5e76\u63d0\u4f9b\u591a\u79cd\u53c2\u6570\u6765\u8c03\u6574\u5cf0\u503c\u68c0\u6d4b\u7684\u7075\u654f\u5ea6\u548c\u7cbe\u786e\u5ea6\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, properties = find_peaks(y, height=0)<\/p>\n<h2><strong>\u6253\u5370\u5cf0\u503c\u4f4d\u7f6e\u548c\u5bf9\u5e94\u7684\u9ad8\u5ea6<\/strong><\/h2>\n<p>print(&quot;Peak positions:&quot;, peaks)<\/p>\n<p>print(&quot;Peak heights:&quot;, properties[&#39;peak_heights&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u51fay\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5cf0\u503c\u4f4d\u7f6e\u53ca\u5176\u5bf9\u5e94\u7684\u9ad8\u5ea6\u3002<strong>find_peaks\u51fd\u6570\u5177\u6709\u9ad8\u5ea6\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u6765\u63a7\u5236\u5cf0\u503c\u68c0\u6d4b\u7684\u7075\u654f\u5ea6\uff0c\u6bd4\u5982\u8bbe\u7f6e\u6700\u5c0f\u5cf0\u503c\u9ad8\u5ea6\u3001\u5cf0\u5bbd\u3001\u5cf0\u95f4\u8ddd\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u5cf0\u503c\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001scipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>find_peaks\u51fd\u6570\u662fscipy.signal\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u6d4b\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u5cf0\u503c\u3002\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, properties = find_peaks(y, height=0)<\/p>\n<h2><strong>\u6253\u5370\u5cf0\u503c\u4f4d\u7f6e\u548c\u5bf9\u5e94\u7684\u9ad8\u5ea6<\/strong><\/h2>\n<p>print(&quot;Peak positions:&quot;, peaks)<\/p>\n<p>print(&quot;Peak heights:&quot;, properties[&#39;peak_heights&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cfind_peaks\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u503c\uff1apeaks\u548cproperties\u3002peaks\u662f\u68c0\u6d4b\u51fa\u7684\u5cf0\u503c\u4f4d\u7f6e\u7d22\u5f15\uff0cproperties\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4e86\u5cf0\u503c\u7684\u5404\u79cd\u5c5e\u6027\uff0c\u6bd4\u5982\u9ad8\u5ea6\u3001\u5bbd\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u53c2\u6570\u8c03\u6574<\/h4>\n<\/p>\n<p><p>find_peaks\u51fd\u6570\u63d0\u4f9b\u4e86\u591a\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8c03\u6574\u5cf0\u503c\u68c0\u6d4b\u7684\u7075\u654f\u5ea6\u548c\u7cbe\u786e\u5ea6\u3002\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>height<\/strong>: \u8bbe\u7f6e\u6700\u5c0f\u5cf0\u503c\u9ad8\u5ea6\u3002<\/li>\n<li><strong>threshold<\/strong>: \u8bbe\u7f6e\u5cf0\u503c\u4e0e\u5176\u90bb\u8fd1\u503c\u7684\u6700\u5c0f\u9ad8\u5ea6\u5dee\u3002<\/li>\n<li><strong>distance<\/strong>: \u8bbe\u7f6e\u76f8\u90bb\u5cf0\u503c\u4e4b\u95f4\u7684\u6700\u5c0f\u8ddd\u79bb\u3002<\/li>\n<li><strong>prominence<\/strong>: \u8bbe\u7f6e\u5cf0\u503c\u7684\u663e\u8457\u6027\u3002<\/li>\n<li><strong>width<\/strong>: \u8bbe\u7f6e\u5cf0\u503c\u7684\u6700\u5c0f\u548c\u6700\u5927\u5bbd\u5ea6\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u8bbe\u7f6eheight\u548cdistance\u53c2\u6570\u6765\u68c0\u6d4b\u5cf0\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, properties = find_peaks(y, height=0.5, distance=5)<\/p>\n<h2><strong>\u6253\u5370\u5cf0\u503c\u4f4d\u7f6e\u548c\u5bf9\u5e94\u7684\u9ad8\u5ea6<\/strong><\/h2>\n<p>print(&quot;Peak positions:&quot;, peaks)<\/p>\n<p>print(&quot;Peak heights:&quot;, properties[&#39;peak_heights&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eheight=0.5\uff0c\u6211\u4eec\u53ea\u68c0\u6d4b\u9ad8\u5ea6\u5927\u4e8e0.5\u7684\u5cf0\u503c\uff1b\u901a\u8fc7\u8bbe\u7f6edistance=5\uff0c\u6211\u4eec\u786e\u4fdd\u76f8\u90bb\u5cf0\u503c\u4e4b\u95f4\u7684\u8ddd\u79bb\u81f3\u5c11\u4e3a5\u4e2a\u6570\u636e\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u68c0\u6d4b\u8d1f\u5cf0\u503c<\/h4>\n<\/p>\n<p><p>find_peaks\u51fd\u6570\u9ed8\u8ba4\u53ea\u68c0\u6d4b\u6b63\u5cf0\u503c\uff08\u5373\u5c40\u90e8\u6700\u5927\u503c\uff09\uff0c\u5982\u679c\u8981\u68c0\u6d4b\u8d1f\u5cf0\u503c\uff08\u5373\u5c40\u90e8\u6700\u5c0f\u503c\uff09\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u53d6\u53cd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u53d6\u53cd\u6570\u636e\u5e76\u4f7f\u7528find_peaks\u51fd\u6570\u68c0\u6d4b\u8d1f\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, properties = find_peaks(-y, height=-0.5)<\/p>\n<h2><strong>\u6253\u5370\u8d1f\u5cf0\u503c\u4f4d\u7f6e\u548c\u5bf9\u5e94\u7684\u9ad8\u5ea6<\/strong><\/h2>\n<p>print(&quot;Negative peak positions:&quot;, peaks)<\/p>\n<p>print(&quot;Negative peak heights:&quot;, -properties[&#39;peak_heights&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6570\u636e\u53d6\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5230\u6240\u6709\u5c40\u90e8\u6700\u5c0f\u503c\u7684\u4f4d\u7f6e\u53ca\u5176\u5bf9\u5e94\u7684\u9ad8\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001numpy\u5e93\u4e2d\u7684argrelextrema\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>argrelextrema\u51fd\u6570\u662fnumpy\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u6d4b\u4e00\u7ef4\u6570\u7ec4\u4e2d\u7684\u5c40\u90e8\u6781\u503c\u3002\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import argrelextrema<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u4f7f\u7528argrelextrema\u51fd\u6570\u68c0\u6d4b\u5c40\u90e8\u6700\u5927\u503c<\/strong><\/h2>\n<p>peaks = argrelextrema(y, np.greater)<\/p>\n<h2><strong>\u6253\u5370\u5c40\u90e8\u6700\u5927\u503c\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>print(&quot;Local maxima positions:&quot;, peaks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cargrelextrema\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5305\u542b\u5c40\u90e8\u6700\u5927\u503c\u4f4d\u7f6e\u7d22\u5f15\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u53c2\u6570\u8c03\u6574<\/h4>\n<\/p>\n<p><p>argrelextrema\u51fd\u6570\u7684\u53c2\u6570\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>data<\/strong>: \u5f85\u68c0\u6d4b\u7684\u6570\u7ec4\u3002<\/li>\n<li><strong>comparator<\/strong>: \u6bd4\u8f83\u51fd\u6570\uff0c\u7528\u4e8e\u786e\u5b9a\u5c40\u90e8\u6781\u503c\u3002\u5e38\u7528\u7684\u6bd4\u8f83\u51fd\u6570\u6709np.greater\uff08\u68c0\u6d4b\u5c40\u90e8\u6700\u5927\u503c\uff09\u548cnp.less\uff08\u68c0\u6d4b\u5c40\u90e8\u6700\u5c0f\u503c\uff09\u3002<\/li>\n<li><strong>order<\/strong>: \u90bb\u57df\u5927\u5c0f\uff0c\u6307\u5b9a\u5728\u591a\u5927\u7684\u90bb\u57df\u5185\u5bfb\u627e\u6781\u503c\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u8bbe\u7f6eorder\u53c2\u6570\u6765\u68c0\u6d4b\u5c40\u90e8\u6781\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import argrelextrema<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u4f7f\u7528argrelextrema\u51fd\u6570\u68c0\u6d4b\u5c40\u90e8\u6700\u5927\u503c<\/strong><\/h2>\n<p>peaks = argrelextrema(y, np.greater, order=5)<\/p>\n<h2><strong>\u6253\u5370\u5c40\u90e8\u6700\u5927\u503c\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>print(&quot;Local maxima positions:&quot;, peaks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eorder=5\uff0c\u6211\u4eec\u5728\u957f\u5ea6\u4e3a5\u7684\u6570\u636e\u6bb5\u5185\u5bfb\u627e\u5c40\u90e8\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><h4>2.3 \u68c0\u6d4b\u5c40\u90e8\u6700\u5c0f\u503c<\/h4>\n<\/p>\n<p><p>argrelextrema\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u5c40\u90e8\u6700\u5c0f\u503c\uff0c\u53ea\u9700\u5c06\u6bd4\u8f83\u51fd\u6570\u8bbe\u7f6e\u4e3anp.less\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import argrelextrema<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u4f7f\u7528argrelextrema\u51fd\u6570\u68c0\u6d4b\u5c40\u90e8\u6700\u5c0f\u503c<\/strong><\/h2>\n<p>troughs = argrelextrema(y, np.less, order=5)<\/p>\n<h2><strong>\u6253\u5370\u5c40\u90e8\u6700\u5c0f\u503c\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>print(&quot;Local minima positions:&quot;, troughs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u6bd4\u8f83\u51fd\u6570\u8bbe\u7f6e\u4e3anp.less\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5230\u6240\u6709\u5c40\u90e8\u6700\u5c0f\u503c\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001pandas\u5e93\u4e2d\u7684\u5185\u7f6e\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u4e2d\u7684DataFrame\u548cSeries\u5bf9\u8c61\u4e5f\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u68c0\u6d4b\u5cf0\u503c\u3002\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;y&#39;: y})<\/p>\n<h2><strong>\u4f7f\u7528diff\u65b9\u6cd5\u8ba1\u7b97\u5dee\u5206<\/strong><\/h2>\n<p>df[&#39;diff&#39;] = df[&#39;y&#39;].diff()<\/p>\n<h2><strong>\u4f7f\u7528shift\u65b9\u6cd5\u8ba1\u7b97\u6ede\u540e\u5dee\u5206<\/strong><\/h2>\n<p>df[&#39;shift_diff&#39;] = df[&#39;diff&#39;].shift(-1)<\/p>\n<h2><strong>\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>df[&#39;peak&#39;] = (df[&#39;diff&#39;] &gt; 0) &amp; (df[&#39;shift_diff&#39;] &lt; 0)<\/p>\n<h2><strong>\u6253\u5370\u5cf0\u503c\u4f4d\u7f6e<\/strong><\/h2>\n<p>print(&quot;Peak positions:&quot;, df[df[&#39;peak&#39;]].index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8ba1\u7b97\u5dee\u5206\u548c\u6ede\u540e\u5dee\u5206\u6765\u68c0\u6d4b\u5cf0\u503c\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u53c2\u6570\u8c03\u6574<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u7684\u65b9\u6cd5\u53ef\u4ee5\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5cf0\u503c\u68c0\u6d4b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408rolling\u65b9\u6cd5\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;y&#39;: y})<\/p>\n<h2><strong>\u4f7f\u7528rolling\u65b9\u6cd5\u8fdb\u884c\u5e73\u6ed1\u5904\u7406<\/strong><\/h2>\n<p>df[&#39;smooth_y&#39;] = df[&#39;y&#39;].rolling(window=5).mean()<\/p>\n<h2><strong>\u4f7f\u7528diff\u65b9\u6cd5\u8ba1\u7b97\u5dee\u5206<\/strong><\/h2>\n<p>df[&#39;diff&#39;] = df[&#39;smooth_y&#39;].diff()<\/p>\n<h2><strong>\u4f7f\u7528shift\u65b9\u6cd5\u8ba1\u7b97\u6ede\u540e\u5dee\u5206<\/strong><\/h2>\n<p>df[&#39;shift_diff&#39;] = df[&#39;diff&#39;].shift(-1)<\/p>\n<h2><strong>\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>df[&#39;peak&#39;] = (df[&#39;diff&#39;] &gt; 0) &amp; (df[&#39;shift_diff&#39;] &lt; 0)<\/p>\n<h2><strong>\u6253\u5370\u5cf0\u503c\u4f4d\u7f6e<\/strong><\/h2>\n<p>print(&quot;Peak positions:&quot;, df[df[&#39;peak&#39;]].index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u51cf\u5c11\u566a\u58f0\u5bf9\u5cf0\u503c\u68c0\u6d4b\u7684\u5f71\u54cd\uff0c\u4ece\u800c\u63d0\u9ad8\u5cf0\u503c\u68c0\u6d4b\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>3.3 \u68c0\u6d4b\u8d1f\u5cf0\u503c<\/h4>\n<\/p>\n<p><p>\u4e0e\u68c0\u6d4b\u6b63\u5cf0\u503c\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u5dee\u5206\u548c\u6ede\u540e\u5dee\u5206\u6765\u68c0\u6d4b\u8d1f\u5cf0\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + np.random.normal(0, 0.1, 100)<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;y&#39;: y})<\/p>\n<h2><strong>\u4f7f\u7528diff\u65b9\u6cd5\u8ba1\u7b97\u5dee\u5206<\/strong><\/h2>\n<p>df[&#39;diff&#39;] = df[&#39;y&#39;].diff()<\/p>\n<h2><strong>\u4f7f\u7528shift\u65b9\u6cd5\u8ba1\u7b97\u6ede\u540e\u5dee\u5206<\/strong><\/h2>\n<p>df[&#39;shift_diff&#39;] = df[&#39;diff&#39;].shift(-1)<\/p>\n<h2><strong>\u68c0\u6d4b\u8d1f\u5cf0\u503c<\/strong><\/h2>\n<p>df[&#39;trough&#39;] = (df[&#39;diff&#39;] &lt; 0) &amp; (df[&#39;shift_diff&#39;] &gt; 0)<\/p>\n<h2><strong>\u6253\u5370\u8d1f\u5cf0\u503c\u4f4d\u7f6e<\/strong><\/h2>\n<p>print(&quot;Negative peak positions:&quot;, df[df[&#39;trough&#39;]].index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5230\u6240\u6709\u5c40\u90e8\u6700\u5c0f\u503c\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>4.1 \u80a1\u7968\u4ef7\u683c\u5cf0\u503c\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5cf0\u503c\u68c0\u6d4b\u5e38\u7528\u4e8e\u5206\u6790\u80a1\u7968\u4ef7\u683c\u7684\u8d70\u52bf\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528scipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570\u6765\u68c0\u6d4b\u80a1\u7968\u4ef7\u683c\u7684\u5cf0\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u83b7\u53d6\u80a1\u7968\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(&#39;AAPL&#39;, start=&#39;2022-01-01&#39;, end=&#39;2022-12-31&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6536\u76d8\u4ef7<\/strong><\/h2>\n<p>close_prices = data[&#39;Close&#39;].values<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, properties = find_peaks(close_prices, height=150, distance=5)<\/p>\n<h2><strong>\u7ed8\u5236\u80a1\u7968\u4ef7\u683c\u548c\u5cf0\u503c\u4f4d\u7f6e<\/strong><\/h2>\n<p>plt.plot(close_prices, label=&#39;Close Prices&#39;)<\/p>\n<p>plt.plot(peaks, close_prices[peaks], &#39;rx&#39;, label=&#39;Peaks&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5230\u82f9\u679c\u516c\u53f8\u80a1\u7968\u57282022\u5e74\u5185\u7684\u6240\u6709\u5cf0\u503c\u4f4d\u7f6e\uff0c\u5e76\u5c06\u5176\u7ed8\u5236\u5728\u4ef7\u683c\u8d70\u52bf\u56fe\u4e0a\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u5fc3\u7535\u56fe\u5cf0\u503c\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>\u5fc3\u7535\u56fe\uff08ECG\uff09\u662f\u533b\u5b66\u9886\u57df\u4e2d\u5e38\u89c1\u7684\u4fe1\u53f7\uff0c\u5cf0\u503c\u68c0\u6d4b\u5728\u5fc3\u7535\u56fe\u4fe1\u53f7\u5206\u6790\u4e2d\u5177\u6709\u91cd\u8981\u4f5c\u7528\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528scipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570\u6765\u68c0\u6d4b\u5fc3\u7535\u56fe\u4fe1\u53f7\u7684\u5cf0\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.signal import find_peaks<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u5fc3\u7535\u56fe\u4fe1\u53f7<\/strong><\/h2>\n<p>t = np.linspace(0, 1, 1000)<\/p>\n<p>ecg_signal = 1.5 * np.sin(5 * np.pi * t) + np.random.normal(0, 0.1, 1000)<\/p>\n<h2><strong>\u4f7f\u7528find_peaks\u51fd\u6570\u68c0\u6d4b\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, properties = find_peaks(ecg_signal, height=1, distance=50)<\/p>\n<h2><strong>\u7ed8\u5236\u5fc3\u7535\u56fe\u4fe1\u53f7\u548c\u5cf0\u503c\u4f4d\u7f6e<\/strong><\/h2>\n<p>plt.plot(ecg_signal, label=&#39;ECG Signal&#39;)<\/p>\n<p>plt.plot(peaks, ecg_signal[peaks], &#39;rx&#39;, label=&#39;Peaks&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5230\u5fc3\u7535\u56fe\u4fe1\u53f7\u4e2d\u7684\u6240\u6709\u5cf0\u503c\u4f4d\u7f6e\uff0c\u5e76\u5c06\u5176\u7ed8\u5236\u5728\u4fe1\u53f7\u56fe\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u8fdb\u884c\u66f2\u7ebf\u5cf0\u503c\u68c0\u6d4b\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecscipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570\u3001numpy\u5e93\u4e2d\u7684argrelextrema\u51fd\u6570\u3001\u4ee5\u53capandas\u5e93\u4e2d\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5cf0\u503c\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><p><strong>find_peaks\u51fd\u6570\u56e0\u5176\u529f\u80fd\u5f3a\u5927\u4e14\u5bb9\u6613\u4f7f\u7528\uff0c\u901a\u5e38\u662f\u5904\u7406\u5cf0\u503c\u68c0\u6d4b\u7684\u9996\u9009\u65b9\u6cd5\u3002<\/strong>\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u6574\u5cf0\u503c\u68c0\u6d4b\u7684\u7075\u654f\u5ea6\u548c\u7cbe\u786e\u5ea6\u3002\u6b64\u5916\uff0c\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u80a1\u7968\u4ef7\u683c\u548c\u5fc3\u7535\u56fe\u4fe1\u53f7\u7684\u5cf0\u503c\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u53ef\u4ee5\u638c\u63e1\u4f7f\u7528Python\u8fdb\u884c\u66f2\u7ebf\u5cf0\u503c\u68c0\u6d4b\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5e76\u80fd\u591f\u5c06\u5176\u5e94\u7528\u4e8e\u5b9e\u9645\u6570\u636e\u5206\u6790\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u66f2\u7ebf\u4ee5\u4fbf\u66f4\u597d\u5730\u8bc6\u522b\u5cf0\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u66f2\u7ebf\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\uff0c\u60a8\u53ef\u4ee5\u76f4\u89c2\u5730\u67e5\u770b\u6570\u636e\u7684\u53d8\u5316\u5e76\u8bc6\u522b\u5cf0\u503c\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u5c06\u6570\u636e\u53ef\u89c6\u5316\u3002\u786e\u4fdd\u5728\u56fe\u4e2d\u6807\u6ce8X\u8f74\u548cY\u8f74\uff0c\u4ee5\u4fbf\u6e05\u6670\u5c55\u793a\u6570\u636e\u542b\u4e49\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u6709\u6548\u5730\u627e\u5230\u66f2\u7ebf\u7684\u5cf0\u503c\uff1f<\/strong><br \/>\u4e3a\u4e86\u5bfb\u627e\u66f2\u7ebf\u7684\u5cf0\u503c\uff0cScipy\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>scipy.signal.find_peaks()<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u5c40\u90e8\u5cf0\u503c\u3002\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u9608\u503c\u3001\u6700\u5c0f\u8ddd\u79bb\u7b49\u53c2\u6570\uff0c\u4ee5\u4fbf\u66f4\u7cbe\u786e\u5730\u627e\u5230\u60a8\u611f\u5174\u8da3\u7684\u5cf0\u503c\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u566a\u58f0\u8f83\u5927\u7684\u6570\u636e\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u627e\u5230\u51c6\u786e\u7684\u5cf0\u503c\uff1f<\/strong><br \/>\u5728\u566a\u58f0\u8f83\u5927\u7684\u6570\u636e\u4e2d\uff0c\u627e\u5230\u51c6\u786e\u7684\u5cf0\u503c\u53ef\u80fd\u4f1a\u5177\u6709\u6311\u6218\u6027\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e73\u6ed1\u6ee4\u6ce2\u6280\u672f\uff0c\u6bd4\u5982Savitzky-Golay\u6ee4\u6ce2\u5668\u6216\u79fb\u52a8\u5e73\u5747\u6cd5\uff0c\u6765\u51cf\u5c11\u6570\u636e\u4e2d\u7684\u566a\u58f0\u3002\u7ecf\u8fc7\u5e73\u6ed1\u5904\u7406\u540e\uff0c\u518d\u4f7f\u7528\u5cf0\u503c\u68c0\u6d4b\u7b97\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6c42\u66f2\u7ebf\u7684\u5cf0\u503c\u53ef\u4ee5\u4f7f\u7528scipy\u5e93\u4e2d\u7684find_peaks\u51fd\u6570\u3001numpy\u5e93\u4e2d\u7684argrelextr [&hellip;]","protected":false},"author":3,"featured_media":1165273,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165267"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1165267"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165267\/revisions"}],"predecessor-version":[{"id":1165276,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165267\/revisions\/1165276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165273"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}