{"id":1024409,"date":"2024-12-30T14:13:36","date_gmt":"2024-12-30T06:13:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024409.html"},"modified":"2024-12-30T14:13:38","modified_gmt":"2024-12-30T06:13:38","slug":"python%e5%a6%82%e4%bd%95%e6%8b%9f%e5%90%88arma%e6%a8%a1%e5%9e%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024409.html","title":{"rendered":"python\u5982\u4f55\u62df\u5408ARMA\u6a21\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e2448a11-7bce-474d-a7b9-33366dc49b9f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u62df\u5408ARMA\u6a21\u578b\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u62df\u5408ARMA\u6a21\u578b<\/strong><\/p>\n<\/p>\n<p><p><strong>Python\u62df\u5408ARMA\u6a21\u578b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528statsmodels\u5e93\u3001\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u53c2\u6570\u3001\u8fdb\u884c\u6a21\u578b\u62df\u5408\u3001\u8fdb\u884c\u6a21\u578b\u8bca\u65ad\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528statsmodels\u5e93\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u5de5\u5177\u3002\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u76f8\u5173\u7684\u5e93\uff0c\u7136\u540e\u51c6\u5907\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u4f7f\u7528\u81ea\u76f8\u5173\u51fd\u6570\u548c\u504f\u81ea\u76f8\u5173\u51fd\u6570\u56fe\u6765\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\uff0c\u6700\u540e\u4f7f\u7528ARMA\u6a21\u578b\u8fdb\u884c\u62df\u5408\uff0c\u5e76\u8fdb\u884c\u6a21\u578b\u8bca\u65ad\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528STATSModels\u5e93<\/p>\n<\/p>\n<p><p>StatsModels\u662f\u4e00\u4e2a\u7528\u4e8e\u4f30\u8ba1\u548c\u63a8\u65ad\u7edf\u8ba1\u6a21\u578b\u7684Python\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u5de5\u5177\uff0c\u5305\u62ecARMA\u6a21\u578b\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165StatsModels\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statsmodels.api as sm<\/p>\n<p>import statsmodels.tsa.api as tsa<\/p>\n<p>import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u51c6\u5907\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u62df\u5408ARMA\u6a21\u578b\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u4e8eCSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5176\u4ed6\u6570\u636e\u6e90\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528Pandas\u5e93\u5bfc\u5165\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;your_time_series_data.csv&#39;)<\/p>\n<p>time_series = data[&#39;your_column_name&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u662f\u4e00\u4e2aPandas Series\u5bf9\u8c61\uff0c\u5e76\u4e14\u65e5\u671f\u65f6\u95f4\u7d22\u5f15\u5df2\u7ecf\u8bbe\u7f6e\u597d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">time_series.index = pd.to_datetime(time_series.index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684ARMA\u6a21\u578b\u53c2\u6570\uff08p, q\uff09\u662f\u62df\u5408\u6a21\u578b\u7684\u5173\u952e\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528\u81ea\u76f8\u5173\u51fd\u6570\uff08ACF\uff09\u548c\u504f\u81ea\u76f8\u5173\u51fd\u6570\uff08PACF\uff09\u56fe\u6765\u5e2e\u52a9\u9009\u62e9\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from statsmodels.graphics.tsaplots import plot_acf, plot_pacf<\/p>\n<p>plot_acf(time_series)<\/p>\n<p>plot_pacf(time_series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u89c2\u5bdfACF\u548cPACF\u56fe\uff0c\u53ef\u4ee5\u786e\u5b9a\u5408\u9002\u7684p\u548cq\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679cPACF\u5728\u7b2cp\u9636\u663e\u8457\u622a\u65ad\uff0c\u800cACF\u5728\u7b2cq\u9636\u663e\u8457\u622a\u65ad\uff0c\u5219\u53ef\u4ee5\u9009\u62e9p\u548cq\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u62df\u5408ARMA\u6a21\u578b<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u597d\u5408\u9002\u7684p\u548cq\u503c\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528StatsModels\u5e93\u4e2d\u7684ARMA\u7c7b\u6765\u62df\u5408\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">model = tsa.ARMA(time_series, order=(p, q))<\/p>\n<p>result = model.fit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6a21\u578b\u8bca\u65ad<\/p>\n<\/p>\n<p><p>\u62df\u5408\u6a21\u578b\u540e\uff0c\u9700\u8981\u8fdb\u884c\u6a21\u578b\u8bca\u65ad\uff0c\u4ee5\u786e\u4fdd\u6a21\u578b\u7684\u6b8b\u5dee\u662f\u767d\u566a\u58f0\u3002\u5982\u679c\u6b8b\u5dee\u4e0d\u662f\u767d\u566a\u58f0\uff0c\u8bf4\u660e\u6a21\u578b\u53ef\u80fd\u4e0d\u9002\u5408\u6570\u636e\u3002\u53ef\u4ee5\u7ed8\u5236\u6b8b\u5dee\u56fe\u548c\u8fdb\u884cLjung-Box\u68c0\u9a8c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">residuals = result.resid<\/p>\n<p>plot_acf(residuals)<\/p>\n<p>sm.stats.acorr_ljungbox(residuals, lags=[10], return_df=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u8fdb\u884c\u9884\u6d4b<\/p>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u578b\u901a\u8fc7\u4e86\u8bca\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u8fdb\u884c\u9884\u6d4b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">forecast = result.forecast(steps=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u672a\u676510\u4e2a\u65f6\u95f4\u6b65\u7684\u9884\u6d4b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684StatsModels\u5e93\u6765\u62df\u5408ARMA\u6a21\u578b\u3002\u9996\u5148\u9700\u8981\u51c6\u5907\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528ACF\u548cPACF\u56fe\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u53c2\u6570\uff0c\u63a5\u7740\u8fdb\u884c\u6a21\u578b\u62df\u5408\u548c\u8bca\u65ad\uff0c\u6700\u540e\u8fdb\u884c\u9884\u6d4b\u3002\u786e\u4fdd\u6bcf\u4e00\u6b65\u90fd\u4ed4\u7ec6\u68c0\u67e5\uff0c\u786e\u4fdd\u6a21\u578b\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fARMA\u6a21\u578b\uff0c\u4e3a\u4ec0\u4e48\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\u4f7f\u7528\u5b83\uff1f<\/strong><br \/>ARMA\u6a21\u578b\uff0c\u5373\u81ea\u56de\u5f52\u79fb\u52a8\u5e73\u5747\u6a21\u578b\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u5206\u6790\u548c\u9884\u6d4b\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u7edf\u8ba1\u65b9\u6cd5\u3002\u5b83\u7ed3\u5408\u4e86\u81ea\u56de\u5f52\uff08AR\uff09\u548c\u79fb\u52a8\u5e73\u5747\uff08MA\uff09\u4e24\u79cd\u6210\u5206\uff0c\u9002\u7528\u4e8e\u5e73\u7a33\u65f6\u95f4\u5e8f\u5217\u3002\u901a\u8fc7\u6355\u6349\u6570\u636e\u7684\u81ea\u76f8\u5173\u6027\u548c\u968f\u673a\u6ce2\u52a8\uff0cARMA\u6a21\u578b\u5e2e\u52a9\u5206\u6790\u5e08\u7406\u89e3\u6570\u636e\u7684\u5185\u5728\u7ed3\u6784\uff0c\u4ece\u800c\u8fdb\u884c\u6709\u6548\u7684\u9884\u6d4b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u54ea\u4e2a\u5e93\u6765\u62df\u5408ARMA\u6a21\u578b\u6700\u4e3a\u5408\u9002\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>statsmodels<\/code>\u5e93\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u5de5\u5177\uff0c\u5305\u62ecARMA\u6a21\u578b\u7684\u62df\u5408\u3002\u9664\u4e86<code>statsmodels<\/code>\uff0c<code>pandas<\/code>\u548c<code>numpy<\/code>\u4e5f\u5e38\u5e38\u88ab\u7528\u6765\u5904\u7406\u548c\u51c6\u5907\u6570\u636e\u3002\u4f7f\u7528<code>statsmodels<\/code>\u7684<code>ARMA<\/code>\u7c7b\uff0c\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u548c\u62df\u5408\u6a21\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bc4\u4f30ARMA\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\uff1f<\/strong><br \/>\u8bc4\u4f30ARMA\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\uff0c\u5305\u62ec\u6b8b\u5dee\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>C\uff08\u8d64\u6c60\u4fe1\u606f\u91cf\u51c6\u5219\uff09\u548cBIC\uff08\u8d1d\u53f6\u65af\u4fe1\u606f\u51c6\u5219\uff09\u3002\u89c2\u5bdf\u6b8b\u5dee\u7684\u81ea\u76f8\u5173\u56fe\u53ef\u4ee5\u5e2e\u52a9\u5224\u65ad\u6a21\u578b\u7684\u9002\u7528\u6027\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6b8b\u5dee\u5e94\u5f53\u662f\u767d\u566a\u58f0\u3002\u6b64\u5916\uff0c\u8f83\u4f4e\u7684AIC\u548cBIC\u503c\u901a\u5e38\u6307\u793a\u6a21\u578b\u5728\u6570\u636e\u62df\u5408\u4e0a\u7684\u66f4\u4f18\u8868\u73b0\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u9009\u62e9\u5230\u6700\u5408\u9002\u7684\u6a21\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u62df\u5408ARMA\u6a21\u578b Python\u62df\u5408ARMA\u6a21\u578b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528statsmodels\u5e93\u3001\u9009\u62e9\u5408 [&hellip;]","protected":false},"author":3,"featured_media":1024418,"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\/1024409"}],"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=1024409"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024409\/revisions"}],"predecessor-version":[{"id":1024422,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024409\/revisions\/1024422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024418"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}