{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:43.226050Z","iopub.execute_input":"2022-07-30T17:02:43.226475Z","iopub.status.idle":"2022-07-30T17:02:43.257053Z","shell.execute_reply.started":"2022-07-30T17:02:43.226386Z","shell.execute_reply":"2022-07-30T17:02:43.256174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 訓練データの読み込み\nStoreSalesデータの訓練用サブセットを読み込む","metadata":{"editable":false}},{"cell_type":"code","source":"oil = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/oil.csv\")\nholidays = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/holidays_events.csv\")\nstores = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/stores.csv\")\ntransactions = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/transactions.csv\")\ntrain = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/test.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/sample_submission.csv\")","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:43.258634Z","iopub.execute_input":"2022-07-30T17:02:43.259222Z","iopub.status.idle":"2022-07-30T17:02:46.681911Z","shell.execute_reply.started":"2022-07-30T17:02:43.259184Z","shell.execute_reply":"2022-07-30T17:02:46.680688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 個々のcsvデータを参照する","metadata":{}},{"cell_type":"markdown","source":"trainデータの場合","metadata":{}},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:46.683545Z","iopub.execute_input":"2022-07-30T17:02:46.683829Z","iopub.status.idle":"2022-07-30T17:02:46.706512Z","shell.execute_reply.started":"2022-07-30T17:02:46.683803Z","shell.execute_reply":"2022-07-30T17:02:46.705465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"testデータの場合","metadata":{}},{"cell_type":"code","source":"test.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:46.707837Z","iopub.execute_input":"2022-07-30T17:02:46.708663Z","iopub.status.idle":"2022-07-30T17:02:46.720657Z","shell.execute_reply.started":"2022-07-30T17:02:46.708630Z","shell.execute_reply":"2022-07-30T17:02:46.719500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"oilデータの場合","metadata":{}},{"cell_type":"code","source":"oil.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:46.725042Z","iopub.execute_input":"2022-07-30T17:02:46.725435Z","iopub.status.idle":"2022-07-30T17:02:46.739321Z","shell.execute_reply.started":"2022-07-30T17:02:46.725399Z","shell.execute_reply":"2022-07-30T17:02:46.738093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"holidaysデータの場合","metadata":{"editable":false}},{"cell_type":"code","source":"holidays.head(10)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:46.741261Z","iopub.execute_input":"2022-07-30T17:02:46.742268Z","iopub.status.idle":"2022-07-30T17:02:46.763087Z","shell.execute_reply.started":"2022-07-30T17:02:46.742220Z","shell.execute_reply":"2022-07-30T17:02:46.761667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"storesデータの場合","metadata":{"editable":false}},{"cell_type":"code","source":"stores.head(10)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:46.764592Z","iopub.execute_input":"2022-07-30T17:02:46.765259Z","iopub.status.idle":"2022-07-30T17:02:46.779436Z","shell.execute_reply.started":"2022-07-30T17:02:46.765212Z","shell.execute_reply":"2022-07-30T17:02:46.778110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"transactionsデータの場合","metadata":{"editable":false}},{"cell_type":"code","source":"transactions.head(10)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:46.781215Z","iopub.execute_input":"2022-07-30T17:02:46.782132Z","iopub.status.idle":"2022-07-30T17:02:46.797236Z","shell.execute_reply.started":"2022-07-30T17:02:46.782085Z","shell.execute_reply":"2022-07-30T17:02:46.796014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 各csvデータの欠損値を確認¶\nもし欠損値があれば, dropnaで欠損値を含む行を削除する","metadata":{"editable":false}},{"cell_type":"code","source":"oil.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:46.798687Z","iopub.execute_input":"2022-07-30T17:02:46.799176Z","iopub.status.idle":"2022-07-30T17:02:46.810626Z","shell.execute_reply.started":"2022-07-30T17:02:46.799112Z","shell.execute_reply":"2022-07-30T17:02:46.809521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oil.isna().sum()\noil = oil.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:46.812401Z","iopub.execute_input":"2022-07-30T17:02:46.813116Z","iopub.status.idle":"2022-07-30T17:02:46.830002Z","shell.execute_reply.started":"2022-07-30T17:02:46.813070Z","shell.execute_reply":"2022-07-30T17:02:46.828633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays.isna().sum()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:46.837434Z","iopub.execute_input":"2022-07-30T17:02:46.837809Z","iopub.status.idle":"2022-07-30T17:02:46.846950Z","shell.execute_reply.started":"2022-07-30T17:02:46.837776Z","shell.execute_reply":"2022-07-30T17:02:46.845923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"holidaysデータ欠損値なし","metadata":{"editable":false}},{"cell_type":"code","source":"stores.isna().sum()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:46.848494Z","iopub.execute_input":"2022-07-30T17:02:46.848996Z","iopub.status.idle":"2022-07-30T17:02:46.861349Z","shell.execute_reply.started":"2022-07-30T17:02:46.848957Z","shell.execute_reply":"2022-07-30T17:02:46.860292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"storesデータ欠損値なし","metadata":{"editable":false}},{"cell_type":"code","source":"transactions.isna().sum()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:46.863128Z","iopub.execute_input":"2022-07-30T17:02:46.863679Z","iopub.status.idle":"2022-07-30T17:02:46.889808Z","shell.execute_reply.started":"2022-07-30T17:02:46.863644Z","shell.execute_reply":"2022-07-30T17:02:46.888371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"transactionsデータ欠損値なし","metadata":{"editable":false}},{"cell_type":"markdown","source":"## 時系列データを扱いやすいようにするためにdatetime64型に変換\nto_datetimeで文字列や数値と日付との変換を行う\ndescribeの引数にinclude=objectを指定することでobjectデータに関する情報を表示する","metadata":{"editable":false}},{"cell_type":"markdown","source":"describe(include=object)で表示されるのは,\nデータ数(count), 重複を排除したデータ数(unique), 最も多く含まれるデータ(top), そのデータが含まれる個数(freq)","metadata":{"editable":false}},{"cell_type":"code","source":"train['date'] = pd.to_datetime(train['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:46.891602Z","iopub.execute_input":"2022-07-30T17:02:46.892473Z","iopub.status.idle":"2022-07-30T17:02:47.459219Z","shell.execute_reply.started":"2022-07-30T17:02:46.892417Z","shell.execute_reply":"2022-07-30T17:02:47.458077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['date'] = pd.to_datetime(test['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.460569Z","iopub.execute_input":"2022-07-30T17:02:47.461790Z","iopub.status.idle":"2022-07-30T17:02:47.477244Z","shell.execute_reply.started":"2022-07-30T17:02:47.461752Z","shell.execute_reply":"2022-07-30T17:02:47.476006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oil['date'] = pd.to_datetime(oil['date'])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.478906Z","iopub.execute_input":"2022-07-30T17:02:47.480022Z","iopub.status.idle":"2022-07-30T17:02:47.487623Z","shell.execute_reply.started":"2022-07-30T17:02:47.479975Z","shell.execute_reply":"2022-07-30T17:02:47.486500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays['date'] = pd.to_datetime(holidays['date'])\nholidays.describe(include = [object])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.488997Z","iopub.execute_input":"2022-07-30T17:02:47.489856Z","iopub.status.idle":"2022-07-30T17:02:47.524783Z","shell.execute_reply.started":"2022-07-30T17:02:47.489811Z","shell.execute_reply":"2022-07-30T17:02:47.523701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['date'] = pd.to_datetime(transactions['date']) ","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.526460Z","iopub.execute_input":"2022-07-30T17:02:47.526882Z","iopub.status.idle":"2022-07-30T17:02:47.548358Z","shell.execute_reply.started":"2022-07-30T17:02:47.526839Z","shell.execute_reply":"2022-07-30T17:02:47.547339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"storesデータはdateを含んでいないため,to_datetimeを適用しない","metadata":{"editable":false}},{"cell_type":"markdown","source":"## データをグラフで描画することで可視化\nグラフの描画にはpandas.plottingを使用する","metadata":{"editable":false}},{"cell_type":"markdown","source":"display.float_formatでfloat型(浮動小数点)を任意の書式の文字列に変換する呼び出し可能objectを指定する\n書式指定文字列'.[桁数]f'で小数点以下の桁数を指定","metadata":{"editable":false}},{"cell_type":"markdown","source":"set_printoptionsで各パラメータを設定する\nprecisionは小数点以下の桁数を指定","metadata":{"editable":false}},{"cell_type":"code","source":"pd.options.display.float_format = \"{:, .2f}\".format\nnp.set_printoptions(precision = 2)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.549988Z","iopub.execute_input":"2022-07-30T17:02:47.550522Z","iopub.status.idle":"2022-07-30T17:02:47.557399Z","shell.execute_reply.started":"2022-07-30T17:02:47.550476Z","shell.execute_reply":"2022-07-30T17:02:47.554771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\noil[\"dcoilwtico\"] = np.where(oil[\"dcoilwtico\"] == 0, np.nan, oil[\"dcoilwtico\"])\noil[\"dcoilwtico_interpolated\"] = oil.dcoilwtico.interpolate()\noil.drop(columns = ['dcoilwtico'], inplace = True , axis = 1)\n\nplt.figure(figsize = (30,8))\nplt.plot(oil['date'],oil['dcoilwtico_interpolated']*10000, color = 'orangered',linewidth = '2.5', linestyle='-',label = 'dcoilwtico_interpolated')\ntrain.groupby(['date'])['sales'].sum().plot(color = 'dodgerblue',alpha = 0.8)\nplt.title(f'Oil Price & Sales(2013-01-01 ~ 2017-08-15)',fontsize = 30)\nplt.xlabel('date',fontsize = 30)\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:05:55.958250Z","iopub.execute_input":"2022-07-30T17:05:55.958663Z","iopub.status.idle":"2022-07-30T17:05:56.422247Z","shell.execute_reply.started":"2022-07-30T17:05:55.958633Z","shell.execute_reply":"2022-07-30T17:05:56.421271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"seabornでMatplotlibの見た目を変更する\nLag_plotで時系列データのパターンを認識する","metadata":{"editable":false}},{"cell_type":"code","source":"from pandas.plotting import lag_plot\nimport seaborn as sns\nsns.set()\n\nlag_plot(oil['date'])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.646427Z","iopub.status.idle":"2022-07-30T17:02:47.646870Z","shell.execute_reply.started":"2022-07-30T17:02:47.646675Z","shell.execute_reply":"2022-07-30T17:02:47.646693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lag_plot(holidays['date'])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.648304Z","iopub.status.idle":"2022-07-30T17:02:47.648758Z","shell.execute_reply.started":"2022-07-30T17:02:47.648565Z","shell.execute_reply":"2022-07-30T17:02:47.648584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lag_plot(transactions['date'])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.650207Z","iopub.status.idle":"2022-07-30T17:02:47.650712Z","shell.execute_reply.started":"2022-07-30T17:02:47.650451Z","shell.execute_reply":"2022-07-30T17:02:47.650474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lag_plot(train['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.652088Z","iopub.status.idle":"2022-07-30T17:02:47.652507Z","shell.execute_reply.started":"2022-07-30T17:02:47.652314Z","shell.execute_reply":"2022-07-30T17:02:47.652333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"storesデータはdateを含んでいないため,dateをlag_plotしない","metadata":{"editable":false}},{"cell_type":"markdown","source":"oil, holidays, transactionsの全てが直線型を示す","metadata":{"editable":false}},{"cell_type":"code","source":"import matplotlib.dates as mpl_dates\n\noil.reset_index(inplace=True)\noil['date'] = oil['date'].apply(mpl_dates.date2num)\noil['date'] = oil['date'].astype(float)\n\nholidays.reset_index(inplace=True)\nholidays['date'] = holidays['date'].apply(mpl_dates.date2num)\nholidays['date'] = holidays['date'].astype(float)\n\ntransactions.reset_index(inplace=True)\ntransactions['date'] = transactions['date'].apply(mpl_dates.date2num)\ntransactions['date'] = transactions['date'].astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.653675Z","iopub.status.idle":"2022-07-30T17:02:47.654560Z","shell.execute_reply.started":"2022-07-30T17:02:47.654322Z","shell.execute_reply":"2022-07-30T17:02:47.654350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 自己相関グラフを描画\n過去の値が現在の値に及ぼす影響を考える","metadata":{"editable":false}},{"cell_type":"code","source":"from statsmodels.graphics.tsaplots import plot_acf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\n\nplot_acf(oil['date']) \nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.656485Z","iopub.status.idle":"2022-07-30T17:02:47.657007Z","shell.execute_reply.started":"2022-07-30T17:02:47.656730Z","shell.execute_reply":"2022-07-30T17:02:47.656752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_acf(holidays['date']) \nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.659330Z","iopub.status.idle":"2022-07-30T17:02:47.659754Z","shell.execute_reply.started":"2022-07-30T17:02:47.659569Z","shell.execute_reply":"2022-07-30T17:02:47.659588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_acf(transactions['date']) \nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.660771Z","iopub.status.idle":"2022-07-30T17:02:47.661244Z","shell.execute_reply.started":"2022-07-30T17:02:47.660978Z","shell.execute_reply":"2022-07-30T17:02:47.661003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 偏自己相関グラフの描画\n自己相関グラフには範囲内における全ての相関が考慮されるため偏自己相関グラフも描画する","metadata":{"editable":false}},{"cell_type":"code","source":"from statsmodels.graphics.tsaplots import plot_pacf\n\nplot_pacf(oil['date'], lags = 30)\nplt.show ()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.664876Z","iopub.status.idle":"2022-07-30T17:02:47.665496Z","shell.execute_reply.started":"2022-07-30T17:02:47.665177Z","shell.execute_reply":"2022-07-30T17:02:47.665233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from statsmodels.graphics.tsaplots import plot_pacf\n\nplot_pacf(holidays['date'], lags = 25)\nplt.show ()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.667017Z","iopub.status.idle":"2022-07-30T17:02:47.667575Z","shell.execute_reply.started":"2022-07-30T17:02:47.667281Z","shell.execute_reply":"2022-07-30T17:02:47.667307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from statsmodels.graphics.tsaplots import plot_pacf\n\nplot_pacf(transactions['date'], lags = 50)\nplt.show ()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-07-30T17:02:47.669723Z","iopub.status.idle":"2022-07-30T17:02:47.670270Z","shell.execute_reply.started":"2022-07-30T17:02:47.669997Z","shell.execute_reply":"2022-07-30T17:02:47.670022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AutoRegで今日分散推定器を確認する","metadata":{}},{"cell_type":"code","source":"from statsmodels.tsa.ar_model import AutoReg, ar_select_order\n\nmod_oil = AutoReg(oil['date'], 1)\nres_oil= mod_oil.fit()\nres_oil.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.671708Z","iopub.status.idle":"2022-07-30T17:02:47.672252Z","shell.execute_reply.started":"2022-07-30T17:02:47.671978Z","shell.execute_reply":"2022-07-30T17:02:47.672003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod_holidays = AutoReg(holidays['date'], 1)\nres_holidays = mod_holidays.fit()\nres_holidays.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.674232Z","iopub.status.idle":"2022-07-30T17:02:47.674759Z","shell.execute_reply.started":"2022-07-30T17:02:47.674488Z","shell.execute_reply":"2022-07-30T17:02:47.674514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod_transactions = AutoReg(transactions['date'], 1)\nres_transactions = mod_transactions.fit()\nres_transactions.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.676118Z","iopub.status.idle":"2022-07-30T17:02:47.676645Z","shell.execute_reply.started":"2022-07-30T17:02:47.676376Z","shell.execute_reply":"2022-07-30T17:02:47.676402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"plt_predictで予測を可視化する","metadata":{}},{"cell_type":"code","source":"fig = res_oil.plot_predict(120, 490)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.678235Z","iopub.status.idle":"2022-07-30T17:02:47.678759Z","shell.execute_reply.started":"2022-07-30T17:02:47.678492Z","shell.execute_reply":"2022-07-30T17:02:47.678516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"モデルがデータの主要な特徴を捉えていることを確認する","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,9))\nfig = res_oil.plot_diagnostics(fig=fig, lags=30)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.680006Z","iopub.status.idle":"2022-07-30T17:02:47.680524Z","shell.execute_reply.started":"2022-07-30T17:02:47.680249Z","shell.execute_reply":"2022-07-30T17:02:47.680274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = res_holidays.plot_predict(120, 490)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.681905Z","iopub.status.idle":"2022-07-30T17:02:47.682459Z","shell.execute_reply.started":"2022-07-30T17:02:47.682173Z","shell.execute_reply":"2022-07-30T17:02:47.682197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,9))\nfig = res_holidays.plot_diagnostics(fig=fig, lags=25)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.684274Z","iopub.status.idle":"2022-07-30T17:02:47.684829Z","shell.execute_reply.started":"2022-07-30T17:02:47.684549Z","shell.execute_reply":"2022-07-30T17:02:47.684574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = res_transactions.plot_predict(120, 490)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.686243Z","iopub.status.idle":"2022-07-30T17:02:47.686768Z","shell.execute_reply.started":"2022-07-30T17:02:47.686499Z","shell.execute_reply":"2022-07-30T17:02:47.686524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,9))\nfig = res_transactions.plot_diagnostics(fig=fig, lags=30)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.688224Z","iopub.status.idle":"2022-07-30T17:02:47.688751Z","shell.execute_reply.started":"2022-07-30T17:02:47.688479Z","shell.execute_reply":"2022-07-30T17:02:47.688506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"trainデータの欠損値を確認、datetime64型に変換する","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/train.csv')\ntrain.isna().sum()\ntrain['date'] = pd.to_datetime(train['date'])\n\ntrain['sales'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.690325Z","iopub.status.idle":"2022-07-30T17:02:47.690851Z","shell.execute_reply.started":"2022-07-30T17:02:47.690584Z","shell.execute_reply":"2022-07-30T17:02:47.690609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"testデータの欠損値を確認する","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/test.csv')\ntest.isna().sum()\ntest","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.693009Z","iopub.status.idle":"2022-07-30T17:02:47.693533Z","shell.execute_reply.started":"2022-07-30T17:02:47.693253Z","shell.execute_reply":"2022-07-30T17:02:47.693279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sales = train[['date','sales']].copy()\ntrain_sales.set_index('date', inplace = True)\ntrain_plot = train_sales.resample('D').sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.695109Z","iopub.status.idle":"2022-07-30T17:02:47.695645Z","shell.execute_reply.started":"2022-07-30T17:02:47.695361Z","shell.execute_reply":"2022-07-30T17:02:47.695387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"oilデータの価格推移を描画する","metadata":{}},{"cell_type":"code","source":"oil = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/oil.csv')\noil['date'] = pd.to_datetime(oil['date'])\noil['dcoilwtico'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.697298Z","iopub.status.idle":"2022-07-30T17:02:47.697829Z","shell.execute_reply.started":"2022-07-30T17:02:47.697557Z","shell.execute_reply":"2022-07-30T17:02:47.697583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"横軸400(日)周期でoilの価格が変動していることが分かる","metadata":{}},{"cell_type":"markdown","source":"周期ごとに平均値を計算し,oilデータの欠損値を補う","metadata":{}},{"cell_type":"code","source":"dcoilwtico_first = oil['dcoilwtico'][0:400].mean()\ndcoilwtico_second = oil['dcoilwtico'][400:800].mean()\ndcoilwtico_third = oil['dcoilwtico'][800:].mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.699224Z","iopub.status.idle":"2022-07-30T17:02:47.699744Z","shell.execute_reply.started":"2022-07-30T17:02:47.699478Z","shell.execute_reply":"2022-07-30T17:02:47.699502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ダミー変数を作成","metadata":{}},{"cell_type":"code","source":"train = pd.get_dummies(train,columns = ['family','store_nbr'],drop_first = True)\ntest = pd.get_dummies(test,columns = ['family','store_nbr'],drop_first = True)\ntest = test.reindex(labels = test.columns, axis = 1 )","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.701129Z","iopub.status.idle":"2022-07-30T17:02:47.701659Z","shell.execute_reply.started":"2022-07-30T17:02:47.701387Z","shell.execute_reply":"2022-07-30T17:02:47.701412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"trainデータ、testデータにoilデータを加える","metadata":{}},{"cell_type":"code","source":"df1 = train.copy()\ndf2 = pd.DataFrame(oil,columns = ['date','dcoilwtico'])\ntrain = pd.merge(df1,df2,left_on ='date', right_on = 'date', how = 'left')\n\ntest = test.assign(dcoilwtico = dcoilwtico_third)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.703162Z","iopub.status.idle":"2022-07-30T17:02:47.703680Z","shell.execute_reply.started":"2022-07-30T17:02:47.703413Z","shell.execute_reply":"2022-07-30T17:02:47.703438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\ntrain.loc[(train[\"date\"] < datetime(2014,7,1)) & (train[\"dcoilwtico\"].isnull()),\"dcoilwtico\"] = dcoilwtico_first\ntrain.loc[(datetime(2014,7,1) < train[\"date\"]) & (train[\"date\"] < datetime(2016,2,1)) & (train[\"dcoilwtico\"].isnull()),\"dcoilwtico\"] = dcoilwtico_second\ntrain.loc[(datetime(2016,2,1) < train[\"date\"]) & (train[\"dcoilwtico\"].isnull()),\"dcoilwtico\"] = dcoilwtico_third\n","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.705126Z","iopub.status.idle":"2022-07-30T17:02:47.706290Z","shell.execute_reply.started":"2022-07-30T17:02:47.706089Z","shell.execute_reply":"2022-07-30T17:02:47.706108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"線形回帰モデルを用いて予測を行う","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\ntrain_x=train.copy()\ntrain_x.drop(columns = ['sales','date','id'],inplace = True, axis = 1)\ntrain_y=train.sales\n\nmodel = LinearRegression()\nmodel.fit(train_x, train_y)\n\ntest_x = test.copy()\ntest_x.drop(columns = ['id','date'] , inplace = True, axis = 1)\n\nprint(model.predict(test_x))\npred = model.predict(test_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.710635Z","iopub.status.idle":"2022-07-30T17:02:47.711050Z","shell.execute_reply.started":"2022-07-30T17:02:47.710832Z","shell.execute_reply":"2022-07-30T17:02:47.710849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['sales'] = pred\nsubmission.to_csv(\"submission.csv\", index = False)\nprint(\"submission successed\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T17:02:47.712902Z","iopub.status.idle":"2022-07-30T17:02:47.713473Z","shell.execute_reply.started":"2022-07-30T17:02:47.713255Z","shell.execute_reply":"2022-07-30T17:02:47.713276Z"},"trusted":true},"execution_count":null,"outputs":[]}]}