{"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":"# Table of contents\n1. [Importing Libraries](#importinglibraries)\n2. [Reading Data Flies](#readingdatafiles)\n3. [Data Cleaning and Preprocessing](#datacleaningpreprocessing)\n4. [EDA of The Data](#eda)","metadata":{}},{"cell_type":"markdown","source":"## Importing Libraries <a name=\"importinglibraries\"></a>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport calendar\nimport numpy as np\nimport seaborn as sns\nfrom IPython.display import IFrame\nfrom matplotlib import rcParams\nimport plotly.express as px\nfrom plotly.subplots import make_subplots\nimport plotly.figure_factory as ff\nimport plotly.offline as offline\nimport plotly.graph_objs as go\noffline.init_notebook_mode(connected = True)","metadata":{"id":"340adc3c","execution":{"iopub.status.busy":"2022-07-14T12:23:06.548039Z","iopub.execute_input":"2022-07-14T12:23:06.548450Z","iopub.status.idle":"2022-07-14T12:23:09.125887Z","shell.execute_reply.started":"2022-07-14T12:23:06.548368Z","shell.execute_reply":"2022-07-14T12:23:09.124724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading Data Flies <a name=\"readingdatafiles\"></a>","metadata":{}},{"cell_type":"code","source":"tr = pd.read_csv(\"../input/store-sales-time-series-forecasting/transactions.csv\")\noil = pd.read_csv(\"../input/store-sales-time-series-forecasting/oil.csv\")\nholi = pd.read_csv(\"../input/store-sales-time-series-forecasting/holidays_events.csv\")\nstores = pd.read_csv(\"../input/store-sales-time-series-forecasting/stores.csv\")\ntrain = pd.read_csv(\"../input/store-sales-time-series-forecasting/train.csv\")","metadata":{"id":"d9eb6aa4","execution":{"iopub.status.busy":"2022-07-14T12:23:09.127953Z","iopub.execute_input":"2022-07-14T12:23:09.129682Z","iopub.status.idle":"2022-07-14T12:23:11.848054Z","shell.execute_reply.started":"2022-07-14T12:23:09.129636Z","shell.execute_reply":"2022-07-14T12:23:11.846525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train.merge(oil, on=\"date\", how=\"left\")\ndf = df.merge(holi, on=\"date\", how=\"left\")\ndf = df.merge(tr, on=[\"date\", \"store_nbr\"], how=\"left\")\ndf = df.merge(stores, on=\"store_nbr\", how=\"left\")","metadata":{"id":"b212509d","execution":{"iopub.status.busy":"2022-07-14T12:23:11.849731Z","iopub.execute_input":"2022-07-14T12:23:11.850195Z","iopub.status.idle":"2022-07-14T12:23:14.861656Z","shell.execute_reply.started":"2022-07-14T12:23:11.850150Z","shell.execute_reply":"2022-07-14T12:23:14.860734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.rename(columns={\"type_x\": \"holiday_type\", \"type_y\": \"store\"})","metadata":{"id":"b5cda829","execution":{"iopub.status.busy":"2022-07-14T12:23:14.864821Z","iopub.execute_input":"2022-07-14T12:23:14.865548Z","iopub.status.idle":"2022-07-14T12:23:16.234128Z","shell.execute_reply.started":"2022-07-14T12:23:14.865501Z","shell.execute_reply":"2022-07-14T12:23:16.233028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"id":"54ef9a39","execution":{"iopub.status.busy":"2022-07-14T12:23:16.235721Z","iopub.execute_input":"2022-07-14T12:23:16.236167Z","iopub.status.idle":"2022-07-14T12:23:16.262679Z","shell.execute_reply.started":"2022-07-14T12:23:16.236127Z","shell.execute_reply":"2022-07-14T12:23:16.261572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"id":"b69ddcda","execution":{"iopub.status.busy":"2022-07-14T12:23:16.264102Z","iopub.execute_input":"2022-07-14T12:23:16.264843Z","iopub.status.idle":"2022-07-14T12:23:16.272265Z","shell.execute_reply.started":"2022-07-14T12:23:16.264798Z","shell.execute_reply":"2022-07-14T12:23:16.271186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"id":"9027f524","execution":{"iopub.status.busy":"2022-07-14T12:23:16.273852Z","iopub.execute_input":"2022-07-14T12:23:16.274403Z","iopub.status.idle":"2022-07-14T12:23:16.292833Z","shell.execute_reply.started":"2022-07-14T12:23:16.274361Z","shell.execute_reply":"2022-07-14T12:23:16.292041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Cleaning and Preprocessing <a name=\"datacleaningpreprocessing\"></a>","metadata":{}},{"cell_type":"code","source":"df[\"date\"] = pd.to_datetime(df[\"date\"])\ndf[\"month\"] = df[\"date\"].dt.month\ndf[\"year\"] = df[\"date\"].dt.year\ndf['week'] = df['date'].dt.isocalendar().week\ndf['quarter'] = df['date'].dt.quarter\ndf.head()","metadata":{"id":"6a1e2a64","execution":{"iopub.status.busy":"2022-07-14T12:23:16.294482Z","iopub.execute_input":"2022-07-14T12:23:16.295102Z","iopub.status.idle":"2022-07-14T12:23:18.568879Z","shell.execute_reply.started":"2022-07-14T12:23:16.295070Z","shell.execute_reply":"2022-07-14T12:23:18.567751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#percentage of Missing values\n(df.isna().sum()/df.shape[0])*100","metadata":{"id":"03ea996c","execution":{"iopub.status.busy":"2022-07-14T12:23:18.570322Z","iopub.execute_input":"2022-07-14T12:23:18.571266Z","iopub.status.idle":"2022-07-14T12:23:19.600791Z","shell.execute_reply.started":"2022-07-14T12:23:18.571227Z","shell.execute_reply":"2022-07-14T12:23:19.599644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating a function  to fill the null values of the data","metadata":{}},{"cell_type":"code","source":"def filling_na(df, column, type_=None):\n    \"\"\"\n    This fucntion for filling null values to work with the data properly\n    Parameters:\n    df: DataFrame to fill the na with\n    column: column which will fill the value in it\n    type_: type of data needed be filled\n\n    \"\"\"\n    if type_ == \"num\":\n        filling_list = df[column].dropna()\n        df[column] = df[column].fillna(pd.Series(np.random.choice(filling_list, size=len(df.index))))\n        \n    else:\n        filling_list = df[column].dropna().unique()\n        df[column] = df[column].fillna(pd.Series(np.random.choice(filling_list, size=len(df.index))))\n      \n    print(df[column].isna().sum)","metadata":{"id":"1bedb28e","execution":{"iopub.status.busy":"2022-07-14T12:23:19.605634Z","iopub.execute_input":"2022-07-14T12:23:19.605968Z","iopub.status.idle":"2022-07-14T12:23:19.613020Z","shell.execute_reply.started":"2022-07-14T12:23:19.605939Z","shell.execute_reply":"2022-07-14T12:23:19.612041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot a distribution figure for dcoilwtico column before filling the values\nplt.figure(figsize=(14,12))\nsns.distplot(df[\"dcoilwtico\"])","metadata":{"id":"HNEU7SQQ6evc","execution":{"iopub.status.busy":"2022-07-14T12:23:19.614364Z","iopub.execute_input":"2022-07-14T12:23:19.614781Z","iopub.status.idle":"2022-07-14T12:23:28.225036Z","shell.execute_reply.started":"2022-07-14T12:23:19.614741Z","shell.execute_reply":"2022-07-14T12:23:28.224240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fill the dcoilwtico column\nfilling_na(df, \"dcoilwtico\", \"num\")","metadata":{"id":"l4jrTOr661vI","execution":{"iopub.status.busy":"2022-07-14T12:23:28.226342Z","iopub.execute_input":"2022-07-14T12:23:28.226864Z","iopub.status.idle":"2022-07-14T12:23:28.387334Z","shell.execute_reply.started":"2022-07-14T12:23:28.226832Z","shell.execute_reply":"2022-07-14T12:23:28.386061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot a distribution figure for dcoilwtico column after filling the values\nplt.figure(figsize=(14,12))\nsns.distplot(df[\"dcoilwtico\"])","metadata":{"id":"itE7JfLM7REA","execution":{"iopub.status.busy":"2022-07-14T12:23:28.388788Z","iopub.execute_input":"2022-07-14T12:23:28.389228Z","iopub.status.idle":"2022-07-14T12:23:40.460391Z","shell.execute_reply.started":"2022-07-14T12:23:28.389184Z","shell.execute_reply":"2022-07-14T12:23:40.459161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot a distribution figure for holiday_type column before filling the values\nplt.figure(figsize=(14,12))\nsns.countplot(df[\"holiday_type\"])","metadata":{"id":"HhyL1I9r9Z80","execution":{"iopub.status.busy":"2022-07-14T12:23:40.462019Z","iopub.execute_input":"2022-07-14T12:23:40.462435Z","iopub.status.idle":"2022-07-14T12:23:41.675400Z","shell.execute_reply.started":"2022-07-14T12:23:40.462397Z","shell.execute_reply":"2022-07-14T12:23:41.674282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#filling the missing values for holiday_type\nfilling_na(df, \"holiday_type\")","metadata":{"id":"KD0Ghrsl9IJY","execution":{"iopub.status.busy":"2022-07-14T12:23:41.677212Z","iopub.execute_input":"2022-07-14T12:23:41.678417Z","iopub.status.idle":"2022-07-14T12:23:42.175652Z","shell.execute_reply.started":"2022-07-14T12:23:41.678371Z","shell.execute_reply":"2022-07-14T12:23:42.174390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot a distribution figure for holiday_type column after filling the values\nplt.figure(figsize=(14,12))\nsns.countplot(df[\"holiday_type\"])","metadata":{"id":"Lds8eew895nt","execution":{"iopub.status.busy":"2022-07-14T12:23:42.176880Z","iopub.execute_input":"2022-07-14T12:23:42.177221Z","iopub.status.idle":"2022-07-14T12:23:44.044346Z","shell.execute_reply.started":"2022-07-14T12:23:42.177170Z","shell.execute_reply":"2022-07-14T12:23:44.043258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filling Missing values for the remaining Columns\nfilling_na(df, \"locale\")\nfilling_na(df, \"locale_name\")\nfilling_na(df, \"description\")\nfilling_na(df, \"transferred\")\nfilling_na(df, \"transactions\", \"num\")","metadata":{"id":"XyrEZNmlAKVg","scrolled":true,"execution":{"iopub.status.busy":"2022-07-14T12:23:44.045730Z","iopub.execute_input":"2022-07-14T12:23:44.046762Z","iopub.status.idle":"2022-07-14T12:23:46.266418Z","shell.execute_reply.started":"2022-07-14T12:23:44.046717Z","shell.execute_reply":"2022-07-14T12:23:46.265367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert date column to datetime object\ndf[\"date\"] = pd.to_datetime(df[\"date\"])\n\n#creating new columns for years and months\ndf[\"month\"] = df[\"date\"].dt.month\ndf[\"year\"] = df[\"date\"].dt.year","metadata":{"id":"dcYEQfpHHSO6","execution":{"iopub.status.busy":"2022-07-14T12:23:46.267728Z","iopub.execute_input":"2022-07-14T12:23:46.268060Z","iopub.status.idle":"2022-07-14T12:23:46.897888Z","shell.execute_reply.started":"2022-07-14T12:23:46.268030Z","shell.execute_reply":"2022-07-14T12:23:46.896778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"id":"8MZn954FIOxc","execution":{"iopub.status.busy":"2022-07-14T12:23:46.899613Z","iopub.execute_input":"2022-07-14T12:23:46.900091Z","iopub.status.idle":"2022-07-14T12:23:46.913485Z","shell.execute_reply.started":"2022-07-14T12:23:46.900046Z","shell.execute_reply":"2022-07-14T12:23:46.912310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"id":"Y6KMZVw0IS_b","execution":{"iopub.status.busy":"2022-07-14T12:23:46.914839Z","iopub.execute_input":"2022-07-14T12:23:46.915208Z","iopub.status.idle":"2022-07-14T12:23:46.946189Z","shell.execute_reply.started":"2022-07-14T12:23:46.915175Z","shell.execute_reply":"2022-07-14T12:23:46.945187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EDA of The Data <a name=\"eda\"></a>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(14,12))\nplt.xticks(rotation=90)\nsns.countplot(df[\"family\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:23:46.948917Z","iopub.execute_input":"2022-07-14T12:23:46.949295Z","iopub.status.idle":"2022-07-14T12:23:49.124417Z","shell.execute_reply.started":"2022-07-14T12:23:46.949266Z","shell.execute_reply":"2022-07-14T12:23:49.123563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14,12))\nplt.xticks(rotation=90)\nsns.countplot(df[\"locale_name\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:23:49.125807Z","iopub.execute_input":"2022-07-14T12:23:49.126270Z","iopub.status.idle":"2022-07-14T12:23:51.327310Z","shell.execute_reply.started":"2022-07-14T12:23:49.126237Z","shell.execute_reply":"2022-07-14T12:23:51.326263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Average Sales along years and the rend is going up as sales increase from along years","metadata":{}},{"cell_type":"code","source":"data = df.groupby(\"year\").agg({\"sales\": \"mean\"}).reset_index()\n\nfig = go.Figure()\n\nfig.add_trace(go.Scatter(x=data[\"year\"], y=data[\"sales\"], mode='lines+markers'))\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:23:51.328902Z","iopub.execute_input":"2022-07-14T12:23:51.329591Z","iopub.status.idle":"2022-07-14T12:23:51.434096Z","shell.execute_reply.started":"2022-07-14T12:23:51.329546Z","shell.execute_reply":"2022-07-14T12:23:51.432881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Transactions along the years and there are deficits in the transcations from 2016 to 2017","metadata":{}},{"cell_type":"code","source":"data = df.groupby([\"year\"]).agg({\"transactions\": \"sum\"}).reset_index()\n\nfig = go.Figure()\n\nfig.add_trace(go.Scatter(x=data[\"year\"], y=data[\"transactions\"], mode='lines+markers', text=data[\"transactions\"]))\nfig.update_traces(textposition=\"bottom right\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:23:51.435405Z","iopub.execute_input":"2022-07-14T12:23:51.436242Z","iopub.status.idle":"2022-07-14T12:23:51.513046Z","shell.execute_reply.started":"2022-07-14T12:23:51.436204Z","shell.execute_reply":"2022-07-14T12:23:51.511965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### After making deep in the months we find that transcations in the season of schools the transactions reduced from July to September","metadata":{}},{"cell_type":"code","source":"data = df.groupby(\"month\").agg({\"transactions\": \"sum\"}).reset_index()\n\nfig = go.Figure()\n\nfig.add_trace(go.Scatter(x=data[\"month\"], y=data[\"transactions\"], mode='lines+markers'))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:23:51.515465Z","iopub.execute_input":"2022-07-14T12:23:51.517111Z","iopub.status.idle":"2022-07-14T12:23:51.591033Z","shell.execute_reply.started":"2022-07-14T12:23:51.517076Z","shell.execute_reply":"2022-07-14T12:23:51.589843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The stores transactions along the year show which stores people go to buy from and the store (A) we findout the where the store placed it achieve the most transactions other than state of (GUAYAS) which mean that people trust that store more than the other","metadata":{}},{"cell_type":"code","source":"sns.relplot(\n    data=df, x=\"year\", y=\"transactions\", col=\"state\",\n    hue=\"store\", style=\"store\", kind=\"line\", height=5, aspect=.7, col_wrap=3\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:23:51.592526Z","iopub.execute_input":"2022-07-14T12:23:51.593554Z","iopub.status.idle":"2022-07-14T12:24:46.536284Z","shell.execute_reply.started":"2022-07-14T12:23:51.593508Z","shell.execute_reply":"2022-07-14T12:24:46.535127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The Sales of Cities and after checking the plots the most city which has sales is QUITO","metadata":{}},{"cell_type":"code","source":"sns.relplot(\n    data=df, x=\"month\", y=\"sales\", col=\"city\",\n    kind=\"line\", height=5, aspect=.7, col_wrap=3\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:24:46.537915Z","iopub.execute_input":"2022-07-14T12:24:46.538611Z","iopub.status.idle":"2022-07-14T12:25:35.435961Z","shell.execute_reply.started":"2022-07-14T12:24:46.538568Z","shell.execute_reply":"2022-07-14T12:25:35.434946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Families of products which people buy in the holidays and work days differenciate but will find out that there are 3 families bought more than the other which is Grocery l, Beverages, Automative","metadata":{}},{"cell_type":"code","source":"sns.relplot(\n    data=df, x=\"month\", y=\"sales\", col=\"holiday_type\",\n    kind=\"line\", hue=\"family\", height=5, aspect=.7, col_wrap=3\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T12:25:35.439807Z","iopub.execute_input":"2022-07-14T12:25:35.440224Z","iopub.status.idle":"2022-07-14T12:27:11.237524Z","shell.execute_reply.started":"2022-07-14T12:25:35.440194Z","shell.execute_reply":"2022-07-14T12:27:11.236490Z"},"trusted":true},"execution_count":null,"outputs":[]}]}