{"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":"markdown","source":"**This notebook is an exercise in the [Time Series](https://www.kaggle.com/learn/time-series) course.  You can reference the tutorial at [this link](https://www.kaggle.com/ryanholbrook/forecasting-with-machine-learning).**\n\n---\n","metadata":{}},{"cell_type":"markdown","source":"# Introduction #\n\nRun this cell to set everything up!","metadata":{}},{"cell_type":"code","source":"# Setup feedback system\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.time_series.ex6 import *\n\n# Setup notebook\nfrom pathlib import Path\nimport ipywidgets as widgets\nfrom learntools.time_series.style import *  # plot style settings\nfrom learntools.time_series.utils import (create_multistep_example,\n                                          load_multistep_data,\n                                          make_lags,\n                                          make_multistep_target,\n                                          plot_multistep)\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.multioutput import RegressorChain\nfrom sklearn.preprocessing import LabelEncoder\nfrom xgboost import XGBRegressor\n\n\ncomp_dir = Path('../input/store-sales-time-series-forecasting')\n\nstore_sales = pd.read_csv(\n    comp_dir / 'train.csv',\n    usecols=['store_nbr', 'family', 'date', 'sales', 'onpromotion'],\n    dtype={\n        'store_nbr': 'category',\n        'family': 'category',\n        'sales': 'float32',\n        'onpromotion': 'uint32',\n    },\n    parse_dates=['date'],\n    infer_datetime_format=True,\n)\nstore_sales['date'] = store_sales.date.dt.to_period('D')\nstore_sales = store_sales.set_index(['store_nbr', 'family', 'date']).sort_index()\n\nfamily_sales = (\n    store_sales\n    .groupby(['family', 'date'])\n    .mean()\n    .unstack('family')\n    .loc['2017']\n)\n\ntest = pd.read_csv(\n    comp_dir / 'test.csv',\n    dtype={\n        'store_nbr': 'category',\n        'family': 'category',\n        'onpromotion': 'uint32',\n    },\n    parse_dates=['date'],\n    infer_datetime_format=True,\n)\ntest['date'] = test.date.dt.to_period('D')\ntest = test.set_index(['store_nbr', 'family', 'date']).sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:19.734141Z","iopub.execute_input":"2022-07-29T16:23:19.734553Z","iopub.status.idle":"2022-07-29T16:23:24.567012Z","shell.execute_reply.started":"2022-07-29T16:23:19.734518Z","shell.execute_reply":"2022-07-29T16:23:24.565861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\nConsider the following three forecasting tasks:\n\na. 3-step forecast using 4 lag features with a 2-step lead time<br>\nb. 1-step forecast using 3 lag features with a 1-step lead time<br>\nc. 3-step forecast using 4 lag features with a 1-step lead time<br>\n\nRun the next cell to see three datasets, each representing one of the tasks above.","metadata":{}},{"cell_type":"code","source":"datasets = load_multistep_data()\n\ndata_tabs = widgets.Tab([widgets.Output() for _ in enumerate(datasets)])\nfor i, df in enumerate(datasets):\n    data_tabs.set_title(i, f'Dataset {i+1}')\n    with data_tabs.children[i]:\n        display(df)\n\ndisplay(data_tabs)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.569341Z","iopub.execute_input":"2022-07-29T16:23:24.570174Z","iopub.status.idle":"2022-07-29T16:23:24.759407Z","shell.execute_reply.started":"2022-07-29T16:23:24.570136Z","shell.execute_reply":"2022-07-29T16:23:24.758072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1) Match description to dataset\n\nCan you match each task to the appropriate dataset?","metadata":{}},{"cell_type":"code","source":"# YOUR CODE HERE: Match the task to the dataset. Answer 1, 2, or 3.\ntask_a = 2\ntask_b = 1\ntask_c = 3\n\n# Check your answer\nq_1.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.761060Z","iopub.execute_input":"2022-07-29T16:23:24.761417Z","iopub.status.idle":"2022-07-29T16:23:24.772517Z","shell.execute_reply.started":"2022-07-29T16:23:24.761385Z","shell.execute_reply":"2022-07-29T16:23:24.771522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lines below will give you a hint or solution code\n#q_1.hint()\n# q_1.solution()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.774999Z","iopub.execute_input":"2022-07-29T16:23:24.775787Z","iopub.status.idle":"2022-07-29T16:23:24.780474Z","shell.execute_reply.started":"2022-07-29T16:23:24.775749Z","shell.execute_reply":"2022-07-29T16:23:24.779341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\nLook at the time indexes of the training and test sets. From this information, can you identify the forecasting task for *Store Sales*?","metadata":{}},{"cell_type":"code","source":"print(\"Training Data\", \"\\n\" + \"-\" * 13 + \"\\n\", store_sales)\nprint(\"\\n\")\nprint(\"Test Data\", \"\\n\" + \"-\" * 9 + \"\\n\", test)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.781586Z","iopub.execute_input":"2022-07-29T16:23:24.781959Z","iopub.status.idle":"2022-07-29T16:23:24.809103Z","shell.execute_reply.started":"2022-07-29T16:23:24.781927Z","shell.execute_reply":"2022-07-29T16:23:24.807978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2) Identify the forecasting task for *Store Sales* competition\n\nTry to identify the *forecast origin* and the *forecast horizon*. How many steps are within the forecast horizon? What is the lead time for the forecast?\n\nRun this cell after you've thought about your answer.","metadata":{}},{"cell_type":"code","source":"# View the solution (Run this cell to receive credit!)\nq_2.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.810953Z","iopub.execute_input":"2022-07-29T16:23:24.811374Z","iopub.status.idle":"2022-07-29T16:23:24.820837Z","shell.execute_reply.started":"2022-07-29T16:23:24.811341Z","shell.execute_reply":"2022-07-29T16:23:24.819623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\nIn the tutorial we saw how to create a multistep dataset for a single time series. Fortunately, we can use exactly the same procedure for datasets of multiple series.\n\n# 3) Create multistep dataset for *Store Sales*\n\nCreate targets suitable for the *Store Sales* forecasting task. Use 4 days of lag features. Drop any missing values from both targets and features.","metadata":{}},{"cell_type":"code","source":"# YOUR CODE HERE\ny = family_sales.loc[:, 'sales']\n\n# YOUR CODE HERE: Make 4 lag features\nX =  make_lags(y, lags=4).dropna()\n\n# YOUR CODE HERE: Make multistep target\ny = make_multistep_target(y, steps=16).dropna()\n\ny, X = y.align(X, join='inner', axis=0)\n\n# Check your answer\nq_3.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.822652Z","iopub.execute_input":"2022-07-29T16:23:24.823026Z","iopub.status.idle":"2022-07-29T16:23:24.857547Z","shell.execute_reply.started":"2022-07-29T16:23:24.822994Z","shell.execute_reply":"2022-07-29T16:23:24.856190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lines below will give you a hint or solution code\n#q_3.hint()\n# q_3.solution()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.859154Z","iopub.execute_input":"2022-07-29T16:23:24.859538Z","iopub.status.idle":"2022-07-29T16:23:24.865863Z","shell.execute_reply.started":"2022-07-29T16:23:24.859507Z","shell.execute_reply":"2022-07-29T16:23:24.863928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\nIn the tutorial, we saw how to forecast with the MultiOutput and Direct strategies on the *Flu Trends* series. Now, you'll apply the DirRec strategy to the multiple time series of *Store Sales*.\n\nMake sure you've successfully completed the previous exercise and then run this cell to prepare the data for XGBoost.","metadata":{}},{"cell_type":"code","source":"le = LabelEncoder()\nX = (X\n    .stack('family')  # wide to long\n    .reset_index('family')  # convert index to column\n    .assign(family=lambda x: le.fit_transform(x.family))  # label encode\n)\ny = y.stack('family')  # wide to long\n\ndisplay(y)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.867601Z","iopub.execute_input":"2022-07-29T16:23:24.867954Z","iopub.status.idle":"2022-07-29T16:23:24.925822Z","shell.execute_reply.started":"2022-07-29T16:23:24.867922Z","shell.execute_reply":"2022-07-29T16:23:24.924552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4) Forecast with the DirRec strategy\n\nInstatiate a model that applies the DirRec strategy to XGBoost.","metadata":{}},{"cell_type":"code","source":"from sklearn.multioutput import RegressorChain\n\n# YOUR CODE HERE\nmodel = RegressorChain(base_estimator=XGBRegressor())\n\n# Check your answer\nq_4.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.929197Z","iopub.execute_input":"2022-07-29T16:23:24.929609Z","iopub.status.idle":"2022-07-29T16:23:24.945709Z","shell.execute_reply.started":"2022-07-29T16:23:24.929553Z","shell.execute_reply":"2022-07-29T16:23:24.944338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lines below will give you a hint or solution code\n#q_4.hint()\n# q_4.solution()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.947402Z","iopub.execute_input":"2022-07-29T16:23:24.948032Z","iopub.status.idle":"2022-07-29T16:23:24.952543Z","shell.execute_reply.started":"2022-07-29T16:23:24.947998Z","shell.execute_reply":"2022-07-29T16:23:24.951308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Run this cell if you'd like to train this model.","metadata":{}},{"cell_type":"code","source":"model.fit(X, y)\n\ny_pred = pd.DataFrame(\n    model.predict(X),\n    index=y.index,\n    columns=y.columns,\n).clip(0.0)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:23:24.954529Z","iopub.execute_input":"2022-07-29T16:23:24.955373Z","iopub.status.idle":"2022-07-29T16:23:45.227339Z","shell.execute_reply.started":"2022-07-29T16:23:24.955337Z","shell.execute_reply":"2022-07-29T16:23:45.226031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"And use this code to see a sample of the 16-step predictions this model makes on the training data.","metadata":{}},{"cell_type":"code","source":"FAMILY = 'BEAUTY'\nSTART = '2017-04-01'\nEVERY = 16\n\ny_pred_ = y_pred.xs(FAMILY, level='family', axis=0).loc[START:]\ny_ = family_sales.loc[START:, 'sales'].loc[:, FAMILY]\n\nfig, ax = plt.subplots(1, 1, figsize=(11, 4))\nax = y_.plot(**plot_params, ax=ax, alpha=0.5)\nax = plot_multistep(y_pred_, ax=ax, every=EVERY)\n_ = ax.legend([FAMILY, FAMILY + ' Forecast'])","metadata":{"lines_to_next_cell":2,"execution":{"iopub.status.busy":"2022-07-29T16:23:45.228980Z","iopub.execute_input":"2022-07-29T16:23:45.229322Z","iopub.status.idle":"2022-07-29T16:23:45.624025Z","shell.execute_reply.started":"2022-07-29T16:23:45.229292Z","shell.execute_reply":"2022-07-29T16:23:45.622342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Next Steps #\n\nCongratulations! You've completed Kaggle's *Time Series* course. If you haven't already, join our companion competition: [Store Sales - Time Series Forecasting](https://www.kaggle.com/c/29781) and apply the skills you've learned.\n\nFor inspiration, check out Kaggle's previous forecasting competitions. Studying winning competition solutions is a great way to upgrade your skills.\n\n- [**Corporación Favorita**](https://www.kaggle.com/c/favorita-grocery-sales-forecasting): the competition *Store Sales* is derived from.\n- [**Rossmann Store Sales**](https://www.kaggle.com/c/rossmann-store-sales)\n- [**Wikipedia Web Traffic**](https://www.kaggle.com/c/web-traffic-time-series-forecasting/)\n- [**Walmart Store Sales**](https://www.kaggle.com/c/walmart-recruiting-store-sales-forecasting)\n- [**Walmart Sales in Stormy Weather**](https://www.kaggle.com/c/walmart-recruiting-sales-in-stormy-weather)\n- [**M5 Forecasting - Accuracy**](https://www.kaggle.com/c/m5-forecasting-accuracy)\n\n# References #\n\nHere are some great resources you might like to consult for more on time series and forecasting. They all played a part in shaping this course:\n\n- *Learnings from Kaggle's forecasting competitions*, an article by Casper Solheim Bojer and Jens Peder Meldgaard.\n- *Forecasting: Principles and Practice*, a book by Rob J Hyndmann and George Athanasopoulos.\n- *Practical Time Series Forecasting with R*, a book by Galit Shmueli and Kenneth C. Lichtendahl Jr.\n- *Time Series Analysis and Its Applications*, a book by Robert H. Shumway and David S. Stoffer.\n- *Machine learning strategies for time series forecasting*, an article by Gianluca Bontempi, Souhaib Ben Taieb, and Yann-Aël Le Borgne.\n- *On the use of cross-validation for time series predictor evaluation*, an article by Christoph Bergmeir and José M. Benítez.\n","metadata":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [course discussion forum](https://www.kaggle.com/learn/time-series/discussion) to chat with other learners.*","metadata":{}}]}