{"id":1059493,"date":"2024-12-31T15:25:39","date_gmt":"2024-12-31T07:25:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059493.html"},"modified":"2024-12-31T15:25:41","modified_gmt":"2024-12-31T07:25:41","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b7%ae%e5%88%86%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059493.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5dee\u5206\u6570\u636e\u5206\u6790"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/849e44f8-8ed9-441e-8980-83b8e4cfdd6b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5dee\u5206\u6570\u636e\u5206\u6790\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u52a0\u8f7d\u6570\u636e\u3001\u53ef\u89c6\u5316\u6570\u636e\u3001\u8fdb\u884c\u5dee\u5206\u5904\u7406\u3001\u68c0\u9a8c\u5dee\u5206\u7ed3\u679c\u3001\u9009\u62e9\u548c\u8bad\u7ec3\u6a21\u578b\u3001\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/strong>\u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u9009\u62e9\u548c\u8bad\u7ec3\u6a21\u578b\u662f\u5b9e\u73b0\u51c6\u786e\u5dee\u5206\u6570\u636e\u5206\u6790\u7684\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002\u901a\u8fc7\u9009\u62e9\u9002\u5f53\u7684\u6a21\u578b\uff0c\u53ef\u4ee5\u6355\u6349\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u548c\u8d8b\u52bf\uff0c\u8fdb\u800c\u8fdb\u884c\u6709\u6548\u9884\u6d4b\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u4e00\u4e9b\u5fc5\u8981\u7684Python\u5e93\u3002\u8fd9\u4e9b\u5e93\u5305\u62ecPandas\u3001NumPy\u3001Matplotlib\u548cStatsmodels\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from statsmodels.tsa.stattools import adfuller<\/p>\n<p>from statsmodels.graphics.tsaplots import plot_acf, plot_pacf<\/p>\n<p>from statsmodels.tsa.arima_model import ARIMA<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5e93\u5206\u522b\u7528\u4e8e\u6570\u636e\u5904\u7406\u3001\u6570\u503c\u8ba1\u7b97\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u65f6\u95f4\u5e8f\u5217\u68c0\u9a8c\u548c\u65f6\u95f4\u5e8f\u5217\u5efa\u6a21\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u52a0\u8f7d\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u672c\u5730\u6587\u4ef6\uff08\u5982CSV\u6587\u4ef6\uff09\u6216\u5728\u7ebf\u6570\u636e\u6e90\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u793a\u4f8bCSV\u6587\u4ef6\u6765\u6f14\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;data.csv&#39;, index_col=&#39;Date&#39;, parse_dates=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u52a0\u8f7d\u5230\u4e00\u4e2aPandas DataFrame\u4e2d\uff0c\u5e76\u5c06\u65e5\u671f\u5217\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u53ef\u89c6\u5316\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5dee\u5206\u5904\u7406\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u4ee5\u4fbf\u4e86\u89e3\u6570\u636e\u7684\u57fa\u672c\u7279\u5f81\u548c\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data.plot()<\/p>\n<p>plt.title(&#39;Original Data&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed8\u5236\u6570\u636e\u7684\u65f6\u95f4\u5e8f\u5217\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u6570\u636e\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8fdb\u884c\u5dee\u5206\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5dee\u5206\u5904\u7406\u662f\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u7528\u4e8e\u6d88\u9664\u6570\u636e\u4e2d\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u6210\u5206\u3002\u5dee\u5206\u5904\u7406\u7684\u57fa\u672c\u601d\u60f3\u662f\u8ba1\u7b97\u76f8\u90bb\u6570\u636e\u70b9\u4e4b\u95f4\u7684\u5dee\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_diff = data.diff().dropna()<\/p>\n<p>data_diff.plot()<\/p>\n<p>plt.title(&#39;Differenced Data&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684<code>diff()<\/code>\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5dee\u5206\u5904\u7406\uff0c\u5e76\u7ed8\u5236\u5dee\u5206\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u68c0\u9a8c\u5dee\u5206\u7ed3\u679c<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5dee\u5206\u5904\u7406\u540e\uff0c\u6211\u4eec\u9700\u8981\u68c0\u9a8c\u6570\u636e\u7684\u5e73\u7a33\u6027\u3002\u5e38\u7528\u7684\u5e73\u7a33\u6027\u68c0\u9a8c\u65b9\u6cd5\u5305\u62ecADF\uff08Augmented Dickey-Fuller\uff09\u68c0\u9a8c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = adfuller(data_diff)<\/p>\n<p>print(&#39;ADF Statistic:&#39;, result[0])<\/p>\n<p>print(&#39;p-value:&#39;, result[1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cp\u503c\u5c0f\u4e8e\u663e\u8457\u6027\u6c34\u5e73\uff08\u901a\u5e38\u4e3a0.05\uff09\uff0c\u5219\u53ef\u4ee5\u8ba4\u4e3a\u6570\u636e\u662f\u5e73\u7a33\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u9009\u62e9\u548c\u8bad\u7ec3\u6a21\u578b<\/h2>\n<\/p>\n<p><p>\u5728\u5dee\u5206\u5904\u7406\u548c\u5e73\u7a33\u6027\u68c0\u9a8c\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u9009\u62e9\u9002\u5f53\u7684\u65f6\u95f4\u5e8f\u5217\u6a21\u578b\u3002\u5e38\u7528\u7684\u6a21\u578b\u5305\u62ecARIMA\uff08AutoRegressive Integrated Moving Average\uff09\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">model = ARIMA(data, order=(p, d, q))<\/p>\n<p>model_fit = model.fit(disp=0)<\/p>\n<p>print(model_fit.summary())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ARIMA\u6a21\u578b\u5bf9\u6570\u636e\u8fdb\u884c\u5efa\u6a21\uff0c\u5e76\u8f93\u51fa\u6a21\u578b\u6458\u8981\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u9884\u6d4b\u503c\u4e0e\u5b9e\u9645\u503c\u4e4b\u95f4\u7684\u8bef\u5dee\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">forecast = model_fit.forecast(steps=10)[0]<\/p>\n<p>plt.plot(data.index, data, label=&#39;Original Data&#39;)<\/p>\n<p>plt.plot(pd.date_range(data.index[-1], periods=10, freq=&#39;D&#39;), forecast, label=&#39;Forecast&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed8\u5236\u9884\u6d4b\u503c\u4e0e\u5b9e\u9645\u503c\u7684\u5bf9\u6bd4\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u8bc4\u4f30\u6a21\u578b\u7684\u9884\u6d4b\u6027\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u3002\u5dee\u5206\u5904\u7406\u53ef\u4ee5\u6d88\u9664\u6570\u636e\u4e2d\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u6210\u5206\uff0c\u4ece\u800c\u4f7f\u6570\u636e\u66f4\u52a0\u5e73\u7a33\u3002\u9009\u62e9\u9002\u5f53\u7684\u65f6\u95f4\u5e8f\u5217\u6a21\u578b\uff08\u5982ARIMA\u6a21\u578b\uff09\u5e76\u8fdb\u884c\u8bad\u7ec3\uff0c\u53ef\u4ee5\u6355\u6349\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u548c\u8d8b\u52bf\uff0c\u8fdb\u800c\u5b9e\u73b0\u6709\u6548\u9884\u6d4b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u7684\u4e3b\u8981\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u901a\u5e38\u6d89\u53ca\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u6570\u636e\u662f\u65f6\u95f4\u5e8f\u5217\u683c\u5f0f\uff0c\u5e76\u5904\u7406\u7f3a\u5931\u503c\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684<code>diff()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5dee\u5206\u3002\u63a5\u4e0b\u6765\uff0c\u7ed8\u5236\u5dee\u5206\u540e\u7684\u6570\u636e\u56fe\u8868\uff0c\u4ee5\u89c2\u5bdf\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u53d8\u5316\u3002\u6700\u540e\uff0c\u4f7f\u7528\u7edf\u8ba1\u6d4b\u8bd5\uff08\u5982ADF\u6d4b\u8bd5\uff09\u6765\u68c0\u9a8c\u6570\u636e\u7684\u5e73\u7a33\u6027\uff0c\u4ece\u800c\u51b3\u5b9a\u662f\u5426\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u5dee\u5206\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5dee\u5206\u9636\u6570\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u5dee\u5206\u9636\u6570\u901a\u5e38\u4f9d\u8d56\u4e8e\u6570\u636e\u7684\u7279\u6027\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u521d\u6b65\u5dee\u5206\uff08\u5373\u4e00\u9636\u5dee\u5206\uff09\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u8d77\u70b9\u3002\u5982\u679c\u6570\u636e\u4ecd\u7136\u663e\u793a\u51fa\u8d8b\u52bf\u6216\u5b63\u8282\u6027\u7279\u5f81\uff0c\u53ef\u4ee5\u8003\u8651\u8fdb\u884c\u4e8c\u9636\u5dee\u5206\u6216\u5b63\u8282\u6027\u5dee\u5206\u3002\u4f7f\u7528\u81ea\u76f8\u5173\u51fd\u6570\uff08ACF\uff09\u548c\u504f\u81ea\u76f8\u5173\u51fd\u6570\uff08PACF\uff09\u56fe\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790\u548c\u51b3\u5b9a\u6700\u5408\u9002\u7684\u5dee\u5206\u9636\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u53ef\u89c6\u5316\u5dee\u5206\u6570\u636e\u5206\u6790\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u53ef\u89c6\u5316\u5dee\u5206\u6570\u636e\u5206\u6790\u7ed3\u679c\u7684\u5de5\u5177\u4e3b\u8981\u6709<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u3002\u901a\u8fc7\u7ed8\u5236\u65f6\u95f4\u5e8f\u5217\u56fe\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u5230\u5dee\u5206\u524d\u540e\u7684\u53d8\u5316\u3002\u6b64\u5916\uff0c\u76f4\u65b9\u56fe\u548cQ-Q\u56fe\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u8bc4\u4f30\u6570\u636e\u7684\u5206\u5e03\u7279\u5f81\u548c\u7a33\u5b9a\u6027\u3002\u4f7f\u7528\u8fd9\u4e9b\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u6e05\u6670\u5730\u7406\u89e3\u6570\u636e\u53d8\u5316\u7684\u6a21\u5f0f\uff0c\u5e76\u4e3a\u540e\u7eed\u7684\u5206\u6790\u63d0\u4f9b\u6709\u529b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8fdb\u884c\u5dee\u5206\u6570\u636e\u5206\u6790\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u52a0\u8f7d\u6570\u636e\u3001\u53ef\u89c6\u5316\u6570\u636e\u3001\u8fdb\u884c\u5dee\u5206\u5904\u7406\u3001\u68c0\u9a8c\u5dee\u5206\u7ed3\u679c\u3001\u9009\u62e9 [&hellip;]","protected":false},"author":3,"featured_media":1059507,"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\/1059493"}],"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=1059493"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059493\/revisions"}],"predecessor-version":[{"id":1059509,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059493\/revisions\/1059509"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059507"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}