{"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":"<a id=\"Introduction\"></a> \n\n<img src=\"https://triuminfo.com/application/views/images/sales_forecasting_banner.jpg\">\n\n<center><h1>💰 Store Sales💰</h1></center>\n\n# Introduction \n\nWhat is the most stores and grocery families that is most consumend on our markets? 🤔\n\ndoes those sales got affected by seasonality (holidays) or by any trend?\n\nWell, you can't imagine consumer behavior... if you want we can test it, you can find a correlation anywhere you look ... if you're really, deeply paying attention.\n\n\n> **🟢 Goal**: This notebook has the purpose of analysing and predicting Sales prices to generate more products to be sold on our stores, as well as identifying if there is any impact for seasonality on the prices.\n\n Manily our stores are located on Ecuador, and we need to imporve our sales in different states. \n\nCorporación Favorita, a large Ecuadorian-based grocery retailer.\n\n","metadata":{}},{"cell_type":"markdown","source":"### 📚 Libraries & Functions","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport pandas as pd\nimport numpy as np\n\nimport datetime as dt\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nimport seaborn as sns\n\n# from sklearn.metrics import mean_squared_error, mean_absolute_error, explained_variance_score, r2_score \n# from sklearn.metrics import mean_poisson_deviance, mean_gamma_deviance, accuracy_score\nfrom sklearn.preprocessing import MinMaxScaler\n\n# from itertools import product\n# import statsmodels.api as sm\n\n# import itertools\n# from statsmodels.tsa.arima_model import ARIMA\n# from statsmodels.tsa.holtwinters import ExponentialSmoothing\n# from statsmodels.tsa.holtwinters import SimpleExpSmoothing\n# from statsmodels.tsa.seasonal import seasonal_decompose\n# import statsmodels.tsa.api as smt\n# from statsmodels.tsa.statespace.sarimax import SARIMAX\n\n\nfrom itertools import cycle\nimport plotly.offline as py\nimport plotly.graph_objects as go\nimport plotly.express as px\nfrom plotly.subplots import make_subplots\n\nfrom statsmodels.tsa.stattools import adfuller       ### Augmented Dickey Fuller\n\n# set style \n\nplt.style.use('seaborn-darkgrid')\n\nmpl.rcParams['xtick.labelsize'] = 16\nmpl.rcParams['ytick.labelsize'] = 16\nmpl.rcParams['axes.spines.left'] = False\nmpl.rcParams['axes.spines.right'] = False\nmpl.rcParams['axes.spines.top'] = False\n\nclass clr:\n    S = '\\033[1m' + '\\033[95m'\n    E = '\\033[0m'\n\n\n# Color palette\n\nmy_colors = [\"#ce8f5a\", \"#efd199\", \"#80c8bc\", \"#5ec0ca\", \"#6287a2\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:15.575568Z","iopub.execute_input":"2022-07-02T21:52:15.575907Z","iopub.status.idle":"2022-07-02T21:52:18.260790Z","shell.execute_reply.started":"2022-07-02T21:52:15.575818Z","shell.execute_reply":"2022-07-02T21:52:18.259781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ===== DICKEY FULLER TEST =====\n\ndef test_stationarity(x, log='non-stationary'):\n    \n    \"\"\"Test stationarity of a time Series variable.\"\"\"\n    \n    # Perform Dicky Fuller test \n    \n    result = adfuller(x)\n    \n    print(\"ADF statistic: {}\".format(result[0]))\n    \n    print(\"p-value: {}\".format(result[1]))\n    \n    for key, value in result[4].items():\n        \n        if result[0] > value:\n            print (clr.S + \"The graph is non-stationary! (it has a trend)\" + clr.E)\n            \n            break\n            \n        else: \n            print (clr.S + \"The graph is stationary! (it doesn't have a trend)\" + clr.E)\n            \n            break\n            \n    print(\"Critical values:\")\n    \n    for key,value in result[4].items():\n        \n        print(\"\\t%s: %.3f \" %(key, value))\n        \n    # Determine rolling statistics \n    \n    rolmean = x.rolling(window=22, center=False).mean()\n    \n    rolstd = x.rolling(window=22, center=False).std()\n    \n    # Plot rolling statistics:\n    \n    plt.figure(figsize= (25, 11))\n    \n    orig = plt.plot(x, color = my_colors[0], lw=8, label = \"Original\")\n    \n    mean = plt.plot(rolmean, color = my_colors[2], lw=2.5, ls= '--', label = 'Rolling Mean')\n    \n    std = plt.plot(rolmean, color = my_colors[3], lw=3, label = 'Rolling Std')\n    \n    plt.legend(loc='best', fontsize=20)\n    \n    plt.title('Rolling Mean & Standard Deviation', size=25)\n    \n    plt.show(block=False)\n    \n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.263527Z","iopub.execute_input":"2022-07-02T21:52:18.263865Z","iopub.status.idle":"2022-07-02T21:52:18.276780Z","shell.execute_reply.started":"2022-07-02T21:52:18.263819Z","shell.execute_reply":"2022-07-02T21:52:18.275424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run this to enable CSS stype\n# WARNING: run it in a separate cell, not alongside other code blocks too\nfrom IPython.core.display import HTML\ndef css_styling():\n    styles = open(\"../input/alerts-21/alerts.css\", \"r\").read()\n    return HTML(\"<style>\"+styles+\"</style>\")\ncss_styling()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.277932Z","iopub.execute_input":"2022-07-02T21:52:18.278153Z","iopub.status.idle":"2022-07-02T21:52:18.305700Z","shell.execute_reply.started":"2022-07-02T21:52:18.278127Z","shell.execute_reply":"2022-07-02T21:52:18.304822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"Understand the data \"></a> \n\n# 1. Understand the data 🧐\n\n## 1.1 🏪 Stores overview\n\n<div class=\"alert simple-alert\">\n  💡<b>Helpful Notes</b>: \n      \n* store_nbr: Identifies the store at which the products are sold.\n* city \n* state \n* type\n* cluster  \n    \n</div>\n","metadata":{}},{"cell_type":"markdown","source":"### 📥 Read in Data","metadata":{}},{"cell_type":"code","source":"%%time \n\nstores = pd.read_csv(\"../input/store-sales-time-series-forecasting/stores.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.307933Z","iopub.execute_input":"2022-07-02T21:52:18.308402Z","iopub.status.idle":"2022-07-02T21:52:18.325406Z","shell.execute_reply.started":"2022-07-02T21:52:18.308358Z","shell.execute_reply":"2022-07-02T21:52:18.324442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores.head() ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.326863Z","iopub.execute_input":"2022-07-02T21:52:18.327525Z","iopub.status.idle":"2022-07-02T21:52:18.351762Z","shell.execute_reply.started":"2022-07-02T21:52:18.327486Z","shell.execute_reply":"2022-07-02T21:52:18.350910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores.cluster.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.353045Z","iopub.execute_input":"2022-07-02T21:52:18.353447Z","iopub.status.idle":"2022-07-02T21:52:18.366247Z","shell.execute_reply.started":"2022-07-02T21:52:18.353399Z","shell.execute_reply":"2022-07-02T21:52:18.365366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores.type.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.367621Z","iopub.execute_input":"2022-07-02T21:52:18.367886Z","iopub.status.idle":"2022-07-02T21:52:18.382671Z","shell.execute_reply.started":"2022-07-02T21:52:18.367857Z","shell.execute_reply":"2022-07-02T21:52:18.381819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.384483Z","iopub.execute_input":"2022-07-02T21:52:18.384996Z","iopub.status.idle":"2022-07-02T21:52:18.408953Z","shell.execute_reply.started":"2022-07-02T21:52:18.384954Z","shell.execute_reply":"2022-07-02T21:52:18.408001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://besthotelshome.com/wp-content/uploads/2020/03/Map-of-Ecuador.jpg)","metadata":{}},{"cell_type":"code","source":"stores.state.unique() \n\n\n# we will focus manil with two states \"Pichincha\" where the capital of ecuador is located ('Quito')\n# and \"Guayas\" where the largest city in Ecuador is located ('Guayaquil'). ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.409923Z","iopub.execute_input":"2022-07-02T21:52:18.410413Z","iopub.status.idle":"2022-07-02T21:52:18.415403Z","shell.execute_reply.started":"2022-07-02T21:52:18.410379Z","shell.execute_reply":"2022-07-02T21:52:18.414852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores.city.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.417598Z","iopub.execute_input":"2022-07-02T21:52:18.418302Z","iopub.status.idle":"2022-07-02T21:52:18.434167Z","shell.execute_reply.started":"2022-07-02T21:52:18.418256Z","shell.execute_reply":"2022-07-02T21:52:18.433356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_cities = len(stores.city.unique())\nno_of_states = len(stores.state.unique()) ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.435099Z","iopub.execute_input":"2022-07-02T21:52:18.435499Z","iopub.status.idle":"2022-07-02T21:52:18.446057Z","shell.execute_reply.started":"2022-07-02T21:52:18.435455Z","shell.execute_reply":"2022-07-02T21:52:18.445014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+f\"Our stores are manily located in {no_of_states} differnet states and in particular {no_of_cities} different cities.\"+clr.E)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.447570Z","iopub.execute_input":"2022-07-02T21:52:18.448343Z","iopub.status.idle":"2022-07-02T21:52:18.458654Z","shell.execute_reply.started":"2022-07-02T21:52:18.448295Z","shell.execute_reply":"2022-07-02T21:52:18.458058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+\"Our unique stores:\"+clr.E, stores.store_nbr.unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.459514Z","iopub.execute_input":"2022-07-02T21:52:18.460034Z","iopub.status.idle":"2022-07-02T21:52:18.471953Z","shell.execute_reply.started":"2022-07-02T21:52:18.460000Z","shell.execute_reply":"2022-07-02T21:52:18.471103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores_per_state_per_city = stores.groupby(['state','city'])['store_nbr'].count().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.473135Z","iopub.execute_input":"2022-07-02T21:52:18.473369Z","iopub.status.idle":"2022-07-02T21:52:18.485727Z","shell.execute_reply.started":"2022-07-02T21:52:18.473342Z","shell.execute_reply":"2022-07-02T21:52:18.485049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(stores_per_state_per_city, x = 'city', y = 'store_nbr', color='state', \n       labels = dict(city= 'City', store_nbr='Number of Stores'),\n    title = 'Stores per state and city ')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:18.486744Z","iopub.execute_input":"2022-07-02T21:52:18.487227Z","iopub.status.idle":"2022-07-02T21:52:19.629576Z","shell.execute_reply.started":"2022-07-02T21:52:18.487194Z","shell.execute_reply":"2022-07-02T21:52:19.628544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Increase Plot size ","metadata":{}},{"cell_type":"markdown","source":"> We can **Conclude** by looking on this chart that Only 5 states have more than 2 stores. From 10th states, there is only one store per state.","metadata":{}},{"cell_type":"markdown","source":"<a id = 'train dataset overview'></a>\n\n## 1.2  train dataset overview\n\n<div class=\"alert simple-alert\">\n  💡<b>Helpful Notes</b>: \n      \n* 3+ million store records.\n* store_nbr: Identifies the store at which the products are sold. \n* family: Type of product sold. \n* Sales: Total sales at a given date. \n* onpromotion: Total number of items in a product family that were being promoted.\n    \n</div>","metadata":{}},{"cell_type":"code","source":"%%time\n\ntrain = pd.read_csv('../input/store-sales-time-series-forecasting/train.csv', index_col=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:19.631151Z","iopub.execute_input":"2022-07-02T21:52:19.631496Z","iopub.status.idle":"2022-07-02T21:52:24.940598Z","shell.execute_reply.started":"2022-07-02T21:52:19.631456Z","shell.execute_reply":"2022-07-02T21:52:24.939766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:24.942209Z","iopub.execute_input":"2022-07-02T21:52:24.942663Z","iopub.status.idle":"2022-07-02T21:52:24.957346Z","shell.execute_reply.started":"2022-07-02T21:52:24.942616Z","shell.execute_reply":"2022-07-02T21:52:24.956657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+f\"The dataset contains {train.shape[0]} rows \"+clr.E)\nprint(clr.S+f\"The dataset contains {train.shape[1]} coulmns\"+clr.E)\n\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:24.959033Z","iopub.execute_input":"2022-07-02T21:52:24.959563Z","iopub.status.idle":"2022-07-02T21:52:25.253477Z","shell.execute_reply.started":"2022-07-02T21:52:24.959514Z","shell.execute_reply":"2022-07-02T21:52:25.252528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>: Note that we have no missing values on our dataset :) </p>\n</div>","metadata":{}},{"cell_type":"code","source":"print(clr.S+ \"Our stores is build on these types of groceries:\"+clr.E, train.family.unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:25.254842Z","iopub.execute_input":"2022-07-02T21:52:25.255154Z","iopub.status.idle":"2022-07-02T21:52:25.446233Z","shell.execute_reply.started":"2022-07-02T21:52:25.255113Z","shell.execute_reply":"2022-07-02T21:52:25.445423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train.family.unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:25.447415Z","iopub.execute_input":"2022-07-02T21:52:25.447643Z","iopub.status.idle":"2022-07-02T21:52:25.630380Z","shell.execute_reply.started":"2022-07-02T21:52:25.447611Z","shell.execute_reply":"2022-07-02T21:52:25.629401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.sales.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:25.631574Z","iopub.execute_input":"2022-07-02T21:52:25.631821Z","iopub.status.idle":"2022-07-02T21:52:25.646560Z","shell.execute_reply.started":"2022-07-02T21:52:25.631791Z","shell.execute_reply":"2022-07-02T21:52:25.645849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_family_mean = train.groupby('family')['sales'].mean().sort_values(ascending=False).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:25.648045Z","iopub.execute_input":"2022-07-02T21:52:25.648289Z","iopub.status.idle":"2022-07-02T21:52:25.936761Z","shell.execute_reply.started":"2022-07-02T21:52:25.648257Z","shell.execute_reply":"2022-07-02T21:52:25.935791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(sales_per_family_mean, x='family', y='sales', \n       labels = dict(family='Category', sales='Average Sales'),\n       title = \"Average Sales Per Category\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:25.937877Z","iopub.execute_input":"2022-07-02T21:52:25.938676Z","iopub.status.idle":"2022-07-02T21:52:25.995438Z","shell.execute_reply.started":"2022-07-02T21:52:25.938643Z","shell.execute_reply":"2022-07-02T21:52:25.994556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+ f\"WE got over {train.family.nunique()} unique and different categories on our stores.\"+clr.E)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:25.996886Z","iopub.execute_input":"2022-07-02T21:52:25.997108Z","iopub.status.idle":"2022-07-02T21:52:26.186504Z","shell.execute_reply.started":"2022-07-02T21:52:25.997083Z","shell.execute_reply":"2022-07-02T21:52:26.185542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info() # to check our coulmns data types ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:26.187352Z","iopub.execute_input":"2022-07-02T21:52:26.187572Z","iopub.status.idle":"2022-07-02T21:52:26.201103Z","shell.execute_reply.started":"2022-07-02T21:52:26.187544Z","shell.execute_reply":"2022-07-02T21:52:26.200320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert warning-alert\">\n  <p>💡<b>Note</b>: we can see that our data in memort usage is above 135 MB, some data types need to be changed</p>\n</div>\n","metadata":{}},{"cell_type":"code","source":"train['date'] = pd.to_datetime(train['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:26.202289Z","iopub.execute_input":"2022-07-02T21:52:26.202538Z","iopub.status.idle":"2022-07-02T21:52:26.584868Z","shell.execute_reply.started":"2022-07-02T21:52:26.202509Z","shell.execute_reply":"2022-07-02T21:52:26.583933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['year'] = train['date'].dt.year\ntrain.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:26.586522Z","iopub.execute_input":"2022-07-02T21:52:26.586885Z","iopub.status.idle":"2022-07-02T21:52:26.872999Z","shell.execute_reply.started":"2022-07-02T21:52:26.586840Z","shell.execute_reply":"2022-07-02T21:52:26.871798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_made_per_year = train.groupby(\"year\")['sales'].sum().reset_index()\n\nsales_made_per_year","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:26.879571Z","iopub.execute_input":"2022-07-02T21:52:26.879877Z","iopub.status.idle":"2022-07-02T21:52:26.954274Z","shell.execute_reply.started":"2022-07-02T21:52:26.879845Z","shell.execute_reply":"2022-07-02T21:52:26.953460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(sales_made_per_year, x='year', y='sales', \n       labels = dict(year= 'Year', sales='Sales'),\n       title= \"Sales Made Per Year \")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:26.955577Z","iopub.execute_input":"2022-07-02T21:52:26.956025Z","iopub.status.idle":"2022-07-02T21:52:27.032932Z","shell.execute_reply.started":"2022-07-02T21:52:26.955987Z","shell.execute_reply":"2022-07-02T21:52:27.032328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert danger-alert\">\n  <p>💡<b>Note</b>: We coiuld see that most our sales made was on 2016, but a huge drop could be seen on 2017 for our sales.</p>\n</div>","metadata":{}},{"cell_type":"code","source":"count_sales_per_year = train.groupby(\"year\")['sales'].count().reset_index()\n\ncount_sales_per_year","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.034514Z","iopub.execute_input":"2022-07-02T21:52:27.034824Z","iopub.status.idle":"2022-07-02T21:52:27.117580Z","shell.execute_reply.started":"2022-07-02T21:52:27.034784Z","shell.execute_reply":"2022-07-02T21:52:27.116902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(count_sales_per_year, x='year', y='sales', \n       labels = dict(year= 'Year', sales='Sales'),\n       title= \"Number of Sales Per Year\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.118886Z","iopub.execute_input":"2022-07-02T21:52:27.119291Z","iopub.status.idle":"2022-07-02T21:52:27.174491Z","shell.execute_reply.started":"2022-07-02T21:52:27.119250Z","shell.execute_reply":"2022-07-02T21:52:27.173615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert danger-alert\">\n  <p>💡<b>Note</b>: Seems like we have a drop in years 2016-2017 overall sales.</p>\n</div>","metadata":{}},{"cell_type":"code","source":"train['month'] = train['date'].dt.month","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.175561Z","iopub.execute_input":"2022-07-02T21:52:27.175797Z","iopub.status.idle":"2022-07-02T21:52:27.460244Z","shell.execute_reply.started":"2022-07-02T21:52:27.175770Z","shell.execute_reply":"2022-07-02T21:52:27.459418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_month = train.groupby(['year','month'])['sales'].sum().reset_index()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.461592Z","iopub.execute_input":"2022-07-02T21:52:27.462031Z","iopub.status.idle":"2022-07-02T21:52:27.639476Z","shell.execute_reply.started":"2022-07-02T21:52:27.462001Z","shell.execute_reply":"2022-07-02T21:52:27.638739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_month.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.640520Z","iopub.execute_input":"2022-07-02T21:52:27.640771Z","iopub.status.idle":"2022-07-02T21:52:27.649495Z","shell.execute_reply.started":"2022-07-02T21:52:27.640721Z","shell.execute_reply":"2022-07-02T21:52:27.648678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(sales_per_month, x = 'month', y= 'sales', color='year')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.650623Z","iopub.execute_input":"2022-07-02T21:52:27.650953Z","iopub.status.idle":"2022-07-02T21:52:27.733715Z","shell.execute_reply.started":"2022-07-02T21:52:27.650919Z","shell.execute_reply":"2022-07-02T21:52:27.732911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We could see that we start every year with a better sales than the preceding year (excep for 2015 year) which is a good indacator on overall,but we got a couple of peaks that needs further investagations. ","metadata":{}},{"cell_type":"code","source":"top_5_categories = train.groupby('family')['sales'].mean().sort_values(ascending=False)[:5].reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.734790Z","iopub.execute_input":"2022-07-02T21:52:27.735069Z","iopub.status.idle":"2022-07-02T21:52:27.986717Z","shell.execute_reply.started":"2022-07-02T21:52:27.735040Z","shell.execute_reply":"2022-07-02T21:52:27.985818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>:These are the top 5 grocerie types in sales :) </p>\n</div>","metadata":{}},{"cell_type":"code","source":"for i in top_5_categories.family.unique():\n    \n    plt.figure(figsize=(20,8))\n    \n    sns.lineplot(y = train[['date','sales']][train['family']==i]\n                 .resample('M', on='date')\n                 .mean()\n                 .reset_index(drop=False)['sales']\n                 \n    , x = train[['date','sales']][train['family']==i]\n                 .resample('M', on='date')\n                 .mean()\n                 .reset_index(drop=False)['date'],linewidth = 1.5)\n    \n    plt.xlabel('Date')\n    plt.ylabel(\"Sales Mean\")\n    plt.title('{} Times Series Forecasting'.format(i))\n    plt.xticks(rotation=45)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:27.988126Z","iopub.execute_input":"2022-07-02T21:52:27.988447Z","iopub.status.idle":"2022-07-02T21:52:31.500724Z","shell.execute_reply.started":"2022-07-02T21:52:27.988404Z","shell.execute_reply":"2022-07-02T21:52:31.499931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"least_5_categories = train.groupby('family')['sales'].mean().sort_values(ascending=True)[:5].reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:31.501835Z","iopub.execute_input":"2022-07-02T21:52:31.502037Z","iopub.status.idle":"2022-07-02T21:52:31.770555Z","shell.execute_reply.started":"2022-07-02T21:52:31.502013Z","shell.execute_reply":"2022-07-02T21:52:31.769792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>:These are the lowest 5 groceries types in sales. </p>\n</div>","metadata":{}},{"cell_type":"code","source":"for cat in least_5_categories.family.unique():\n    \n    plt.figure(figsize=(20,8))\n    \n    sns.lineplot(y = train[['date','sales']][train['family']==cat]\n                 .resample('M', on='date')\n                 .mean()\n                 .reset_index(drop=False)['sales']\n                 \n    , x = train[['date','sales']][train['family']==cat]\n                 .resample('M', on='date')\n                 .mean()\n                 .reset_index(drop=False)['date'],linewidth = 1.5)\n    \n    plt.xlabel('Date')\n    plt.ylabel(\"Sales Mean\")\n    plt.title('{} Times Series Forecasting'.format(i))\n    plt.xticks(rotation=45)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:31.771599Z","iopub.execute_input":"2022-07-02T21:52:31.771812Z","iopub.status.idle":"2022-07-02T21:52:35.244805Z","shell.execute_reply.started":"2022-07-02T21:52:31.771787Z","shell.execute_reply":"2022-07-02T21:52:35.243828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = 'Further data to analyze'></a>\n\n# 1.2 Further data to analyze (Events) dataset \n\n**💡 Helpful Notes**:\n\n\n* Date \n* Type: if its a holiday or not \n* Locale: if this holiday is local or regional \n* locale_name: name of the local city \n* description: brief describtion about the event \n","metadata":{}},{"cell_type":"code","source":"%%time\n\nevents = pd.read_csv('../input/store-sales-time-series-forecasting/holidays_events.csv')\n\noil = pd.read_csv('../input/store-sales-time-series-forecasting/oil.csv')\n\nstores = pd.read_csv('../input/store-sales-time-series-forecasting/stores.csv')\n\ntest = pd.read_csv('../input/store-sales-time-series-forecasting/test.csv')\n\nsubmission = pd.read_csv('../input/store-sales-time-series-forecasting/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.246006Z","iopub.execute_input":"2022-07-02T21:52:35.246302Z","iopub.status.idle":"2022-07-02T21:52:35.306446Z","shell.execute_reply.started":"2022-07-02T21:52:35.246263Z","shell.execute_reply":"2022-07-02T21:52:35.305805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.307402Z","iopub.execute_input":"2022-07-02T21:52:35.308105Z","iopub.status.idle":"2022-07-02T21:52:35.321147Z","shell.execute_reply.started":"2022-07-02T21:52:35.308056Z","shell.execute_reply":"2022-07-02T21:52:35.320299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events['date'] = pd.to_datetime(events.date) # change column type ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.322410Z","iopub.execute_input":"2022-07-02T21:52:35.323332Z","iopub.status.idle":"2022-07-02T21:52:35.328492Z","shell.execute_reply.started":"2022-07-02T21:52:35.323294Z","shell.execute_reply":"2022-07-02T21:52:35.327807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events['year'] = events.date.dt.year #create seperate year column to get an overview per year. ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.329576Z","iopub.execute_input":"2022-07-02T21:52:35.330053Z","iopub.status.idle":"2022-07-02T21:52:35.340138Z","shell.execute_reply.started":"2022-07-02T21:52:35.330019Z","shell.execute_reply":"2022-07-02T21:52:35.339345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events['month'] = events.date.dt.month  ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.341806Z","iopub.execute_input":"2022-07-02T21:52:35.342327Z","iopub.status.idle":"2022-07-02T21:52:35.355127Z","shell.execute_reply.started":"2022-07-02T21:52:35.342290Z","shell.execute_reply":"2022-07-02T21:52:35.354114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_type_value_counts = events.type.value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.356356Z","iopub.execute_input":"2022-07-02T21:52:35.356864Z","iopub.status.idle":"2022-07-02T21:52:35.370565Z","shell.execute_reply.started":"2022-07-02T21:52:35.356817Z","shell.execute_reply":"2022-07-02T21:52:35.369647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(events_type_value_counts, x='index', y='type',\n        labels=dict(index = \"Type\", type=\"Frequency\"))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.371607Z","iopub.execute_input":"2022-07-02T21:52:35.371859Z","iopub.status.idle":"2022-07-02T21:52:35.434189Z","shell.execute_reply.started":"2022-07-02T21:52:35.371831Z","shell.execute_reply":"2022-07-02T21:52:35.433161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_events = events.groupby(\"year\")['type'].count().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.435383Z","iopub.execute_input":"2022-07-02T21:52:35.435616Z","iopub.status.idle":"2022-07-02T21:52:35.443117Z","shell.execute_reply.started":"2022-07-02T21:52:35.435587Z","shell.execute_reply":"2022-07-02T21:52:35.442507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_events # count events per year ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.444402Z","iopub.execute_input":"2022-07-02T21:52:35.445399Z","iopub.status.idle":"2022-07-02T21:52:35.458754Z","shell.execute_reply.started":"2022-07-02T21:52:35.445353Z","shell.execute_reply":"2022-07-02T21:52:35.457821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(count_events, x='year', y='type', \n        labels= dict(type=\"Frequency of events\", year= 'Year'))","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.460376Z","iopub.execute_input":"2022-07-02T21:52:35.460896Z","iopub.status.idle":"2022-07-02T21:52:35.519494Z","shell.execute_reply.started":"2022-07-02T21:52:35.460846Z","shell.execute_reply":"2022-07-02T21:52:35.518943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>:Seems like the holidays per year is not equally distributed !!\n    We can look for more deeper look on the holidays :)</p>\n</div>","metadata":{}},{"cell_type":"code","source":"events.type.value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.520650Z","iopub.execute_input":"2022-07-02T21:52:35.521050Z","iopub.status.idle":"2022-07-02T21:52:35.530821Z","shell.execute_reply.started":"2022-07-02T21:52:35.521011Z","shell.execute_reply":"2022-07-02T21:52:35.529938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event = events[(events['type'] == 'Holiday') | (events['type'] == 'Event')] ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.531962Z","iopub.execute_input":"2022-07-02T21:52:35.532180Z","iopub.status.idle":"2022-07-02T21:52:35.544517Z","shell.execute_reply.started":"2022-07-02T21:52:35.532154Z","shell.execute_reply":"2022-07-02T21:52:35.543712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.546011Z","iopub.execute_input":"2022-07-02T21:52:35.546680Z","iopub.status.idle":"2022-07-02T21:52:35.566206Z","shell.execute_reply.started":"2022-07-02T21:52:35.546634Z","shell.execute_reply":"2022-07-02T21:52:35.565534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = holidays_or_event.groupby('year')['type'].value_counts()\nx.index","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.567206Z","iopub.execute_input":"2022-07-02T21:52:35.567535Z","iopub.status.idle":"2022-07-02T21:52:35.581218Z","shell.execute_reply.started":"2022-07-02T21:52:35.567506Z","shell.execute_reply":"2022-07-02T21:52:35.580288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event['month'] = holidays_or_event.date.dt.month \n#let's look for holidays per month to get more consice overview","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.582318Z","iopub.execute_input":"2022-07-02T21:52:35.582874Z","iopub.status.idle":"2022-07-02T21:52:35.589583Z","shell.execute_reply.started":"2022-07-02T21:52:35.582829Z","shell.execute_reply":"2022-07-02T21:52:35.588803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event_per_year_and_month = holidays_or_event.groupby(['year','month'])['type'].value_counts().reset_index(level=[0,1])\n\nholidays_or_event_per_year_and_month","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.590713Z","iopub.execute_input":"2022-07-02T21:52:35.591306Z","iopub.status.idle":"2022-07-02T21:52:35.617078Z","shell.execute_reply.started":"2022-07-02T21:52:35.591261Z","shell.execute_reply":"2022-07-02T21:52:35.616153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event_per_year_and_month.columns = ['Year', 'Month', 'Count']","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.618127Z","iopub.execute_input":"2022-07-02T21:52:35.618566Z","iopub.status.idle":"2022-07-02T21:52:35.622291Z","shell.execute_reply.started":"2022-07-02T21:52:35.618533Z","shell.execute_reply":"2022-07-02T21:52:35.621394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event_per_year_and_month = holidays_or_event_per_year_and_month.reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.623712Z","iopub.execute_input":"2022-07-02T21:52:35.623959Z","iopub.status.idle":"2022-07-02T21:52:35.637663Z","shell.execute_reply.started":"2022-07-02T21:52:35.623933Z","shell.execute_reply":"2022-07-02T21:52:35.636723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_or_event_per_year_and_month.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.638891Z","iopub.execute_input":"2022-07-02T21:52:35.639386Z","iopub.status.idle":"2022-07-02T21:52:35.652195Z","shell.execute_reply.started":"2022-07-02T21:52:35.639350Z","shell.execute_reply":"2022-07-02T21:52:35.651322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(holidays_or_event_per_year_and_month, y='Count', x='Month', color='Year')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.653279Z","iopub.execute_input":"2022-07-02T21:52:35.653485Z","iopub.status.idle":"2022-07-02T21:52:35.891841Z","shell.execute_reply.started":"2022-07-02T21:52:35.653460Z","shell.execute_reply":"2022-07-02T21:52:35.891070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(sales_per_month, x = 'month', y= 'sales', color='year')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.892946Z","iopub.execute_input":"2022-07-02T21:52:35.893268Z","iopub.status.idle":"2022-07-02T21:52:35.962320Z","shell.execute_reply.started":"2022-07-02T21:52:35.893240Z","shell.execute_reply":"2022-07-02T21:52:35.961613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#px.scatter(sales_per_month, x = 'month', y= 'sales', color='year')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.963527Z","iopub.execute_input":"2022-07-02T21:52:35.963909Z","iopub.status.idle":"2022-07-02T21:52:35.968125Z","shell.execute_reply.started":"2022-07-02T21:52:35.963875Z","shell.execute_reply":"2022-07-02T21:52:35.967253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.969218Z","iopub.execute_input":"2022-07-02T21:52:35.969585Z","iopub.status.idle":"2022-07-02T21:52:35.987506Z","shell.execute_reply.started":"2022-07-02T21:52:35.969553Z","shell.execute_reply":"2022-07-02T21:52:35.986573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try_this_one = events.groupby('date')['type'].count().reset_index()\ntry_this_one","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:35.988554Z","iopub.execute_input":"2022-07-02T21:52:35.988922Z","iopub.status.idle":"2022-07-02T21:52:36.002839Z","shell.execute_reply.started":"2022-07-02T21:52:35.988888Z","shell.execute_reply":"2022-07-02T21:52:36.002099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(try_this_one, x='date', y='type')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.003988Z","iopub.execute_input":"2022-07-02T21:52:36.004341Z","iopub.status.idle":"2022-07-02T21:52:36.065920Z","shell.execute_reply.started":"2022-07-02T21:52:36.004300Z","shell.execute_reply":"2022-07-02T21:52:36.065322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event_per_month = events.groupby(['year','month'])['type'].count().reset_index()\nevent_per_month","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.067071Z","iopub.execute_input":"2022-07-02T21:52:36.067438Z","iopub.status.idle":"2022-07-02T21:52:36.080637Z","shell.execute_reply.started":"2022-07-02T21:52:36.067399Z","shell.execute_reply":"2022-07-02T21:52:36.080079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(event_per_month, x='month', y='type',color='year',\n       labels = dict(month='Month', type='Frequency'))","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.081774Z","iopub.execute_input":"2022-07-02T21:52:36.082128Z","iopub.status.idle":"2022-07-02T21:52:36.155669Z","shell.execute_reply.started":"2022-07-02T21:52:36.082088Z","shell.execute_reply":"2022-07-02T21:52:36.154864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diff_events = events.locale.value_counts().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.156838Z","iopub.execute_input":"2022-07-02T21:52:36.157071Z","iopub.status.idle":"2022-07-02T21:52:36.164121Z","shell.execute_reply.started":"2022-07-02T21:52:36.157044Z","shell.execute_reply":"2022-07-02T21:52:36.163086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(diff_events, x = 'index', y='locale', \n       labels = dict(index='Event Type', locale='Frequency'))","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.165553Z","iopub.execute_input":"2022-07-02T21:52:36.166167Z","iopub.status.idle":"2022-07-02T21:52:36.226892Z","shell.execute_reply.started":"2022-07-02T21:52:36.166125Z","shell.execute_reply":"2022-07-02T21:52:36.226106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1.3 oil dataset \n\n<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>: We could use oil prices for further investgations by joining this dataset to train dataset. </p>\n</div>","metadata":{}},{"cell_type":"code","source":"oil.head() \n# this dataset give us more information about oil price in a given specific date ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.228214Z","iopub.execute_input":"2022-07-02T21:52:36.228571Z","iopub.status.idle":"2022-07-02T21:52:36.237789Z","shell.execute_reply.started":"2022-07-02T21:52:36.228540Z","shell.execute_reply":"2022-07-02T21:52:36.236840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oil['date'] = pd.to_datetime(oil.date)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.239260Z","iopub.execute_input":"2022-07-02T21:52:36.239498Z","iopub.status.idle":"2022-07-02T21:52:36.252430Z","shell.execute_reply.started":"2022-07-02T21:52:36.239471Z","shell.execute_reply":"2022-07-02T21:52:36.251362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(oil, x='date', y='dcoilwtico', labels=dict(date='Date', dcoilwtico='Oil Price'), title ='Oil price per month')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.253632Z","iopub.execute_input":"2022-07-02T21:52:36.254420Z","iopub.status.idle":"2022-07-02T21:52:36.360253Z","shell.execute_reply.started":"2022-07-02T21:52:36.254366Z","shell.execute_reply":"2022-07-02T21:52:36.359227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = 'Further data to analyze'></a>\n\n# 1.4 Deeper analysis by merging all avaliable datasets  \n","metadata":{}},{"cell_type":"code","source":"events.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.361466Z","iopub.execute_input":"2022-07-02T21:52:36.361707Z","iopub.status.idle":"2022-07-02T21:52:36.374985Z","shell.execute_reply.started":"2022-07-02T21:52:36.361679Z","shell.execute_reply":"2022-07-02T21:52:36.374055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_new = events.drop(['year','month', 'locale', 'locale_name','description', 'transferred'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.376452Z","iopub.execute_input":"2022-07-02T21:52:36.377429Z","iopub.status.idle":"2022-07-02T21:52:36.387266Z","shell.execute_reply.started":"2022-07-02T21:52:36.377381Z","shell.execute_reply":"2022-07-02T21:52:36.386374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> We previously managed to investgate on **events** dataset alone but we can merge it with other datasets to make more complex and deeper knowledge.","metadata":{}},{"cell_type":"code","source":"train_stores = train.merge(stores, how='left', on='store_nbr')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:36.388494Z","iopub.execute_input":"2022-07-02T21:52:36.389303Z","iopub.status.idle":"2022-07-02T21:52:37.008113Z","shell.execute_reply.started":"2022-07-02T21:52:36.389257Z","shell.execute_reply":"2022-07-02T21:52:37.007253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:37.009242Z","iopub.execute_input":"2022-07-02T21:52:37.009528Z","iopub.status.idle":"2022-07-02T21:52:37.023078Z","shell.execute_reply.started":"2022-07-02T21:52:37.009498Z","shell.execute_reply":"2022-07-02T21:52:37.022003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_new = events.drop(['locale','locale_name', 'description', 'transferred', 'year', 'month'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:37.025057Z","iopub.execute_input":"2022-07-02T21:52:37.025331Z","iopub.status.idle":"2022-07-02T21:52:37.032336Z","shell.execute_reply.started":"2022-07-02T21:52:37.025301Z","shell.execute_reply":"2022-07-02T21:52:37.031685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train_stores.merge(events_new, how='left', on= 'date')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:37.033419Z","iopub.execute_input":"2022-07-02T21:52:37.034061Z","iopub.status.idle":"2022-07-02T21:52:37.700712Z","shell.execute_reply.started":"2022-07-02T21:52:37.034016Z","shell.execute_reply":"2022-07-02T21:52:37.699943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.merge(oil, how ='left', on='date')\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:37.702009Z","iopub.execute_input":"2022-07-02T21:52:37.702240Z","iopub.status.idle":"2022-07-02T21:52:38.401570Z","shell.execute_reply.started":"2022-07-02T21:52:37.702213Z","shell.execute_reply":"2022-07-02T21:52:38.400768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+f\"the overall dateset rows is {data.shape[0]} with {data.shape[1]} coulmns.\"+clr.E)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:38.403101Z","iopub.execute_input":"2022-07-02T21:52:38.403613Z","iopub.status.idle":"2022-07-02T21:52:38.410541Z","shell.execute_reply.started":"2022-07-02T21:52:38.403568Z","shell.execute_reply":"2022-07-02T21:52:38.409484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_family_per_city = data.groupby(['city','family'])['sales'].sum()\nsales_per_family_per_city","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:38.411946Z","iopub.execute_input":"2022-07-02T21:52:38.412587Z","iopub.status.idle":"2022-07-02T21:52:38.869082Z","shell.execute_reply.started":"2022-07-02T21:52:38.412531Z","shell.execute_reply":"2022-07-02T21:52:38.868058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_family_per_city = sales_per_family_per_city.reset_index(level=[0,1])\nsales_per_family_per_city ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:38.870089Z","iopub.execute_input":"2022-07-02T21:52:38.870304Z","iopub.status.idle":"2022-07-02T21:52:38.886069Z","shell.execute_reply.started":"2022-07-02T21:52:38.870279Z","shell.execute_reply":"2022-07-02T21:52:38.885246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(sales_per_family_per_city, y='sales', x='city', color='family')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:38.894048Z","iopub.execute_input":"2022-07-02T21:52:38.894309Z","iopub.status.idle":"2022-07-02T21:52:39.075951Z","shell.execute_reply.started":"2022-07-02T21:52:38.894283Z","shell.execute_reply":"2022-07-02T21:52:39.075111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:39.077335Z","iopub.execute_input":"2022-07-02T21:52:39.077548Z","iopub.status.idle":"2022-07-02T21:52:39.092248Z","shell.execute_reply.started":"2022-07-02T21:52:39.077522Z","shell.execute_reply":"2022-07-02T21:52:39.091616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores_per_city = data.groupby('city')['store_nbr'].count().reset_index()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:39.093389Z","iopub.execute_input":"2022-07-02T21:52:39.093899Z","iopub.status.idle":"2022-07-02T21:52:39.298604Z","shell.execute_reply.started":"2022-07-02T21:52:39.093860Z","shell.execute_reply":"2022-07-02T21:52:39.297795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores_per_city.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:39.299598Z","iopub.execute_input":"2022-07-02T21:52:39.299843Z","iopub.status.idle":"2022-07-02T21:52:39.310108Z","shell.execute_reply.started":"2022-07-02T21:52:39.299813Z","shell.execute_reply":"2022-07-02T21:52:39.309214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(stores_per_city, y='store_nbr', x ='city')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:39.311024Z","iopub.execute_input":"2022-07-02T21:52:39.311225Z","iopub.status.idle":"2022-07-02T21:52:39.370249Z","shell.execute_reply.started":"2022-07-02T21:52:39.311201Z","shell.execute_reply":"2022-07-02T21:52:39.369703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Seems to me some of our categorical features that have the impact on sales are the following:\n\n* State \n* family \n* city\n* type_x\n* type_y ","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:39.371010Z","iopub.execute_input":"2022-07-02T21:52:39.371211Z","iopub.status.idle":"2022-07-02T21:52:39.386706Z","shell.execute_reply.started":"2022-07-02T21:52:39.371187Z","shell.execute_reply":"2022-07-02T21:52:39.386039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['week'] = data['date'].dt.isocalendar().week\ndata['quarter'] = data['date'].dt.quarter\ndata['day_of_week'] = data['date'].dt.day_name()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:39.387627Z","iopub.execute_input":"2022-07-02T21:52:39.387902Z","iopub.status.idle":"2022-07-02T21:52:41.305330Z","shell.execute_reply.started":"2022-07-02T21:52:39.387873Z","shell.execute_reply":"2022-07-02T21:52:41.304326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['month_name'] = data['date'].dt.strftime('%b')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:41.306622Z","iopub.execute_input":"2022-07-02T21:52:41.306888Z","iopub.status.idle":"2022-07-02T21:52:56.215773Z","shell.execute_reply.started":"2022-07-02T21:52:41.306857Z","shell.execute_reply":"2022-07-02T21:52:56.215117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:56.217103Z","iopub.execute_input":"2022-07-02T21:52:56.217569Z","iopub.status.idle":"2022-07-02T21:52:56.237479Z","shell.execute_reply.started":"2022-07-02T21:52:56.217525Z","shell.execute_reply":"2022-07-02T21:52:56.236639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_store_sales = data.groupby('type_x').agg({\"sales\" : \"mean\"})\\\n     .reset_index()\\\n    .sort_values(by='sales', ascending=False)\n\ndf_fam_sales = data.groupby('family').agg({\"sales\" : \"mean\"})\\\n    .reset_index()\\\n    .sort_values(by='sales', ascending=False)[:10]\n\ndf_clus_sales = data.groupby('month_name').agg({\"sales\" : \"mean\"})\\\n    .reset_index() ","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:56.238775Z","iopub.execute_input":"2022-07-02T21:52:56.239076Z","iopub.status.idle":"2022-07-02T21:52:56.843999Z","shell.execute_reply.started":"2022-07-02T21:52:56.239035Z","shell.execute_reply":"2022-07-02T21:52:56.842959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(px.colors.diverging.RdYlGn.reverse)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:56.845501Z","iopub.execute_input":"2022-07-02T21:52:56.845703Z","iopub.status.idle":"2022-07-02T21:52:56.850627Z","shell.execute_reply.started":"2022-07-02T21:52:56.845678Z","shell.execute_reply":"2022-07-02T21:52:56.849671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors = px.colors.sequential.amp_r\nfig = make_subplots(rows=2, cols=2, \n                    specs=[[{\"type\": \"bar\"}, {\"type\": \"pie\"}],\n                           [{\"colspan\": 2}, None]],\n                    column_widths=[0.7, 0.3], vertical_spacing=0, horizontal_spacing=0.02,\n                    subplot_titles=(\"Top 10 Highest Product Sales\", \"Highest Sales in Stores\", \"Months Vs Sales (in millions)\"))\n\nfig.add_trace(go.Bar(x=df_fam_sales['sales'], y=df_fam_sales['family'], marker=dict(color= colors),\n                     name='Family', orientation='h'), \n                     row=1, col=1)\nfig.add_trace(go.Pie(values=df_store_sales['sales'], labels=df_store_sales['type_x'], name='Store type',\n                     marker=dict(colors=colors), hole=0.7,\n                     hoverinfo='label+percent+value', textinfo='label'), \n                    row=1, col=2)\nfig.add_trace(go.Bar(x=df_clus_sales['month_name'], y=df_clus_sales['sales'], \n                     marker=dict(color= px.colors.sequential.amp[3]), name='Months'), \n                     row=2, col=1)\n\n# styling -------------------------------------------------------------------------------\nfig.update_yaxes(showgrid=False, ticksuffix=' ', categoryorder='total ascending', row=1, col=1)\nfig.update_xaxes(visible=False, row=1, col=1)\nfig.update_xaxes(tickmode = 'array', tickvals=df_clus_sales.month_name,  row=2, col=1)\nfig.update_yaxes(visible=True, row=2, col=1, range=[0,700])\nfig.update_layout(height=600, bargap=0.2,\n                  margin=dict(b=0,r=20,l=20), xaxis=dict(tickmode='linear'),\n                  title_text=\"Average Sales Analysis\",\n                  template=\"plotly_white\",\n                  title_font=dict(size=25, color='#8a8d93', family=\"Lato, sans-serif\"),\n                  font=dict(color='#8a8d93'),\n                  hoverlabel=dict(bgcolor=\"#f2f2f2\", font_size=13, font_family=\"Lato, sans-serif\"),\n                  showlegend=False)\n\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:56.852200Z","iopub.execute_input":"2022-07-02T21:52:56.852651Z","iopub.status.idle":"2022-07-02T21:52:56.990944Z","shell.execute_reply.started":"2022-07-02T21:52:56.852605Z","shell.execute_reply":"2022-07-02T21:52:56.990040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_city  = stores.groupby('city') .size().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:56.991948Z","iopub.execute_input":"2022-07-02T21:52:56.992228Z","iopub.status.idle":"2022-07-02T21:52:56.999456Z","shell.execute_reply.started":"2022-07-02T21:52:56.992200Z","shell.execute_reply":"2022-07-02T21:52:56.998498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores_nbr_id = data.store_nbr.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.000767Z","iopub.execute_input":"2022-07-02T21:52:57.001479Z","iopub.status.idle":"2022-07-02T21:52:57.028411Z","shell.execute_reply.started":"2022-07-02T21:52:57.001425Z","shell.execute_reply":"2022-07-02T21:52:57.027535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stores_nbr_id","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.030468Z","iopub.execute_input":"2022-07-02T21:52:57.030805Z","iopub.status.idle":"2022-07-02T21:52:57.037053Z","shell.execute_reply.started":"2022-07-02T21:52:57.030762Z","shell.execute_reply":"2022-07-02T21:52:57.036185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_grouped = data.groupby(['store_nbr','date']).agg({'sales':'sum'})\nsales_by_store_id = {}\nfor store in range(1,len(stores_nbr_id)):\n    sales_by_store_id[store] = {'sale': sales_grouped.loc[store].sales.mean(),\n                                'city': stores.city[store],\n                                'city-id': stores.city[store]+\"-{}\".format(store) ,\n                                'state': stores.state[store] ,\n                                'type': stores.type[store] ,\n                                'cluster': stores.cluster[store] }\nsales_by_store_id = pd.DataFrame.from_dict(sales_by_store_id, orient='index') # convert to Pandas DataFrame \n\nsales_by_city      = {}\nfor city in top_city.index.values:\n    sales_by_city[city]      = {'sum': sales_by_store_id [ stores.city==city ].sale.sum(),\n                                'mean': sales_by_store_id [ stores.city==city ].sale.mean() }\nsales_by_city      = pd.DataFrame.from_dict(sales_by_city, orient='index')    \n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.038594Z","iopub.execute_input":"2022-07-02T21:52:57.039125Z","iopub.status.idle":"2022-07-02T21:52:57.296239Z","shell.execute_reply.started":"2022-07-02T21:52:57.039069Z","shell.execute_reply":"2022-07-02T21:52:57.295500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sorting\nsales_by_store_id.sort_values( by='sale', inplace=True, ascending=False )\nsales_by_city_sum  = sales_by_city.sort_values( by='sum', ascending=False )","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.297349Z","iopub.execute_input":"2022-07-02T21:52:57.297557Z","iopub.status.idle":"2022-07-02T21:52:57.303593Z","shell.execute_reply.started":"2022-07-02T21:52:57.297532Z","shell.execute_reply":"2022-07-02T21:52:57.302629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_by_store_id.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.304935Z","iopub.execute_input":"2022-07-02T21:52:57.305215Z","iopub.status.idle":"2022-07-02T21:52:57.323536Z","shell.execute_reply.started":"2022-07-02T21:52:57.305171Z","shell.execute_reply":"2022-07-02T21:52:57.322514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.bar(sales_by_store_id, x='city-id', y='sale', title= 'Average sales per given store')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.324754Z","iopub.execute_input":"2022-07-02T21:52:57.325000Z","iopub.status.idle":"2022-07-02T21:52:57.383666Z","shell.execute_reply.started":"2022-07-02T21:52:57.324971Z","shell.execute_reply":"2022-07-02T21:52:57.382706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_by_store_id.sale.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.384928Z","iopub.execute_input":"2022-07-02T21:52:57.385157Z","iopub.status.idle":"2022-07-02T21:52:57.391392Z","shell.execute_reply.started":"2022-07-02T21:52:57.385128Z","shell.execute_reply":"2022-07-02T21:52:57.390633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_grouped  = data.groupby('date').agg({'sales':'sum'}).to_period(\"D\")\nsales_grouped['year']      = sales_grouped.index.year\nsales_grouped['quarter']   = sales_grouped.index.quarter\nsales_grouped['month']     = sales_grouped.index.month\nsales_grouped['week']      = sales_grouped.index.week\nsales_grouped['dayofweek'] = sales_grouped.index.dayofweek  # Monday=0, Sunday=6\nsales_grouped['dayofmonth']= sales_grouped.index.day  # day in month from 01 to 31\nsales_grouped['dayofyear'] = sales_grouped.index.dayofyear\n\nsales_smooth7  = sales_grouped.copy()\nsales_smooth30 = sales_grouped.copy()\nsales_smooth365= sales_grouped.copy()\n\nsales_smooth7[\"sales\"]   = sales_smooth7.  sales.rolling(window=7,  center=True, min_periods=3 ).mean()\nsales_smooth30[\"sales\"]  = sales_smooth30. sales.rolling(window=30, center=True, min_periods=15).mean()\nsales_smooth365[\"sales\"] = sales_smooth365.sales.rolling(window=365,center=True, min_periods=183).mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.392290Z","iopub.execute_input":"2022-07-02T21:52:57.392511Z","iopub.status.idle":"2022-07-02T21:52:57.476502Z","shell.execute_reply.started":"2022-07-02T21:52:57.392484Z","shell.execute_reply":"2022-07-02T21:52:57.475757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figsize = (14,5)\n\nfig, (ax1,ax2) = plt.subplots(1,2,figsize=figsize)\n\nsales_grouped.groupby(['month']).agg({'sales':'mean'}).plot(kind=\"barh\",ax=ax1)\n\nax1.set(title=\"Average Sales by Month\")\nax1.set(ylabel=\"Month\", xlabel=\"Average Sales\")\nax1.get_legend().remove()\n\nlabels1 = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']\n\nax1.set_yticks(range(0,12), labels1)\n\nsales_grouped.groupby(['quarter']).agg({'sales':'mean'}).plot.pie(y=\"sales\",ax=ax2, legend=False, autopct='%1.f%%',\n                             startangle=90, labels=[\"Quarter 1\",\"Quarter 2\",\"Quarter 3\",\"Quarter 4\"], fontsize=\"x-large\")\nax2.set(title=\"Average Sales by Quarter\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.477767Z","iopub.execute_input":"2022-07-02T21:52:57.478529Z","iopub.status.idle":"2022-07-02T21:52:57.822919Z","shell.execute_reply.started":"2022-07-02T21:52:57.478485Z","shell.execute_reply":"2022-07-02T21:52:57.821927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>: Most of the sales occurred in December. People tend to stock for the end of the year to prepare for a new year or to decorate the house, or schedule for a long holiday starting from Christmas. </p>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert simple-alert\">\n  <p>💡<b>Note</b>: Although the sales in each quarter are very similar, the last (4th) quarter is the biggist, which agrees to the sales by month where there is a jump in December. </p>\n</div>","metadata":{}},{"cell_type":"code","source":"top_stores_2017 = data.loc[data.date.dt.year==2017].groupby(\"store_nbr\").agg({\"sales\":\"sum\"})\ntop_stores_2017 = pd.merge(top_stores_2017, stores, on=\"store_nbr\").drop([\"city\",\"state\"],axis=1)\n\nfigsize = (15,5)\nfig, (ax1,ax2) = plt.subplots(1,2,figsize=figsize)\ntop_stores_2017.groupby(['type']).agg({'sales':'mean'}).plot.pie(y=\"sales\",ax=ax1, legend=False, autopct='%1.f%%',\n                             startangle=90, labels=[\"Type A\",\"Type B\",\"Type C\",\"Type D\",\"Type E\"], fontsize=\"x-large\")\nax1.set(title=\"Average Sales by Type\")\n\ntop_stores_2017.groupby(['cluster']).agg({'sales':'mean'}).plot.bar(ax=ax2,  fontsize=\"large\")\nax2.set(title=\"Average Sales by Cluster\")\nax2.set(ylabel=\"Average Sales\", xlabel=\"Cluster\")\nax2.get_legend().remove()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:57.824962Z","iopub.execute_input":"2022-07-02T21:52:57.825578Z","iopub.status.idle":"2022-07-02T21:52:59.352472Z","shell.execute_reply.started":"2022-07-02T21:52:57.825300Z","shell.execute_reply":"2022-07-02T21:52:59.351597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert simple-alert\">\n  💡<b>Note</b>: \n    \n* Store Type A has the highest sales which is 37%, followed by Types B and D.\n\n* Store Cluster 5 has the highest sales, followed by Clusters 11, 14, and 8. \n</div>","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Scaling of Numerical Features","metadata":{}},{"cell_type":"code","source":"cols = list(data[['sales', 'onpromotion']].columns)\ncols","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:59.353591Z","iopub.execute_input":"2022-07-02T21:52:59.353850Z","iopub.status.idle":"2022-07-02T21:52:59.371602Z","shell.execute_reply.started":"2022-07-02T21:52:59.353819Z","shell.execute_reply":"2022-07-02T21:52:59.370641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = MinMaxScaler(feature_range=(0,1))\n\ntrain[cols] = scaler.fit_transform(train[cols])","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:59.373196Z","iopub.execute_input":"2022-07-02T21:52:59.373598Z","iopub.status.idle":"2022-07-02T21:52:59.505597Z","shell.execute_reply.started":"2022-07-02T21:52:59.373511Z","shell.execute_reply":"2022-07-02T21:52:59.504669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_date = data[cols]","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:59.506783Z","iopub.execute_input":"2022-07-02T21:52:59.507023Z","iopub.status.idle":"2022-07-02T21:52:59.520929Z","shell.execute_reply.started":"2022-07-02T21:52:59.506994Z","shell.execute_reply":"2022-07-02T21:52:59.520273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = data.groupby(['year']).agg({'sales':'mean', 'onpromotion':'mean'}).reset_index()\ntrain_data.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:59.522053Z","iopub.execute_input":"2022-07-02T21:52:59.522380Z","iopub.status.idle":"2022-07-02T21:52:59.632371Z","shell.execute_reply.started":"2022-07-02T21:52:59.522351Z","shell.execute_reply":"2022-07-02T21:52:59.631458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.scatter(train_data, x ='sales', y='onpromotion', trendline='ols', color='year' )","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:52:59.633698Z","iopub.execute_input":"2022-07-02T21:52:59.633926Z","iopub.status.idle":"2022-07-02T21:53:00.235101Z","shell.execute_reply.started":"2022-07-02T21:52:59.633900Z","shell.execute_reply":"2022-07-02T21:53:00.234136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.corrcoef(train_data.sales, train_data.onpromotion)[0][1]","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.236304Z","iopub.execute_input":"2022-07-02T21:53:00.236548Z","iopub.status.idle":"2022-07-02T21:53:00.246466Z","shell.execute_reply.started":"2022-07-02T21:53:00.236520Z","shell.execute_reply":"2022-07-02T21:53:00.245366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.247855Z","iopub.execute_input":"2022-07-02T21:53:00.248232Z","iopub.status.idle":"2022-07-02T21:53:00.268429Z","shell.execute_reply.started":"2022-07-02T21:53:00.248190Z","shell.execute_reply":"2022-07-02T21:53:00.267818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_oil_price = data.groupby(['year']).agg({'sales':'mean', 'dcoilwtico':'mean'}).reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.269820Z","iopub.execute_input":"2022-07-02T21:53:00.270082Z","iopub.status.idle":"2022-07-02T21:53:00.360218Z","shell.execute_reply.started":"2022-07-02T21:53:00.270051Z","shell.execute_reply":"2022-07-02T21:53:00.359062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_per_oil_price","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.361699Z","iopub.execute_input":"2022-07-02T21:53:00.362006Z","iopub.status.idle":"2022-07-02T21:53:00.372911Z","shell.execute_reply.started":"2022-07-02T21:53:00.361971Z","shell.execute_reply":"2022-07-02T21:53:00.371961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.scatter(sales_per_oil_price, y='dcoilwtico', x='sales', color='year', labels=dict(dcoilwtico='Avg Oil Price', sales='Avg Sales'))","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.374065Z","iopub.execute_input":"2022-07-02T21:53:00.374446Z","iopub.status.idle":"2022-07-02T21:53:00.442034Z","shell.execute_reply.started":"2022-07-02T21:53:00.374404Z","shell.execute_reply":"2022-07-02T21:53:00.441123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.corrcoef(train_data.sales, train_data.onpromotion)[0][1]","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.443365Z","iopub.execute_input":"2022-07-02T21:53:00.443594Z","iopub.status.idle":"2022-07-02T21:53:00.449727Z","shell.execute_reply.started":"2022-07-02T21:53:00.443566Z","shell.execute_reply":"2022-07-02T21:53:00.448826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Modeling 🏋️‍\n\n<img src='../input/photos-/2022-05-28 (2).png'></img>\n\n\n>  Of course there's a lot of choices the open source community, scikit-learn, scipy, tensorflow, pytorch, mxnet.\n\n> They should be popular, algorithms that are in demand. ","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.450874Z","iopub.execute_input":"2022-07-02T21:53:00.451812Z","iopub.status.idle":"2022-07-02T21:53:00.470905Z","shell.execute_reply.started":"2022-07-02T21:53:00.451766Z","shell.execute_reply":"2022-07-02T21:53:00.469954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['date'] = pd.to_datetime(train['date'])\ntrain = train.set_index('date')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.472053Z","iopub.execute_input":"2022-07-02T21:53:00.472763Z","iopub.status.idle":"2022-07-02T21:53:00.650745Z","shell.execute_reply.started":"2022-07-02T21:53:00.472705Z","shell.execute_reply":"2022-07-02T21:53:00.649718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_5_categories.family.values","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.651970Z","iopub.execute_input":"2022-07-02T21:53:00.652213Z","iopub.status.idle":"2022-07-02T21:53:00.658913Z","shell.execute_reply.started":"2022-07-02T21:53:00.652185Z","shell.execute_reply":"2022-07-02T21:53:00.658144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_5 = train[train['family'].isin(['GROCERY I', 'BEVERAGES', 'PRODUCE', 'CLEANING', 'DAIRY'])][['family','sales']]\ntop_5","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.659854Z","iopub.execute_input":"2022-07-02T21:53:00.660718Z","iopub.status.idle":"2022-07-02T21:53:00.886212Z","shell.execute_reply.started":"2022-07-02T21:53:00.660675Z","shell.execute_reply":"2022-07-02T21:53:00.885307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will work on the last year (last date = 2017-08-15)\ngrocery = top_5[top_5['family']=='GROCERY I'].groupby('date')['sales'].mean()#.resample('7D').mean()\ngrocery = grocery['2016-08-15':]\ngrocery","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.887491Z","iopub.execute_input":"2022-07-02T21:53:00.887896Z","iopub.status.idle":"2022-07-02T21:53:00.939002Z","shell.execute_reply.started":"2022-07-02T21:53:00.887851Z","shell.execute_reply":"2022-07-02T21:53:00.938062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grocery_re = grocery.reset_index()\ngrocery_re","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.940343Z","iopub.execute_input":"2022-07-02T21:53:00.941037Z","iopub.status.idle":"2022-07-02T21:53:00.955759Z","shell.execute_reply.started":"2022-07-02T21:53:00.940989Z","shell.execute_reply":"2022-07-02T21:53:00.954839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.2 Testing Stationarity \n\nOur time series data **Can have a trend or not**.It is of the utmost importance to determine how the series is behaving before applying any model to it. \n\n> **Augmented Dickey Fuller test**: it determines how strongly a time series is defined by a trend. \n\n#### Hypothesis:\n\n<div class =\"alert simple-alert\"><p> 1. Null Hypothesis (H0): Null Hypothesis of the test is that the time series can be represented by a unit root that <b>is not stationary</b>.\n\n2. Alternative Hypothesis (H1): Alternative Hypothesis of the test is that the time series **is stationary**.</p> </div>\n\n#### Why is Stationary Important?\n\n<div class = \"alert success-alert\"><p>For data to be stationary, the statistical properties of a system <b>do not change over time</b>. This does not mean that the values for each data point have to be the same, but the overall behavior of the data should remain constant.\n\nIf the data is non-stationary (meaning it has a trend), we need to **remove** it in order to proceed with the analysis.</p></div> ","metadata":{}},{"cell_type":"code","source":"sales = pd.Series(grocery_re['sales'].to_numpy())\n\ntest_stationarity(sales, log='non-stationary')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:00.956666Z","iopub.execute_input":"2022-07-02T21:53:00.957588Z","iopub.status.idle":"2022-07-02T21:53:01.268105Z","shell.execute_reply.started":"2022-07-02T21:53:00.957540Z","shell.execute_reply":"2022-07-02T21:53:01.267440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert danger-alert\">\n  <p>💡<i>Note</i>: If our data have trends (is non-stationary we could remove this trending by using <b>natural log</b> to our series</p>\n</div>","metadata":{}},{"cell_type":"code","source":"prediction_period = 30 # last 1 month\n\n# Set Train data to be uplo ( Total data length - prediction_period )\ndf_train= grocery[:len(grocery)-prediction_period]\n\n\n# Set Test data to be the last prediction_period\ndf_test= grocery[len(grocery)-prediction_period:]","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.269020Z","iopub.execute_input":"2022-07-02T21:53:01.269652Z","iopub.status.idle":"2022-07-02T21:53:01.274794Z","shell.execute_reply.started":"2022-07-02T21:53:01.269609Z","shell.execute_reply":"2022-07-02T21:53:01.274058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape, df_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.276003Z","iopub.execute_input":"2022-07-02T21:53:01.276684Z","iopub.status.idle":"2022-07-02T21:53:01.288856Z","shell.execute_reply.started":"2022-07-02T21:53:01.276627Z","shell.execute_reply":"2022-07-02T21:53:01.288124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nplt.plot(df_train)\nplt.plot(df_test)\nplt.legend(['TRAIN','TEST'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.290624Z","iopub.execute_input":"2022-07-02T21:53:01.291446Z","iopub.status.idle":"2022-07-02T21:53:01.540210Z","shell.execute_reply.started":"2022-07-02T21:53:01.291399Z","shell.execute_reply":"2022-07-02T21:53:01.539248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.541335Z","iopub.execute_input":"2022-07-02T21:53:01.541594Z","iopub.status.idle":"2022-07-02T21:53:01.545553Z","shell.execute_reply.started":"2022-07-02T21:53:01.541562Z","shell.execute_reply":"2022-07-02T21:53:01.544478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.546945Z","iopub.execute_input":"2022-07-02T21:53:01.547235Z","iopub.status.idle":"2022-07-02T21:53:01.558624Z","shell.execute_reply.started":"2022-07-02T21:53:01.547197Z","shell.execute_reply":"2022-07-02T21:53:01.557964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.560082Z","iopub.execute_input":"2022-07-02T21:53:01.560963Z","iopub.status.idle":"2022-07-02T21:53:01.570307Z","shell.execute_reply.started":"2022-07-02T21:53:01.560918Z","shell.execute_reply":"2022-07-02T21:53:01.569712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def plot_co2(df_train, df_test, y_pred, title):\n#     mae = mean_absolute_error(df_test, y_pred)\n#     #plt.plot(df_train), legend=True, label=\"TRAIN\", title=f\"{title}, MAE: {round(mae,2)}\")\n#     #plt.plot(df_test), legend=True, label=\"TEST\", figsize=(6, 4))\n#     #plt.plot(y_pred), legend=True, label=\"PREDICTION\")\n#     plt.figure(figsize=(16,6))\n#     plt.title(f\"{title}, MAE: {round(mae,2)}\")\n#     sns.lineplot(x = df_train.reset_index().date[250:], y = df_train.reset_index().sales[250:])\n#     sns.lineplot(x = df_test.reset_index().date, y = df_test.reset_index().sales)\n#     sns.lineplot(x = df_test.reset_index().date, y = y_pred.values)\n#     plt.xticks(rotation=45)\n#     plt.legend(['TRAIN','TEST','PREDICTION'])\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.571553Z","iopub.execute_input":"2022-07-02T21:53:01.571895Z","iopub.status.idle":"2022-07-02T21:53:01.582306Z","shell.execute_reply.started":"2022-07-02T21:53:01.571854Z","shell.execute_reply":"2022-07-02T21:53:01.581748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Configuring a SARIMA requires selecting hyperparameters for both the trend and seasonal elements of the series.\n\n#### Trend Elements\nThere are three trend elements that require configuration.\n\nThey are the same as the ARIMA model; specifically:\n\n- p: Trend autoregression order.\n- d: Trend difference order.\n- q: Trend moving average order.\n\n#### Seasonal Elements\nThere are four seasonal elements that are not part of ARIMA that must be configured; they are:\n\n- P: Seasonal autoregressive order.\n- D: Seasonal difference order.\n- Q: Seasonal moving average order.\n- m: The number of time steps for a single seasonal period.","metadata":{}},{"cell_type":"code","source":"#p = d = q = range(0, 4)\n# p = range(0,3)\n# q = range(0,3)\n# d = range(0,2)\n# s = range(1,8)\n\n# pdq = list(itertools.product(p, d, q))\n# seasonal_pdq = list(itertools.product(p, d, q, s))\n\n\n# def sarima_optimizer_mae(train, pdq, seasonal_pdq):\n#     best_mae, best_order, best_seasonal_order = float(\"inf\"), float(\"inf\"), None\n#     for param in pdq:\n#         for param_seasonal in seasonal_pdq:\n#             try:\n#                 model = SARIMAX(train, order=param, seasonal_order=param_seasonal)\n#                 sarima_model = model.fit(disp=0)\n#                 y_pred_test = sarima_model.get_forecast(steps=30)\n#                 y_pred = y_pred_test.predicted_mean\n#                 mae = mean_absolute_error(df_test, y_pred)\n\n#                 # mae = fit_model_sarima(train, val, param, param_seasonal)\n\n#                 if mae < best_mae:\n#                     best_mae, best_order, best_seasonal_order = mae, param, param_seasonal\n#                 print('SARIMA{}x{}12 - MAE:{}'.format(param, param_seasonal, mae))\n#             except:\n#                 continue\n#     print('SARIMA{}x{}12 - MAE:{}'.format(best_order, best_seasonal_order, best_mae))\n#     return best_order, best_seasonal_order\n\n# best_order, best_seasonal_order = sarima_optimizer_mae(df_train, pdq, seasonal_pdq)\n\n# model = SARIMAX(df_train, order=best_order, seasonal_order=best_seasonal_order)\n# sarima_final_model = model.fit(disp=0)\n\n# y_pred_test = sarima_final_model.get_forecast(steps=30)\n# y_pred = y_pred_test.predicted_mean\n# y_pred = pd.Series(y_pred.values, index=df_test.index)\n\n\n# plot_co2(df_train, df_test, y_pred, \"SARIMA\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.583586Z","iopub.execute_input":"2022-07-02T21:53:01.583922Z","iopub.status.idle":"2022-07-02T21:53:01.595340Z","shell.execute_reply.started":"2022-07-02T21:53:01.583880Z","shell.execute_reply":"2022-07-02T21:53:01.594305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, ax = plt.subplots(2,1)\n# fig = sm.graphics.tsa.plot_acf(df_train, lags=30, ax=ax[0])\n# fig = sm.graphics.tsa.plot_pacf(df_train, lags=30, ax=ax[1])\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.596599Z","iopub.execute_input":"2022-07-02T21:53:01.597466Z","iopub.status.idle":"2022-07-02T21:53:01.608185Z","shell.execute_reply.started":"2022-07-02T21:53:01.597414Z","shell.execute_reply":"2022-07-02T21:53:01.607578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = SARIMAX(df_train, order=(2, 1, 1), seasonal_order=(2, 1, 1, 7))\n# sarima_final_model = model.fit(max_iter = 50, method = 'powell')\n\n# y_pred_test = sarima_final_model.get_forecast(steps=30)\n# y_pred = y_pred_test.predicted_mean\n# y_pred = pd.Series(y_pred.values, index=df_test.index)\n\n\n# plot_co2(df_train, df_test, y_pred, \"SARIMA\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.609756Z","iopub.execute_input":"2022-07-02T21:53:01.610336Z","iopub.status.idle":"2022-07-02T21:53:01.619350Z","shell.execute_reply.started":"2022-07-02T21:53:01.610291Z","shell.execute_reply":"2022-07-02T21:53:01.618624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from pmdarima import auto_arima\n# model = auto_arima(df_train.values, start_p=1, start_q=1,\n#                       test='adf',       # use adftest to find optimal 'd'\n#                       max_p=5, max_q=5, # maximum p and q\n#                       m=7,              # frequency of series\n#                       d=None,           # let model determine 'd'\n#                       seasonal=True,   # Seasonality\n#                       start_P=0, \n#                       D=1, \n#                       trace=True,\n#                       error_action='ignore',  \n#                       suppress_warnings=True, \n#                       stepwise=True)\n\n# print(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.620967Z","iopub.execute_input":"2022-07-02T21:53:01.621545Z","iopub.status.idle":"2022-07-02T21:53:01.634863Z","shell.execute_reply.started":"2022-07-02T21:53:01.621499Z","shell.execute_reply":"2022-07-02T21:53:01.633928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# n_periods = 30\n# fc, confint = model.predict(n_periods=n_periods, return_conf_int=True)\n# fc_series = pd.Series(fc, index=df_test.index)\n\n# plot_co2(df_train, df_test, fc_series, \"AUTO_ARIMA\")","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.636219Z","iopub.execute_input":"2022-07-02T21:53:01.636596Z","iopub.status.idle":"2022-07-02T21:53:01.647321Z","shell.execute_reply.started":"2022-07-02T21:53:01.636551Z","shell.execute_reply":"2022-07-02T21:53:01.646335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LSTM\n\nWe will frame the problem to take a window of the last so many number of days of data to predict the current days data.\n\nso we will define function `dataset_generator_lstm()` that will split the input sequence into windows of data for fitting a supervised learning model, like an LSTM\n\nFor example, if the sequence was:\n```\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10\n```\nThen the samples for training the model will look like:\n\n```\nInput               | Output\n1, 2, 3, 4, 5       6\n2, 3, 4, 5, 6       7\n3, 4, 5, 6, 7       8\n```\n\n#### Define look_back period\n\nA `“lookback period”` defines how many previous timesteps are used in order to predict the subsequent timestep.\n\nFor example if I set the lookback period is to 5, that that means that I am using the time steps at t-4, t-3, t-2, t-1, and t to predict the value at time t+1.\n```\nlookback = 5\n\nX_train, Y_train = dataset_generator_lstm(train, lookback)\n\nX_val, Y_val = dataset_generator_lstm(val, lookback)\n```","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def dataset_generator_lstm(dataset, look_back=5):\n#     # A “lookback period” defines the window-size of how many\n#     # previous timesteps are used in order to predict\n#     # the subsequent timestep. \n#     dataX, dataY = [], []\n    \n#     for i in range(len(dataset) - look_back):\n#         window_size_x = dataset[i:(i + look_back), 0]\n#         dataX.append(window_size_x)\n#         dataY.append(dataset[i + look_back, 0]) # this is the label or actual y-value\n#     return np.array(dataX), np.array(dataY)\n\n# trainX, trainY = dataset_generator_lstm(scaled_train)\n\n# testX, testY = dataset_generator_lstm(scaled_test)\n\n# print(\"trainX: \", trainX.shape)\n# print(\"trainY: \", trainY.shape)\n# print(\"testY: \", testX.shape)\n# print(\"testY\", testY.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.648715Z","iopub.execute_input":"2022-07-02T21:53:01.649183Z","iopub.status.idle":"2022-07-02T21:53:01.659214Z","shell.execute_reply.started":"2022-07-02T21:53:01.649132Z","shell.execute_reply":"2022-07-02T21:53:01.658458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainX","metadata":{"execution":{"iopub.status.busy":"2022-07-02T21:53:01.660415Z","iopub.execute_input":"2022-07-02T21:53:01.660808Z","iopub.status.idle":"2022-07-02T21:53:01.673258Z","shell.execute_reply.started":"2022-07-02T21:53:01.660765Z","shell.execute_reply":"2022-07-02T21:53:01.672516Z"},"trusted":true},"execution_count":null,"outputs":[]}]}