{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7391,"databundleVersionId":44328,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.metrics import mean_absolute_percentage_error\nfrom statsmodels.tsa.stattools import adfuller\nfrom statsmodels.graphics.tsaplots import plot_pacf\nfrom statsmodels.graphics.tsaplots import plot_acf\n!pip install pyunpack\n!pip install patool\nos.system('apt-get install p7zip')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-04T08:48:31.479604Z","iopub.execute_input":"2024-07-04T08:48:31.480173Z","iopub.status.idle":"2024-07-04T08:49:08.752362Z","shell.execute_reply.started":"2024-07-04T08:48:31.480116Z","shell.execute_reply":"2024-07-04T08:49:08.750743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"创建文件夹","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input/'):\n    for filename in filenames:\n        if not os.path.exists('/kaggle/working/'+filename.split(\".\")[0]+\"/\"):\n            os.makedirs('/kaggle/working/'+filename.split(\".\")[0]+\"/\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:49:08.755693Z","iopub.execute_input":"2024-07-04T08:49:08.757368Z","iopub.status.idle":"2024-07-04T08:49:08.767609Z","shell.execute_reply.started":"2024-07-04T08:49:08.757308Z","shell.execute_reply":"2024-07-04T08:49:08.766370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"解压缩数据","metadata":{}},{"cell_type":"code","source":"from pyunpack import Archive\nimport shutil\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/train.csv.7z\").extractall('/kaggle/working/')\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/test.csv.7z\").extractall('/kaggle/working/')\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/oil.csv.7z\").extractall('/kaggle/working/')\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/stores.csv.7z\").extractall('/kaggle/working/')\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/items.csv.7z\").extractall('/kaggle/working/')\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/transactions.csv.7z\").extractall('/kaggle/working/')\nArchive(\"/kaggle/input/favorita-grocery-sales-forecasting/holidays_events.csv.7z\").extractall('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:49:08.769541Z","iopub.execute_input":"2024-07-04T08:49:08.770000Z","iopub.status.idle":"2024-07-04T08:50:21.844639Z","shell.execute_reply.started":"2024-07-04T08:49:08.769959Z","shell.execute_reply":"2024-07-04T08:50:21.843409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"数据读取","metadata":{}},{"cell_type":"code","source":"import pandas as pd \nprint(\"data_load\")\ndtypes = {'id':'uint32', 'item_nbr':'int32', 'store_nbr':'int8', 'onpromotion':str}\ndf_holevents = pd.read_csv('../working/holidays_events.csv')\nprint(\"df_holevents ok\")\ndf_items     = pd.read_csv('../working/items.csv')\nprint(\"df_items ok\")\ndf_oil       = pd.read_csv('../working/oil.csv', parse_dates=['date'])\nprint(\"df_oil ok\")\ndf_stores    = pd.read_csv('../working/stores.csv')\nprint(\"df_stores ok \")\ndf_test      = pd.read_csv('../working/test.csv', dtype=dtypes, parse_dates=['date'])\nprint(\"df_test ok \")\ndf_trans     = pd.read_csv('../working/transactions.csv', parse_dates=['date'])\nprint(\"df_trans ok \")\ndf_train     = pd.read_csv('../working/train.csv')\nprint(\"df_train ok \")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:50:21.846682Z","iopub.execute_input":"2024-07-04T08:50:21.847587Z","iopub.status.idle":"2024-07-04T08:52:43.948526Z","shell.execute_reply.started":"2024-07-04T08:50:21.847534Z","shell.execute_reply":"2024-07-04T08:52:43.946992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"数据分析（显示基本信息）","metadata":{}},{"cell_type":"code","source":"print(\"df_train的大小维度\",df_train.shape)\nprint(\"df_train的前5条记录\")\nprint(df_train.head())\nprint(\"df_train基本信息\")\nprint(df_train.info())\nprint(\"显示df_train的相关最大值\")\nprint(df_train.max())\nprint(\"显示df_train的相关最小值\")\nprint(df_train.min())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:52:43.951996Z","iopub.execute_input":"2024-07-04T08:52:43.952426Z","iopub.status.idle":"2024-07-04T08:54:02.984085Z","shell.execute_reply.started":"2024-07-04T08:52:43.952389Z","shell.execute_reply":"2024-07-04T08:54:02.982780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"df_test的大小维度\",df_test.shape)\nprint(\"df_test的前5条记录\")\nprint(df_test.head())\nprint(\"df_test基本信息\")\nprint(df_test.info())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:02.985841Z","iopub.execute_input":"2024-07-04T08:54:02.986303Z","iopub.status.idle":"2024-07-04T08:54:03.003004Z","shell.execute_reply.started":"2024-07-04T08:54:02.986266Z","shell.execute_reply":"2024-07-04T08:54:03.001648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"df_stores的大小维度\",df_stores.shape)\nprint(\"df_stores的前5条记录\")\nprint(df_stores.head())\nprint(\"df_stores基本信息\")\nprint(df_stores.info())\nprint(\"显示df_stores的相关最大值\")\nprint(df_stores.max())\nprint(\"显示df_stores的相关最小值\")\nprint(df_stores.min())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:03.004525Z","iopub.execute_input":"2024-07-04T08:54:03.004967Z","iopub.status.idle":"2024-07-04T08:54:03.031261Z","shell.execute_reply.started":"2024-07-04T08:54:03.004930Z","shell.execute_reply":"2024-07-04T08:54:03.029875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"df_items的大小维度\",df_items.shape)\nprint(\"df_items的前5条记录\")\nprint(df_items.head())\nprint(\"df_items基本信息\")\nprint(df_items.info())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:03.032965Z","iopub.execute_input":"2024-07-04T08:54:03.033419Z","iopub.status.idle":"2024-07-04T08:54:03.060223Z","shell.execute_reply.started":"2024-07-04T08:54:03.033384Z","shell.execute_reply":"2024-07-04T08:54:03.058932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"df_oil的大小维度\",df_oil.shape)\nprint(\"df_oil的前5条记录\")\nprint(df_oil.head())\nprint(\"df_oil的基本信息\")\nprint(df_oil.info())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:03.061897Z","iopub.execute_input":"2024-07-04T08:54:03.062302Z","iopub.status.idle":"2024-07-04T08:54:03.084833Z","shell.execute_reply.started":"2024-07-04T08:54:03.062257Z","shell.execute_reply":"2024-07-04T08:54:03.083282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"df_trans的大小维度\",df_trans.shape)\nprint(\"df_trans的前5条记录\")\nprint(df_trans.head())\nprint(\"df_trans的基本信息\")\nprint(df_trans.info())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:03.086672Z","iopub.execute_input":"2024-07-04T08:54:03.087171Z","iopub.status.idle":"2024-07-04T08:54:03.107172Z","shell.execute_reply.started":"2024-07-04T08:54:03.087124Z","shell.execute_reply":"2024-07-04T08:54:03.105783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"df_holevent的大小维度\",df_holevents.shape)\nprint(\"df_holevents的前5条记录\")\nprint(df_holevents.head())\nprint(\"df_holevents的基本信息\")\nprint(df_holevents.info())","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:03.109313Z","iopub.execute_input":"2024-07-04T08:54:03.109783Z","iopub.status.idle":"2024-07-04T08:54:03.126757Z","shell.execute_reply.started":"2024-07-04T08:54:03.109741Z","shell.execute_reply":"2024-07-04T08:54:03.125375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"数据分析","metadata":{}},{"cell_type":"code","source":"#创建一个条形图，显示df_train数据集中每年收集的数据的数量。\ndf_train_copy = df_train.copy()\ndf_train_copy[\"date\"] = pd.to_datetime(df_train[\"date\"])\ndf_train_copy[\"date\"].dt.year.value_counts(sort = False).plot.bar()\nplt.xlabel('Year')\nplt.ylabel(\"Collected Data\")\nplt.savefig('year_data.png')","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:03.128246Z","iopub.execute_input":"2024-07-04T08:54:03.128660Z","iopub.status.idle":"2024-07-04T08:54:34.240083Z","shell.execute_reply.started":"2024-07-04T08:54:03.128627Z","shell.execute_reply":"2024-07-04T08:54:34.238916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n#对商店商品进行分析\ndf_stores['store_nbr'].unique()\n#找出df_stores数据集中state列的所有唯一值，即所有的州名。\ndf_stores['state'].unique()\n#出df_train_2016数据集中item_nbr列的所有唯一值的数量，即所有的商品编号的数量。\ndf_train[\"item_nbr\"].unique().shape[0]\n#储存商店编号\nstores = np.arange(1, 55)\n#存储每个商店的商品数量。\nitems_store = np.zeros((54, ))\n#对于每个商店，计算该商店中售卖的商品种类数量，并存储在items_store数组中。\nfor i, store in enumerate(stores) :\n    items_store[i] = df_train[\"item_nbr\"][df_train[\"store_nbr\"] \\\n                                               == store].unique().shape[0]\n#创建一个条形图，显示每个商店的商品种类数量。\nsns.barplot(x=stores, y=items_store)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:34.241528Z","iopub.execute_input":"2024-07-04T08:54:34.241921Z","iopub.status.idle":"2024-07-04T08:54:45.710061Z","shell.execute_reply.started":"2024-07-04T08:54:34.241879Z","shell.execute_reply":"2024-07-04T08:54:45.708778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"数据选取2015到2017","metadata":{}},{"cell_type":"code","source":"#日期格式进行转换\ndf_train[\"date\"] =  pd.to_datetime(df_train[\"date\"])\n#选取2015年的数据\ndf_train_2015 = df_train[df_train[\"date\"].dt.year == 2015].copy()\n#格式转换\ndf_train_2015['id'] = df_train_2015['id'].astype('uint32')\ndf_train_2015['item_nbr'] = df_train_2015['item_nbr'].astype('uint32')\ndf_train_2015['store_nbr'] = df_train_2015['store_nbr'].astype('int8')\ndf_train_2015['onpromotion'] = df_train_2015['onpromotion'].astype('str')\n\nprint(\"df_train_2015 ok\")\n\n#选取2016年的数据\ndf_train_2016 = df_train[df_train[\"date\"].dt.year == 2016].copy()\n#格式转换\ndf_train_2016['id'] = df_train_2016['id'].astype('uint32')\ndf_train_2016['item_nbr'] = df_train_2016['item_nbr'].astype('uint32')\ndf_train_2016['store_nbr'] = df_train_2016['store_nbr'].astype('int8')\ndf_train_2016['onpromotion'] = df_train_2016['onpromotion'].astype('str')\n\nprint(\"df_train_2016 ok\")\n\ndf_train_2017 = df_train[df_train[\"date\"].dt.year == 2017].copy()\n#格式转换\ndf_train_2017['id'] = df_train_2017['id'].astype('uint32')\ndf_train_2017['item_nbr'] = df_train_2017['item_nbr'].astype('uint32')\ndf_train_2017['store_nbr'] = df_train_2017['store_nbr'].astype('int8')\ndf_train_2017['onpromotion'] = df_train_2017['onpromotion'].astype('str')\nprint(\"df_train_2017 ok\")","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:54:45.715187Z","iopub.execute_input":"2024-07-04T08:54:45.715632Z","iopub.status.idle":"2024-07-04T08:56:10.511769Z","shell.execute_reply.started":"2024-07-04T08:54:45.715589Z","shell.execute_reply":"2024-07-04T08:56:10.510220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"以2016年数据为例，对2016年数据进行可视化","metadata":{}},{"cell_type":"code","source":"#用于显示数据集df_train_2016中日期的月份和日期的天数的分布情况\ndf_train_2016[\"date\"].dt.month.value_counts(sort = False).plot.bar()\nplt.savefig('2016_month_data.png')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:56:10.516327Z","iopub.execute_input":"2024-07-04T08:56:10.518143Z","iopub.status.idle":"2024-07-04T08:56:13.451556Z","shell.execute_reply.started":"2024-07-04T08:56:10.518030Z","shell.execute_reply":"2024-07-04T08:56:13.447235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_2016[\"date\"].dt.day.value_counts(sort = False).plot.bar()\nplt.savefig('2016_day_data.png')","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:56:13.456457Z","iopub.execute_input":"2024-07-04T08:56:13.457666Z","iopub.status.idle":"2024-07-04T08:56:19.258581Z","shell.execute_reply.started":"2024-07-04T08:56:13.457510Z","shell.execute_reply":"2024-07-04T08:56:19.256736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_2016[\"unit_sales\"].describe()","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:56:19.261196Z","iopub.execute_input":"2024-07-04T08:56:19.261930Z","iopub.status.idle":"2024-07-04T08:56:21.315366Z","shell.execute_reply.started":"2024-07-04T08:56:19.261856Z","shell.execute_reply":"2024-07-04T08:56:21.314152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"MA模型，特征提取分析（分别计算销量前10，20，30）","metadata":{}},{"cell_type":"code","source":"print(\"generate top\")\n# train = df_train_2016\ntrain = pd.concat([df_train_2015,df_train_2016, df_train_2017])\n#分别计算销量前10，20，30\ntop_ten_selling_products = train.groupby(['item_nbr']).count()['id'].sort_values(ascending=False).head(10)\ntop_twenty_selling_products = train.groupby(['item_nbr']).count()['id'].sort_values(ascending=False).head(20)\ntop_Thirty_selling_products = train.groupby(['item_nbr']).count()['id'].sort_values(ascending=False).head(30)\n\nplt.figure(figsize=(15,6))  # 创建新的图形\nfor item_nbr in top_ten_selling_products.index:\n    top1 = df_train[df_train.item_nbr == item_nbr]\n    top1.groupby(['date']).sum()['unit_sales'].rolling(window=7).mean().plot(x='date',y='unit_sales',rot=0, label=\"Item \" + str(item_nbr))\nplt.ylabel(\"Total Unit Sold\")\nplt.xlabel(\"Day\")\nplt.title(\"Moving Average Forecast for Top 10 Items\")\nplt.legend(loc=\"upper right\")\nplt.savefig('predicted_vs_actual_test_top10.png')\n\nplt.figure(figsize=(15,6))  # 创建新的图形\nfor item_nbr in top_twenty_selling_products.index:\n    top2 = df_train[df_train.item_nbr == item_nbr]\n    top2.groupby(['date']).sum()['unit_sales'].rolling(window=7).mean().plot(x='date',y='unit_sales',rot=0, label=\"Item \" + str(item_nbr))\nplt.ylabel(\"Total Unit Sold\")\nplt.xlabel(\"Day\")\nplt.title(\"Moving Average Forecast for Top 20 Items\")\nplt.legend(loc=\"upper right\")\nplt.savefig('predicted_vs_actual_test_top20.png')\n\nplt.figure(figsize=(15,6))  # 创建新的图形\nfor item_nbr in top_Thirty_selling_products.index:\n    top3 = df_train[df_train.item_nbr == item_nbr]\n    top3.groupby(['date']).sum()['unit_sales'].rolling(window=7).mean().plot(x='date',y='unit_sales',rot=0, label=\"Item \" + str(item_nbr))\nplt.ylabel(\"Total Unit Sold\")\nplt.xlabel(\"Day\")\nplt.title(\"Moving Average Forecast for Top 30 Items\")\nplt.legend(loc=\"upper right\")\nplt.savefig('predicted_vs_actual_test_top30.png')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:56:21.316914Z","iopub.execute_input":"2024-07-04T08:56:21.317299Z","iopub.status.idle":"2024-07-04T08:57:24.663878Z","shell.execute_reply.started":"2024-07-04T08:56:21.317269Z","shell.execute_reply":"2024-07-04T08:57:24.662608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"数据检查（这段代码的主要目标是对时间序列数据进行检查，以确定其是否为平稳，并通过自相关函数（ACF）和偏自相关函数（PACF）来决定ARMA模型的阶数）","metadata":{}},{"cell_type":"code","source":"print(\"top1 check\")\nfrom statsmodels.tsa.stattools import adfuller\nimport matplotlib.pyplot as plt\nfrom statsmodels.graphics.tsaplots import plot_pacf\nfrom statsmodels.graphics.tsaplots import plot_acf\nunit_sales_by_date_1 = top1.groupby('date').sum()['unit_sales']\nX_1 = unit_sales_by_date_1.values\nresult_1 = adfuller(X_1)\n#代码使用 adfuller 函数进行Augmented Dickey-Fuller（ADF）检验，以确定时间序列是否为平稳\nprint('ADF Statistic: %f' % result_1[0])\nprint('p-value: %f' % result_1[1])\nprint('Critical Values:')\nfor key, value in result_1[4].items():\n    print('\\t%s: %.3f' % (key, value))\n\nif result_1[0] < result_1[4][\"5%\"]:\n    print (\"拒绝 Ho - 时间序列是静态的\")\nelse:\n    print (\"未能拒绝 Ho - 时间序列是非静态的\")\n\n#代码使用 plot_pacf 和 plot_acf 函数分别绘制了偏自相关函数（PACF）和自相关函数（ACF）的图表。\n#这两个图表可以帮助我们确定ARMA模型的阶数。\nplot_pacf(unit_sales_by_date_1)\nplot_acf(unit_sales_by_date_1, lags=7)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:24.665382Z","iopub.execute_input":"2024-07-04T08:57:24.665741Z","iopub.status.idle":"2024-07-04T08:57:25.770556Z","shell.execute_reply.started":"2024-07-04T08:57:24.665709Z","shell.execute_reply":"2024-07-04T08:57:25.769229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"top2 check\")\nfrom statsmodels.tsa.stattools import adfuller\nimport matplotlib.pyplot as plt\nfrom statsmodels.graphics.tsaplots import plot_pacf\nfrom statsmodels.graphics.tsaplots import plot_acf\nunit_sales_by_date_2 = top2.groupby('date').sum()['unit_sales']\nX_2 = unit_sales_by_date_2.values\nresult_2 = adfuller(X_2)\nprint('ADF Statistic: %f' % result_2[0])\nprint('p-value: %f' % result_2[1])\nprint('Critical Values:')\nfor key, value in result_2[4].items():\n    print('\\t%s: %.3f' % (key, value))\n\nif result_2[0] < result_2[4][\"5%\"]:\n    print (\"Reject Ho - Time Series is Stationary\")\nelse:\n    print (\"Failed to Reject Ho - Time Series is Non-Stationary\")\n\n#检查 ACF 和 PACF 以决定模型顺序\nplot_pacf(unit_sales_by_date_2)\nplot_acf(unit_sales_by_date_2, lags=7)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:25.772271Z","iopub.execute_input":"2024-07-04T08:57:25.772656Z","iopub.status.idle":"2024-07-04T08:57:26.837089Z","shell.execute_reply.started":"2024-07-04T08:57:25.772622Z","shell.execute_reply":"2024-07-04T08:57:26.835796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"top3 check\")\nfrom statsmodels.tsa.stattools import adfuller\nimport matplotlib.pyplot as plt\nfrom statsmodels.graphics.tsaplots import plot_pacf\nfrom statsmodels.graphics.tsaplots import plot_acf\nunit_sales_by_date_3 = top3.groupby('date').sum()['unit_sales']\nX_3 = unit_sales_by_date_3.values\nresult_3 = adfuller(X_3)\nprint('ADF Statistic: %f' % result_3[0])\nprint('p-value: %f' % result_3[1])\nprint('Critical Values:')\nfor key, value in result_3[4].items():\n    print('\\t%s: %.3f' % (key, value))\n\nif result_3[0] < result_3[4][\"5%\"]:\n    print (\"Reject Ho - Time Series is Stationary\")\nelse:\n    print (\"Failed to Reject Ho - Time Series is Non-Stationary\")\n\n#检查 ACF 和 PACF 以决定模型顺序\nplot_pacf(unit_sales_by_date_3)\nplot_acf(unit_sales_by_date_3, lags=7)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:26.838961Z","iopub.execute_input":"2024-07-04T08:57:26.840043Z","iopub.status.idle":"2024-07-04T08:57:27.919200Z","shell.execute_reply.started":"2024-07-04T08:57:26.839990Z","shell.execute_reply":"2024-07-04T08:57:27.917753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"移动平均模型（MA）","metadata":{}},{"cell_type":"code","source":"def moving_average_forecast(series, window_size):\n    \"\"\"Forecasts the mean of the last few values.\n     If window_size=1, then this is equivalent to naive forecast\"\"\"\n    forecast = []\n    for time in range(len(series) - window_size):\n        forecast.append(series[time:time + window_size].mean())\n    return np.array(forecast)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:27.921022Z","iopub.execute_input":"2024-07-04T08:57:27.921539Z","iopub.status.idle":"2024-07-04T08:57:27.929419Z","shell.execute_reply.started":"2024-07-04T08:57:27.921489Z","shell.execute_reply":"2024-07-04T08:57:27.927887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.metrics import mean_absolute_percentage_error\ndef optimize_moving_average_forecast(series, window_sizes):\n    best_score = float('inf')\n    best_window_size = None\n\n    # 对于每一个窗口大小，计算移动平均并评估预测效果\n    for window_size in window_sizes:\n        forecast = moving_average_forecast(series, window_size)\n        score = mean_squared_error(series[window_size:], forecast)\n\n        # 如果当前的预测效果比之前的好，那么更新最佳得分和最佳窗口大小\n        if score < best_score:\n            best_score = score\n            best_window_size = window_size\n\n    # 返回最佳窗口大小和最佳得分\n    return best_window_size, best_score\nwindow_sizes = [7, 14, 21, 28, 35, 42, 49]\n# 使用优化函数来找到最佳的窗口大小\nbest_window_size_1, best_score_1 = optimize_moving_average_forecast(unit_sales_by_date_1, window_sizes)\nbest_window_size_2, best_score_2 = optimize_moving_average_forecast(unit_sales_by_date_2, window_sizes)\nbest_window_size_3, best_score_3 = optimize_moving_average_forecast(unit_sales_by_date_3, window_sizes)\n\nprint('Best window size_1:', best_window_size_1)\nprint('Best score_1:', best_score_1)\nprint('Best window size_2:', best_window_size_2)\nprint('Best score_2:', best_score_2)\nprint('Best window size_3:', best_window_size_3)\nprint('Best score_3:', best_score_3)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:27.931092Z","iopub.execute_input":"2024-07-04T08:57:27.931469Z","iopub.status.idle":"2024-07-04T08:57:30.270842Z","shell.execute_reply.started":"2024-07-04T08:57:27.931436Z","shell.execute_reply":"2024-07-04T08:57:30.269556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"top1\")\nsplit_time = 1455\nmoving_average_days = 6\nmoving_avg_1 = moving_average_forecast(unit_sales_by_date_1,moving_average_days )# [split_time + 7:]\nmoving_avg_best_1 = moving_average_forecast(unit_sales_by_date_1,best_window_size_1 )# [split_time + 7:]\nprint(moving_avg_1.shape,unit_sales_by_date_1.shape)\nprint(moving_avg_best_1.shape,unit_sales_by_date_1.shape)\n\n\nprint(\"mean_squared_error\",mean_squared_error(unit_sales_by_date_1.values[moving_average_days:], moving_avg_1))\nprint(\"mean_absolute_error\",mean_absolute_error(unit_sales_by_date_1.values[moving_average_days:], moving_avg_1))\nprint(\"mean_absolute_percentage_error\",mean_absolute_percentage_error(unit_sales_by_date_1.values[moving_average_days:], moving_avg_1))\n\nprint(\"mean_squared_error_1\",mean_squared_error(unit_sales_by_date_1.values[best_window_size_1:], moving_avg_best_1))\nprint(\"mean_absolute_error_1\",mean_absolute_error(unit_sales_by_date_1.values[best_window_size_1:], moving_avg_best_1))\nprint(\"mean_absolute_percentage_error_1\",mean_absolute_percentage_error(unit_sales_by_date_1.values[best_window_size_1:], moving_avg_best_1))\n# Plot Moving average vs Actual\nplt.figure(figsize=(15,6))\n\nplt.plot(unit_sales_by_date_1.values[best_window_size_1:], label=\"Actual\")\nplt.plot(moving_avg_1, label=\"Moving average\")\nplt.plot(moving_avg_best_1, label=\"Optimized moving average\")\nplt.ylabel(\"Total Unit Sold\")\nplt.xlabel(\"Day\")\nplt.title(\"Moving Average Forecast\")\nplt.legend(loc=\"upper right\")\nplt.savefig('predicted_vs_actual_test.png')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:30.272349Z","iopub.execute_input":"2024-07-04T08:57:30.272727Z","iopub.status.idle":"2024-07-04T08:57:31.184135Z","shell.execute_reply.started":"2024-07-04T08:57:30.272694Z","shell.execute_reply":"2024-07-04T08:57:31.182406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"top2\")\nsplit_time = 1455\nmoving_average_days = 6\nmoving_avg_2 = moving_average_forecast(unit_sales_by_date_2,moving_average_days )# [split_time + 7:]\nmoving_avg_best_2 = moving_average_forecast(unit_sales_by_date_2,best_window_size_2 )# [split_time + 7:]\nprint(moving_avg_2.shape,unit_sales_by_date_2.shape)\nprint(moving_avg_best_2.shape,unit_sales_by_date_2.shape)\n\n\nprint(\"mean_squared_error\",mean_squared_error(unit_sales_by_date_2.values[moving_average_days:], moving_avg_2))\nprint(\"mean_absolute_error\",mean_absolute_error(unit_sales_by_date_2.values[moving_average_days:], moving_avg_2))\nprint(\"mean_absolute_percentage_error\",mean_absolute_percentage_error(unit_sales_by_date_2.values[moving_average_days:]\n                                                                      , moving_avg_2))\n\nprint(\"mean_squared_error_2\",mean_squared_error(unit_sales_by_date_2.values[best_window_size_2:], moving_avg_best_2))\nprint(\"mean_absolute_error_2\",mean_absolute_error(unit_sales_by_date_2.values[best_window_size_2:], moving_avg_best_2))\nprint(\"mean_absolute_percentage_error_2\",mean_absolute_percentage_error(unit_sales_by_date_2.values[best_window_size_2:]\n                                                                        , moving_avg_best_2))\n# Plot Moving average vs Actual\nplt.figure(figsize=(15,6))\n\nplt.plot(unit_sales_by_date_2.values[best_window_size_2:], label=\"Actual\")\nplt.plot(moving_avg_2, label=\"Moving average\")\nplt.plot(moving_avg_best_2, label=\"Optimized moving average\")\nplt.ylabel(\"Total Unit Sold\")\nplt.xlabel(\"Day\")\nplt.title(\"Moving Average Forecast\")\nplt.legend(loc=\"upper right\")\nplt.savefig('predicted_vs_actual_test.png')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:31.185684Z","iopub.execute_input":"2024-07-04T08:57:31.186088Z","iopub.status.idle":"2024-07-04T08:57:32.125726Z","shell.execute_reply.started":"2024-07-04T08:57:31.186053Z","shell.execute_reply":"2024-07-04T08:57:32.124165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"top3\")\nsplit_time = 1455\nmoving_average_days = 6\nmoving_avg_3 = moving_average_forecast(unit_sales_by_date_3,moving_average_days )# [split_time + 7:]\nmoving_avg_best_3 = moving_average_forecast(unit_sales_by_date_3,best_window_size_3 )# [split_time + 7:]\nprint(moving_avg_3.shape,unit_sales_by_date_3.shape)\nprint(moving_avg_best_3.shape,unit_sales_by_date_3.shape)\n\n\nprint(\"mean_squared_error\",mean_squared_error(unit_sales_by_date_3.values[moving_average_days:]\n                                              , moving_avg_3))\nprint(\"mean_absolute_error\",mean_absolute_error(unit_sales_by_date_3.values[moving_average_days:], moving_avg_3))\nprint(\"mean_absolute_percentage_error\",mean_absolute_percentage_error(unit_sales_by_date_3.values[moving_average_days:]\n                                                                      , moving_avg_3))\n\nprint(\"mean_squared_error_3\",mean_squared_error(unit_sales_by_date_3.values[best_window_size_3:], moving_avg_best_3))\nprint(\"mean_absolute_error_3\",mean_absolute_error(unit_sales_by_date_3.values[best_window_size_3:], moving_avg_best_3))\nprint(\"mean_absolute_percentage_error_3\",mean_absolute_percentage_error(unit_sales_by_date_3.values[best_window_size_3:]\n                                                                        , moving_avg_best_3))\n# Plot Moving average vs Actual\nplt.figure(figsize=(15,6))\n\nplt.plot(unit_sales_by_date_3.values[best_window_size_3:], label=\"Actual\")\nplt.plot(moving_avg_3, label=\"Moving average\")\nplt.plot(moving_avg_best_3, label=\"Optimized moving average\")\nplt.ylabel(\"Total Unit Sold\")\nplt.xlabel(\"Day\")\nplt.title(\"Moving Average Forecast\")\nplt.legend(loc=\"upper right\")\nplt.savefig('predicted_vs_actual_test.png')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:32.127760Z","iopub.execute_input":"2024-07-04T08:57:32.128315Z","iopub.status.idle":"2024-07-04T08:57:33.011738Z","shell.execute_reply.started":"2024-07-04T08:57:32.128266Z","shell.execute_reply":"2024-07-04T08:57:33.010085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"数据测试集预测","metadata":{}},{"cell_type":"code","source":"# #使用训练集中的最后best_window_size个销售数据来计算移动平均，然后将这个值作为对测试集中第一天的预测值。\n# #然后，你可以将这个预测值添加到历史销售数据中，然后再次计算移动平均，得到对第二天的预测值，以此类推\n# # 假设 df_test 是你的测试集\n# for item_nbr in top_ten_selling_products.index:\n#     # 获取训练集中该商品的历史销售数据\n#     train_data = df_train[df_train.item_nbr == item_nbr]\n#     unit_sales_by_date_train = train_data.groupby('date').sum()['unit_sales']\n\n#     # 获取测试集中该商品的数据\n#     test_data = df_test[df_test.item_nbr == item_nbr]\n\n#     # 初始化一个列表来存储预测值\n#     predictions = []\n\n# # 对于测试集中的每一天\n# for i in range(len(test_data)):\n#     # 使用训练集的最后几天的数据和已经预测的数据来计算移动平均\n#     if len(predictions) > 0:\n#         history = pd.concat([unit_sales_by_date_train, pd.Series(predictions)])\n#     else:\n#         history = unit_sales_by_date_train\n#     moving_avg_test = moving_average_forecast(history, best_window_size_1)\n\n#     # 将最后一个移动平均值作为预测值\n#     predictions.append(moving_avg_test[-1])\n\n#     # 绘制预测结果\n#     plt.figure(figsize=(15,6))  # 创建新的图形\n#     plt.plot(predictions, label=\"Moving average\")\n#     plt.ylabel(\"Total Unit Sold\")\n#     plt.xlabel(\"Day\")\n#     plt.title(\"Moving Average Forecast for item \" + str(item_nbr))\n#     plt.legend(loc=\"upper right\")\n#     plt.savefig('predicted_vs_actual_test_' + str(item_nbr) + '.png')","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:33.013742Z","iopub.execute_input":"2024-07-04T08:57:33.014304Z","iopub.status.idle":"2024-07-04T08:57:33.023719Z","shell.execute_reply.started":"2024-07-04T08:57:33.014256Z","shell.execute_reply":"2024-07-04T08:57:33.021965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AR Model","metadata":{}},{"cell_type":"markdown","source":"测试集和训练集划分（在原有的数据集中进行）","metadata":{}},{"cell_type":"code","source":"unit_sales_by_date = top1.groupby('date').sum()['unit_sales']\nx_train = unit_sales_by_date['2013':'2015'].values\nx_test  = unit_sales_by_date['2016'].values","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:33.025993Z","iopub.execute_input":"2024-07-04T08:57:33.026487Z","iopub.status.idle":"2024-07-04T08:57:33.083360Z","shell.execute_reply.started":"2024-07-04T08:57:33.026449Z","shell.execute_reply":"2024-07-04T08:57:33.081825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame()\n\ndf[\"Original Values\"]  = unit_sales_by_date\ndf[\"shift1\"] = df[\"Original Values\"].shift()\ndf[\"shift2\"] = df[\"shift1\"].shift()\ndf[\"shift3\"] = df[\"shift2\"].shift()\ndf[\"shift4\"] = df[\"shift3\"].shift()\ndf[\"shift5\"] = df[\"shift4\"].shift()\ndf[\"shift6\"] = df[\"shift5\"].shift()\n#df[\"shift7\"] = df[\"shift6\"].shift()\n#df[\"shift8\"] = df[\"shift7\"].shift()\nlag_value = 6\ndf.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:33.085091Z","iopub.execute_input":"2024-07-04T08:57:33.085488Z","iopub.status.idle":"2024-07-04T08:57:33.106766Z","shell.execute_reply.started":"2024-07-04T08:57:33.085453Z","shell.execute_reply":"2024-07-04T08:57:33.105475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.index = pd.to_datetime(df.index, format='%d-%m-%Y')\nprint(df.index)\nx_train, y_train = df['2013':'2016'].iloc[:,0:lag_value].values, df['2013':'2016'].iloc[:,lag_value:].values\nx_test, y_test = df['2017-01-01':'2017-12-31'].iloc[:,0:lag_value].values, df['2017-01-01':'2017-12-31'].iloc[:,lag_value:].values","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:33.108516Z","iopub.execute_input":"2024-07-04T08:57:33.109145Z","iopub.status.idle":"2024-07-04T08:57:33.134488Z","shell.execute_reply.started":"2024-07-04T08:57:33.109097Z","shell.execute_reply":"2024-07-04T08:57:33.132936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AR Model模型构造","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nreg = LinearRegression().fit(x_train, y_train)\nprint(reg.coef_)\nprint(reg.intercept_)\n\n\nar_predictions = reg.predict(x_test)\n\nplt.figure(figsize=(15,6))\nplt.plot(ar_predictions ,label = \"Predictions\")\nplt.plot(y_test, label = \"Original\" )\nplt.xlabel(\"Days\")\nplt.ylabel(\"Sale Count\")\n\nprint(\"mean_squared_error\",mean_squared_error(y_test, ar_predictions))\nprint(\"mean_absolute_error\",mean_absolute_error(y_test, ar_predictions))\nprint(\"mean_absolute_percentage_error\",mean_absolute_percentage_error(y_test, ar_predictions))\n\n#ar_predictions = reg.predict(x_train)\n#plt.figure(figsize=(15,6))\n#plt.plot(ar_predictions,label = \"Predictions\")\n#plt.plot(y_test, label = \"Y_train\")\nplt.legend(loc=\"upper right\")\n#print(\"mean_squared_error\",mean_squared_error(y_test, ar_predictions))\n#print(\"mean_absolute_error\",mean_absolute_error(y_test, ar_predictions))\n#print(\"mean_absolute_percentage_error\",mean_absolute_percentage_error(y_test, ar_predictions))","metadata":{"execution":{"iopub.status.busy":"2024-07-04T08:57:33.136317Z","iopub.execute_input":"2024-07-04T08:57:33.137071Z","iopub.status.idle":"2024-07-04T08:57:33.674241Z","shell.execute_reply.started":"2024-07-04T08:57:33.137023Z","shell.execute_reply":"2024-07-04T08:57:33.672785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ARIMA model构造","metadata":{}},{"cell_type":"code","source":"import statsmodels.api as sm\nfrom statsmodels.tsa.arima.model import ARIMA\nfrom datetime import datetime, timedelta\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error\n\n# 使用ARIMA模型进行拟合\nARIMA_model = ARIMA(x_train.reshape(-1).tolist(), order=(6,2,6)).fit()\n\n# 进行预测\narima_predictions = ARIMA_model.predict(start=len(x_train), end=len(x_train) + len(x_test)-1, dynamic=False)\n\n# 绘制预测结果和原始数据\nplt.figure(figsize=(15,6))\nplt.plot(arima_predictions ,label = \"Predictions\")\nplt.plot(y_test, label = \"Original\" )\nplt.xlabel(\"Days\")\nplt.ylabel(\"Sale Count\")\nplt.legend(loc=\"upper right\")\n\n# 计算误差\nprint(\"mean_squared_error\", mean_squared_error(y_test, arima_predictions))\nprint(\"mean_absolute_error\", mean_absolute_error(y_test, arima_predictions))\n# print(\"mean_absolute_percentage_error\", mean_absolute_percentage_error(y_test, arima_predictions)) # 如果你有这个函数的定义","metadata":{"execution":{"iopub.status.busy":"2024-07-04T09:20:56.245589Z","iopub.execute_input":"2024-07-04T09:20:56.248481Z","iopub.status.idle":"2024-07-04T09:21:40.926751Z","shell.execute_reply.started":"2024-07-04T09:20:56.248381Z","shell.execute_reply":"2024-07-04T09:21:40.925417Z"},"trusted":true},"execution_count":null,"outputs":[]}]}