{"id":4389,"date":"2024-01-26T16:51:18","date_gmt":"2024-01-26T07:51:18","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=4389"},"modified":"2024-01-26T16:51:18","modified_gmt":"2024-01-26T07:51:18","slug":"arima%e3%83%a2%e3%83%87%e3%83%ab%e3%81%a7%e6%99%82%e7%b3%bb%e5%88%97%e4%ba%88%e6%b8%ac%e3%82%92%e3%82%84%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/arima%e3%83%a2%e3%83%87%e3%83%ab%e3%81%a7%e6%99%82%e7%b3%bb%e5%88%97%e4%ba%88%e6%b8%ac%e3%82%92%e3%82%84%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b\/","title":{"rendered":"ARIMA\u30e2\u30c7\u30eb\u3067\u6642\u7cfb\u5217\u4e88\u6e2c\u3092\u3084\u3063\u3066\u307f\u308b"},"content":{"rendered":"\n<p>ARIMA\u30e2\u30c7\u30eb\u3067\u6642\u7cfb\u5217\u4e88\u6e2c\u3092\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002\r\n<\/p>\n\n\n<h2>\u4e00\u822c\u7684\u306a\u6642\u7cfb\u5217\u30e2\u30c7\u30eb\u3067\u4e88\u6e2c\u3057\u3066\u307f\u308b<\/h2>\n<p>\u738b\u9053\u7684\u306a\u30e2\u30c7\u30eb\u3068\u3057\u3066ARIMA\u30e2\u30c7\u30eb\u3084SARIMAX\u30e2\u30c7\u30eb\u3001Prophet\u306a\u3069\u6642\u7cfb\u5217\u30e2\u30c7\u30eb\u3067\u3042\u308a\u307e\u3059\u3088\u306d\u3002\u4eca\u56de\u306fARIMA\u30e2\u30c7\u30eb\u3067\u4e88\u6e2c\u304b\u3089\u7d50\u679c\u3092\u53ef\u8996\u5316\u3059\u308b\u30b3\u30fc\u30c9\u307e\u3067\u8f09\u305b\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u81ea\u5df1\u76f8\u95a2\u4fc2\u6570\u3084\u504f\u81ea\u5df1\u76f8\u95a2\u4fc2\u6570\u306e\u8868\u793a\u3082\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\r\n<p>\u53c2\u8003\u30b3\u30fc\u30c9\uff1a<a href=\"https:\/\/www.salesanalytics.co.jp\/datascience\/datascience087\/\">Python\u3067\u6642\u7cfb\u5217\u89e3\u6790\u30fb\u8d85\u5165\u9580\uff08\u305d\u306e3\uff09ARIMA\u7cfb\u30e2\u30c7\u30eb\u3067\u4e88\u6e2c\u3059\u308b\u65b9\u6cd5 \u2013 \u30bb\u30fc\u30eb\u30b9\u30a2\u30ca\u30ea\u30c6\u30a3\u30af\u30b9 (salesanalytics.co.jp)<\/a><\/p>\n\n<h2>\u6642\u7cfb\u5217\u30c7\u30fc\u30bf\u3092\u30a4\u30f3\u30dd\u30fc\u30c8<\/h2>\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>!pip install pmdarima\r\n!pip install optuna\r\n!pip install japanize_matplotlib\r\n\r\nimport japanize_matplotlib as plt\r\nimport pandas as pd\r\nimport numpy as np\r\nfrom scipy import signal\r\nfrom statsmodels.tsa.seasonal import seasonal_decompose\r\nfrom statsmodels.tsa.seasonal import STL\r\nfrom statsmodels.tsa.stattools import adfuller\r\nfrom statsmodels.graphics.tsaplots import plot_acf, plot_pacf\r\nimport pmdarima as pm\r\nfrom pmdarima import utils\r\nfrom pmdarima import arima\r\nfrom pmdarima import model_selection\r\nfrom statsmodels.tsa.statespace.sarimax import SARIMAX\r\nimport optuna\r\nfrom prophet import Prophet\r\nfrom prophet.diagnostics import cross_validation\r\nfrom prophet.diagnostics import performance_metrics\r\n\r\nurl='https:\/\/www.salesanalytics.co.jp\/591h' #\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u3042\u308bURL\r\ndf=pd.read_csv(url, #\u8aad\u307f\u8fbc\u3080\u30c7\u30fc\u30bf\u306eURL\r\nindex_col='Month', #\u5909\u6570\u300cMonth\u300d\u3092\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u8a2d\u5b9a\r\nparse_dates=True) #\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u65e5\u4ed8\u578b\u306b\u8a2d\u5b9a\r\n\r\n# \u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\r\nurl='https:\/\/www.salesanalytics.co.jp\/591h'\r\ndf=pd.read_csv(url, index_col='Month', parse_dates=True)\r\n\r\n# matplotlib\u3067\u30b0\u30e9\u30d5\u306e\u30b9\u30bf\u30a4\u30eb\u3092\u8a2d\u5b9a\r\nplt.style.use('ggplot')\r\n\r\n# \u30b0\u30e9\u30d5\u306e\u63cf\u753b\r\ndf.plot(figsize=(10, 5)) # figsize\u3067\u30b0\u30e9\u30d5\u306e\u30b5\u30a4\u30ba\u3092\u6307\u5b9a\r\n\r\n# \u30b0\u30e9\u30d5\u306e\u30bf\u30a4\u30c8\u30eb\u3068\u8ef8\u30e9\u30d9\u30eb\u3092\u65e5\u672c\u8a9e\u3067\u8a2d\u5b9a\r\nplt.title('\u98db\u884c\u6a5f\u306e\u4e57\u5ba2\u6570\u63a8\u79fb') # \u30b0\u30e9\u30d5\u306e\u30bf\u30a4\u30c8\u30eb\u3092\u8a2d\u5b9a\r\nplt.xlabel('\u65e5\u4ed8') # \u6a2a\u8ef8\uff08X\u8ef8\uff09\u306e\u30e9\u30d9\u30eb\u3092\u300c\u65e5\u4ed8\u300d\u3068\u3059\u308b\r\nplt.ylabel('\u5ba2\u6570') # \u7e26\u8ef8\uff08Y\u8ef8\uff09\u306e\u30e9\u30d9\u30eb\u3092\u300c\u5024\u300d\u3068\u3059\u308b\r\n\r\n# \u30b0\u30e9\u30d5\u3092\u8868\u793a\r\nplt.show()<\/code><\/pre>\r\n<p>\u4eca\u56de\u306f\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u3067\u3042\u308b\u4e57\u5ba2\u6570\u63a8\u79fb\u3067\u7df4\u7fd2\u3057\u307e\u3059\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<\/div>\n\n<h2>\u30c7\u30fc\u30bf\u306e\u6982\u8981<\/h2>\n<p>\u30c7\u30fc\u30bf\u306e\u6982\u8981\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n<h2>STL\u5206\u89e3\u3092\u3057\u3066\u307f\u308b<\/h2>\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import pandas as pd\r\nfrom statsmodels.tsa.seasonal import STL\r\n\r\n# STL\u5206\u89e3\u3092\u884c\u3046\r\nstl = STL(df['Passengers'], seasonal=13)\r\nresult = stl.fit()\r\n\r\n# \u5206\u89e3\u3055\u308c\u305f\u6210\u5206\u3092\u30d7\u30ed\u30c3\u30c8\u3059\u308b\r\nseasonal, trend, resid = result.seasonal, result.trend, result.resid\r\n\r\n\r\nplt.figure(figsize=(8, 6))\r\n\r\n# \u30c8\u30ec\u30f3\u30c9\u6210\u5206\r\nplt.subplot(311)\r\nplt.plot(trend)\r\nplt.title('\u30c8\u30ec\u30f3\u30c9\u6210\u5206')\r\n\r\n# \u5b63\u7bc0\u6210\u5206\r\nplt.subplot(312)\r\nplt.plot(seasonal)\r\nplt.title('\u5b63\u7bc0\u6210\u5206')\r\n\r\n# \u6b8b\u5dee\u6210\u5206\r\nplt.subplot(313)\r\nplt.plot(resid)\r\nplt.title('\u6b8b\u5dee\u6210\u5206')\r\n\r\nplt.tight_layout()\r\nplt.show()<\/code><\/pre>\r\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u3067STL\u5206\u89e3\u3092\u884c\u3044\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8003\u5bdf\u3057\u307e\u3057\u305f\u3002<\/p>\r\n<\/div>\n\n<h2>\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u3066\u307f\u308b<\/h2>\n<p>\u81ea\u5df1\u76f8\u95a2\u3068\u504f\u81ea\u5df1\u76f8\u95a2\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>plot_acf(df.Passengers, lags=20)\r\nplot_pacf(df.Passengers, lags=20)<\/code><\/pre>\r\n<\/div>\n\n<h2>\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u8003\u5bdf<\/h2>\n<p>\u4e0a\u8a18\u306e\u51fa\u529b\u7d50\u679c\u304b\u3089\u3053\u306e\u3088\u3046\u306b\u8003\u5bdf\u3057\u307e\u3057\u305f\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u8003\u5bdf.png\" alt=\"\" width=\"1222\" height=\"572\" class=\"alignnone size-full wp-image-4407\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u8003\u5bdf.png 1222w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u8003\u5bdf-300x140.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u8003\u5bdf-1024x479.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/\u30b3\u30ec\u30ed\u30b0\u30e9\u30e0\u8003\u5bdf-768x359.png 768w\" sizes=\"(max-width: 1222px) 100vw, 1222px\" \/><\/p>\n\n<h2>ARIMA\u30e2\u30c7\u30eb\u3067\u5b66\u7fd2\u3057\u3066\u307f\u308b<\/h2>\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sklearn.metrics import mean_squared_error,mean_absolute_error,mean_absolute_percentage_error\r\n\r\ndf_train, df_test = model_selection.train_test_split(df, test_size=12)\r\n\r\narima_model = pm.auto_arima(df_train,\r\nseasonal=False,\r\nm=12,\r\ntrace=True,\r\nn_jobs=-1,\r\nmaxiter=10)\r\n\r\n# \u4e88\u6e2c\r\n##\u5b66\u7fd2\u30c7\u30fc\u30bf\u306e\u671f\u9593\u306e\u4e88\u6e2c\u5024\r\ntrain_pred = arima_model.predict_in_sample()\r\n##\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u671f\u9593\u306e\u4e88\u6e2c\u5024\r\ntest_pred, test_pred_ci = arima_model.predict(\r\nn_periods=df_test.shape[0],\r\nreturn_conf_int=True\r\n)\r\n# \u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3067\u7cbe\u5ea6\u691c\u8a3c\r\nprint('RMSE:')\r\nprint(np.sqrt(mean_squared_error(df_test, test_pred)))\r\nprint('MAE:')\r\nprint(mean_absolute_error(df_test, test_pred))\r\nprint('MAPE:')\r\nprint(mean_absolute_percentage_error(df_test, test_pred))\r\n\r\n# \u30b0\u30e9\u30d5\u5316\r\nfig, ax = plt.subplots()\r\nax.plot(df_train[24:].index, df_train[24:].values, label=\"actual(train dataset)\")\r\nax.plot(df_test.index, df_test.values, label=\"actual(test dataset)\", color=\"gray\")\r\nax.plot(df_train[24:].index, train_pred[24:], color=\"c\")\r\nax.plot(df_test.index, test_pred, label=\"auto ARIMA\", color=\"c\")\r\nax.fill_between(\r\ndf_test.index,\r\ntest_pred_ci[:, 0],\r\ntest_pred_ci[:, 1],\r\ncolor='c',\r\nalpha=.2)\r\nax.legend()<\/code><\/pre>\r\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308c\u3070\u3053\u306e\u3088\u3046\u306a\u5b9f\u884c\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-92.png\" alt=\"\" width=\"1559\" height=\"570\" class=\"alignnone size-full wp-image-4400\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-92.png 1559w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-92-300x110.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-92-1024x374.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-92-768x281.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-92-1536x562.png 1536w\" sizes=\"(max-width: 1559px) 100vw, 1559px\" \/><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-93.png\" alt=\"\" width=\"605\" height=\"449\" class=\"alignnone size-full wp-image-4401\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-93.png 605w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-93-300x223.png 300w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/p>\r\n<p>\u6700\u5f8c\u306b\u6b8b\u5dee\u3092\u30d7\u30ed\u30c3\u30c8\u3057\u3066\u4e88\u6e2c\u7cbe\u5ea6\u3068\u30e2\u30c7\u30eb\u306e\u9069\u5408\u5177\u5408\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-94.png\" alt=\"\" width=\"616\" height=\"469\" class=\"alignnone size-full wp-image-4403\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-94.png 616w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/Untitled-94-300x228.png 300w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u3053\u3053\u304b\u3089\u6b8b\u5dee\u306e\u5206\u5e03\u3092\u63cf\u753b\u3057\u307e\u3059\u3002\r\nresiduals = df_test.values.flatten() - test_pred\r\nplt.figure()\r\nplt.hist(residuals, bins=6, alpha=0.7, color='blue')\r\nplt.xlabel('\u6b8b\u5dee')\r\nplt.ylabel('\u983b\u5ea6')\r\nplt.show()<\/code><\/pre>\r\n<p>\u30e2\u30c7\u30eb\u306e\u30b5\u30de\u30ea\u30fc\u306f\u3053\u3061\u3089\u304b\u3089\u8868\u793a\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<p>\u203b\u3053\u3061\u3089\u306e\u753b\u50cf\u306fSARIMAX\u30e2\u30c7\u30eb\u3067\u5b66\u7fd2\u3057\u305f\u6642\u306e\u753b\u50cf\u3067\u3059\u3002<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># \u30e2\u30c7\u30eb\u306e\u30b5\u30de\u30ea\u30fc\u3092\u8868\u793a\u3059\u308b\r\nprint(arima_model.summary())<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n\n<h2>ARIMA\u30e2\u30c7\u30eb\u3067\u5b66\u7fd2\u3057\u305f\u7d50\u679c<\/h2>\n<p>ARIMA\u30e2\u30c7\u30eb\u3067\u5b66\u7fd2\u3057\u305f\u7d50\u679c\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u307e\u3068\u3081\u307e\u3057\u305f\u3002<\/p>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/model\u8003\u5bdf.png\" alt=\"\" width=\"1237\" height=\"602\" class=\"alignnone size-full wp-image-4408\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/model\u8003\u5bdf.png 1237w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/model\u8003\u5bdf-300x146.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/model\u8003\u5bdf-1024x498.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/01\/model\u8003\u5bdf-768x374.png 768w\" sizes=\"(max-width: 1237px) 100vw, 1237px\" \/><\/p>","protected":false},"excerpt":{"rendered":"<p>ARIMA\u30e2\u30c7\u30eb\u3067\u6642\u7cfb\u5217\u4e88\u6e2c\u3092\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002 \u4e00\u822c\u7684\u306a\u6642\u7cfb\u5217\u30e2\u30c7\u30eb\u3067\u4e88\u6e2c\u3057\u3066\u307f\u308b \u738b\u9053\u7684\u306a\u30e2\u30c7\u30eb\u3068\u3057\u3066ARIMA\u30e2\u30c7\u30eb\u3084SARIMAX\u30e2\u30c7\u30eb\u3001Prophet\u306a\u3069\u6642\u7cfb\u5217\u30e2\u30c7\u30eb\u3067\u3042\u308a\u307e\u3059\u3088\u306d\u3002\u4eca\u56de\u306fARIMA\u30e2\u30c7\u30eb\u3067\u4e88\u6e2c\u304b [&hellip;]<\/p>\n","protected":false},"author":83,"featured_media":3651,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1246],"tags":[],"class_list":["post-4389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-infrastructure"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/83"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=4389"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/4389\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/3651"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=4389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=4389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=4389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}