{"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":"# Python Project - HM data visualization","metadata":{}},{"cell_type":"markdown","source":"# Import pkgs and datasets","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport seaborn as sns \nimport matplotlib.pyplot as plt ","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:46:12.722457Z","iopub.execute_input":"2022-04-23T19:46:12.722843Z","iopub.status.idle":"2022-04-23T19:46:13.735604Z","shell.execute_reply.started":"2022-04-23T19:46:12.72274Z","shell.execute_reply":"2022-04-23T19:46:13.734817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\narticles.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:53:17.764402Z","iopub.execute_input":"2022-04-23T19:53:17.765129Z","iopub.status.idle":"2022-04-23T19:53:19.059106Z","shell.execute_reply.started":"2022-04-23T19:53:17.76509Z","shell.execute_reply":"2022-04-23T19:53:19.058266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\ncustomers.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:53:22.646177Z","iopub.execute_input":"2022-04-23T19:53:22.646872Z","iopub.status.idle":"2022-04-23T19:53:28.167974Z","shell.execute_reply.started":"2022-04-23T19:53:22.646828Z","shell.execute_reply":"2022-04-23T19:53:28.167092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntransactions.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:53:29.477312Z","iopub.execute_input":"2022-04-23T19:53:29.477588Z","iopub.status.idle":"2022-04-23T19:54:37.144729Z","shell.execute_reply.started":"2022-04-23T19:53:29.477551Z","shell.execute_reply":"2022-04-23T19:54:37.143625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Articles","metadata":{}},{"cell_type":"code","source":"articles.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:02:53.320622Z","iopub.execute_input":"2022-04-21T22:02:53.322328Z","iopub.status.idle":"2022-04-21T22:02:53.554447Z","shell.execute_reply.started":"2022-04-21T22:02:53.322258Z","shell.execute_reply":"2022-04-21T22:02:53.553469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.nunique().sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:05:25.805749Z","iopub.execute_input":"2022-04-21T22:05:25.806183Z","iopub.status.idle":"2022-04-21T22:05:25.998059Z","shell.execute_reply.started":"2022-04-21T22:05:25.806138Z","shell.execute_reply":"2022-04-21T22:05:25.996863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.corr()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:06:27.92965Z","iopub.execute_input":"2022-04-21T22:06:27.93003Z","iopub.status.idle":"2022-04-21T22:06:28.010607Z","shell.execute_reply.started":"2022-04-21T22:06:27.929992Z","shell.execute_reply":"2022-04-21T22:06:28.009746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"0. article_id = product_code \n1. Repeated attributes: eg index_group_no & index_group_name\n2. No missing values or null values for attributes of interests\n3. Don't worry about the figure data, graphical_appearance_name, at this moment\n4. For items categories, choose one to proceed from index_group_no, index_code, garment_group_no, section_no, product_type_no and department_no.\n5. For item color categories, choose one to proceed from perceived_colour_value_id, perceived_colour_master_id and colour_group_code.","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10, 7))\nax = sns.histplot(data=articles, y='perceived_colour_value_name')\nax.set_xlabel('count')\nax.set_ylabel('perceived_colour_value_name')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:55:24.575637Z","iopub.execute_input":"2022-04-23T19:55:24.576481Z","iopub.status.idle":"2022-04-23T19:55:25.037448Z","shell.execute_reply.started":"2022-04-23T19:55:24.576436Z","shell.execute_reply":"2022-04-23T19:55:25.036601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10, 7))\nax = sns.histplot(data=articles, y='perceived_colour_value_name', hue='index_group_name', multiple=\"stack\")\nax.set_xlabel('count')\nax.set_ylabel('perceived_colour_value_name')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:55:30.920482Z","iopub.execute_input":"2022-04-23T19:55:30.920798Z","iopub.status.idle":"2022-04-23T19:55:31.663217Z","shell.execute_reply.started":"2022-04-23T19:55:30.920767Z","shell.execute_reply":"2022-04-23T19:55:31.6624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Customers","metadata":{}},{"cell_type":"code","source":"customers.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:16:33.003717Z","iopub.execute_input":"2022-04-21T22:16:33.004367Z","iopub.status.idle":"2022-04-21T22:16:33.768546Z","shell.execute_reply.started":"2022-04-21T22:16:33.004314Z","shell.execute_reply":"2022-04-21T22:16:33.767516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. Null values: FN, Active, club_member_status, fashion_news_frequency \n2. Missing values: age\n3. Similar attributes: FN, Active, fashion_news_frequency ","metadata":{}},{"cell_type":"code","source":"customers.corr()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:16:43.089994Z","iopub.execute_input":"2022-04-21T22:16:43.090313Z","iopub.status.idle":"2022-04-21T22:16:43.183831Z","shell.execute_reply.started":"2022-04-21T22:16:43.090283Z","shell.execute_reply":"2022-04-21T22:16:43.182541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.nunique().sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-04-23T19:57:38.401877Z","iopub.execute_input":"2022-04-23T19:57:38.402217Z","iopub.status.idle":"2022-04-23T19:57:40.074546Z","shell.execute_reply.started":"2022-04-23T19:57:38.402183Z","shell.execute_reply":"2022-04-23T19:57:40.073741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,7))\nax = sns.histplot(data=customers, x='age', bins=50, color='green')\nax.set_xlabel('Distribution of the age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:16:55.189647Z","iopub.execute_input":"2022-04-21T22:16:55.189966Z","iopub.status.idle":"2022-04-21T22:16:55.731203Z","shell.execute_reply.started":"2022-04-21T22:16:55.189928Z","shell.execute_reply":"2022-04-21T22:16:55.730285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,7))\nax = sns.boxplot(data=customers, x='age', color='green')\nax.set_xlabel('Distribution of the age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:18:58.002077Z","iopub.execute_input":"2022-04-21T22:18:58.00245Z","iopub.status.idle":"2022-04-21T22:18:58.279749Z","shell.execute_reply.started":"2022-04-21T22:18:58.00241Z","shell.execute_reply":"2022-04-21T22:18:58.278679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_backup = customers.copy()\ncustomers_backup.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T02:46:55.445253Z","iopub.execute_input":"2022-04-20T02:46:55.446308Z","iopub.status.idle":"2022-04-20T02:46:55.539641Z","shell.execute_reply.started":"2022-04-20T02:46:55.446247Z","shell.execute_reply":"2022-04-20T02:46:55.538402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[\"age\"].mean()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:24:47.157547Z","iopub.execute_input":"2022-04-21T22:24:47.157818Z","iopub.status.idle":"2022-04-21T22:24:47.166092Z","shell.execute_reply.started":"2022-04-21T22:24:47.15779Z","shell.execute_reply":"2022-04-21T22:24:47.165254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[\"age\"].mode()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:24:44.044155Z","iopub.execute_input":"2022-04-21T22:24:44.044512Z","iopub.status.idle":"2022-04-21T22:24:44.07009Z","shell.execute_reply.started":"2022-04-21T22:24:44.044476Z","shell.execute_reply":"2022-04-21T22:24:44.06911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['age'].fillna(value=21,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:24:49.767207Z","iopub.execute_input":"2022-04-21T22:24:49.768318Z","iopub.status.idle":"2022-04-21T22:24:49.77757Z","shell.execute_reply.started":"2022-04-21T22:24:49.768231Z","shell.execute_reply":"2022-04-21T22:24:49.7765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_FN = customers.groupby('FN')['customer_id'].count()\npie_FN","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:31:16.569287Z","iopub.execute_input":"2022-04-21T22:31:16.569754Z","iopub.status.idle":"2022-04-21T22:31:16.760371Z","shell.execute_reply.started":"2022-04-21T22:31:16.569721Z","shell.execute_reply":"2022-04-21T22:31:16.759394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10,7))\ncolors = sns.color_palette('deep')\nax.pie(pie_FN, labels=pie_FN.index, colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of FN')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:31:19.055941Z","iopub.execute_input":"2022-04-21T22:31:19.056236Z","iopub.status.idle":"2022-04-21T22:31:19.177072Z","shell.execute_reply.started":"2022-04-21T22:31:19.056205Z","shell.execute_reply":"2022-04-21T22:31:19.17575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_fashion_news_frequency = customers.groupby('fashion_news_frequency')['customer_id'].count()\npie_fashion_news_frequency","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:31:26.328423Z","iopub.execute_input":"2022-04-21T22:31:26.329173Z","iopub.status.idle":"2022-04-21T22:31:26.521472Z","shell.execute_reply.started":"2022-04-21T22:31:26.32913Z","shell.execute_reply":"2022-04-21T22:31:26.520618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10,7))\ncolors = sns.color_palette('deep')\nax.pie(pie_fashion_news_frequency, labels=pie_fashion_news_frequency.index, colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of news frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:31:29.893759Z","iopub.execute_input":"2022-04-21T22:31:29.894142Z","iopub.status.idle":"2022-04-21T22:31:30.017531Z","shell.execute_reply.started":"2022-04-21T22:31:29.894068Z","shell.execute_reply":"2022-04-21T22:31:30.015977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_Active = customers.groupby('Active')['customer_id'].count()\npie_Active","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:33:29.477188Z","iopub.execute_input":"2022-04-21T22:33:29.477508Z","iopub.status.idle":"2022-04-21T22:33:29.668687Z","shell.execute_reply.started":"2022-04-21T22:33:29.477476Z","shell.execute_reply":"2022-04-21T22:33:29.667797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10,7))\ncolors = sns.color_palette('deep')\nax.pie(pie_Active, labels=pie_Active.index, colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of Active')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:34:08.661702Z","iopub.execute_input":"2022-04-21T22:34:08.661988Z","iopub.status.idle":"2022-04-21T22:34:08.784306Z","shell.execute_reply.started":"2022-04-21T22:34:08.661956Z","shell.execute_reply":"2022-04-21T22:34:08.783011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['FN'].fillna(value=0,inplace=True)\ncustomers['Active'].fillna(value=0,inplace=True)\ncustomers['club_member_status'].fillna(value=\"ACTIVE\",inplace=True)\ncustomers['fashion_news_frequency'].fillna(value=\"NONE\",inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:26:02.031564Z","iopub.execute_input":"2022-04-21T22:26:02.031899Z","iopub.status.idle":"2022-04-21T22:26:02.364678Z","shell.execute_reply.started":"2022-04-21T22:26:02.031863Z","shell.execute_reply":"2022-04-21T22:26:02.363498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[\"fashion_news_frequency\"]=customers[\"fashion_news_frequency\"].apply(lambda x: \"NONE\" if x==\"None\" else x)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:26:04.235979Z","iopub.execute_input":"2022-04-21T22:26:04.236313Z","iopub.status.idle":"2022-04-21T22:26:04.580585Z","shell.execute_reply.started":"2022-04-21T22:26:04.236273Z","shell.execute_reply":"2022-04-21T22:26:04.57952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[\"fashion_news_frequency\"]=customers[\"fashion_news_frequency\"].apply(lambda x: 0 if x==\"NONE\" else 1)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:26:05.769191Z","iopub.execute_input":"2022-04-21T22:26:05.769484Z","iopub.status.idle":"2022-04-21T22:26:06.6766Z","shell.execute_reply.started":"2022-04-21T22:26:05.769454Z","shell.execute_reply":"2022-04-21T22:26:06.675444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:26:10.851033Z","iopub.execute_input":"2022-04-21T22:26:10.851522Z","iopub.status.idle":"2022-04-21T22:26:11.358133Z","shell.execute_reply.started":"2022-04-21T22:26:10.851476Z","shell.execute_reply":"2022-04-21T22:26:11.357436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.nunique().sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-04-20T02:47:37.685425Z","iopub.execute_input":"2022-04-20T02:47:37.685749Z","iopub.status.idle":"2022-04-20T02:47:39.389671Z","shell.execute_reply.started":"2022-04-20T02:47:37.685718Z","shell.execute_reply":"2022-04-20T02:47:39.388561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.corr()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:26:14.357804Z","iopub.execute_input":"2022-04-21T22:26:14.35837Z","iopub.status.idle":"2022-04-21T22:26:14.463855Z","shell.execute_reply.started":"2022-04-21T22:26:14.358311Z","shell.execute_reply":"2022-04-21T22:26:14.463129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So FN = Active = fashion_news_frequency","metadata":{}},{"cell_type":"code","source":"pie_club_member_status = customers.groupby('club_member_status')['customer_id'].count()\npie_club_member_status","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:27:50.477656Z","iopub.execute_input":"2022-04-21T22:27:50.4783Z","iopub.status.idle":"2022-04-21T22:27:50.754022Z","shell.execute_reply.started":"2022-04-21T22:27:50.478255Z","shell.execute_reply":"2022-04-21T22:27:50.753145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10,7))\ncolors = sns.color_palette('deep')\nax.pie(pie_club_member_status, labels=pie_club_member_status.index, colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of club_member_status')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:27:54.117488Z","iopub.execute_input":"2022-04-21T22:27:54.11784Z","iopub.status.idle":"2022-04-21T22:27:54.253057Z","shell.execute_reply.started":"2022-04-21T22:27:54.117796Z","shell.execute_reply":"2022-04-21T22:27:54.251907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transactions","metadata":{}},{"cell_type":"code","source":"transactions.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:35:28.427887Z","iopub.execute_input":"2022-04-21T22:35:28.428214Z","iopub.status.idle":"2022-04-21T22:35:28.441681Z","shell.execute_reply.started":"2022-04-21T22:35:28.428184Z","shell.execute_reply":"2022-04-21T22:35:28.440404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.nunique().sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:51:51.823272Z","iopub.execute_input":"2022-04-21T03:51:51.824506Z","iopub.status.idle":"2022-04-21T03:52:04.324583Z","shell.execute_reply.started":"2022-04-21T03:51:51.824376Z","shell.execute_reply":"2022-04-21T03:52:04.323712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (10, 7))\nplt.xlim(0, 0.3)\nax = sns.histplot(data = transactions, x = 'price', bins = 75, color = 'green')\nax.set_xlabel('Distribution of price')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:40:36.967636Z","iopub.execute_input":"2022-04-21T22:40:36.967993Z","iopub.status.idle":"2022-04-21T22:40:49.570016Z","shell.execute_reply.started":"2022-04-21T22:40:36.967958Z","shell.execute_reply":"2022-04-21T22:40:49.569154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['price'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:40:54.377928Z","iopub.execute_input":"2022-04-21T22:40:54.378859Z","iopub.status.idle":"2022-04-21T22:40:55.794857Z","shell.execute_reply.started":"2022-04-21T22:40:54.378798Z","shell.execute_reply":"2022-04-21T22:40:55.793835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_sales_channel_id = transactions.groupby('sales_channel_id')['customer_id'].count()\npie_sales_channel_id","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:40:59.911246Z","iopub.execute_input":"2022-04-21T22:40:59.911546Z","iopub.status.idle":"2022-04-21T22:41:05.621449Z","shell.execute_reply.started":"2022-04-21T22:40:59.911502Z","shell.execute_reply":"2022-04-21T22:41:05.620389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(10,7))\ncolors = sns.color_palette('deep')\nax.pie(pie_sales_channel_id, labels=pie_sales_channel_id.index, colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of sales_channel_id')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:41:05.623421Z","iopub.execute_input":"2022-04-21T22:41:05.623743Z","iopub.status.idle":"2022-04-21T22:41:05.764981Z","shell.execute_reply.started":"2022-04-21T22:41:05.623703Z","shell.execute_reply":"2022-04-21T22:41:05.763982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. transactions count and total sales on each date; scatter plot and trending lines\n2. on each day; histplot and period analysis","metadata":{}},{"cell_type":"code","source":"daily_sales_count = transactions.groupby(\"t_dat\")[\"price\"].count().reset_index()\ndaily_sales_count.rename(columns={\"t_dat\":\"Date\", \"price\":\"daily_sales_count\"}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:41:11.816829Z","iopub.execute_input":"2022-04-21T22:41:11.817429Z","iopub.status.idle":"2022-04-21T22:41:15.873386Z","shell.execute_reply.started":"2022-04-21T22:41:11.817383Z","shell.execute_reply":"2022-04-21T22:41:15.872452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:10.813941Z","iopub.execute_input":"2022-04-21T03:53:10.814221Z","iopub.status.idle":"2022-04-21T03:53:10.830398Z","shell.execute_reply.started":"2022-04-21T03:53:10.814192Z","shell.execute_reply":"2022-04-21T03:53:10.829424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_events = pd.read_csv(\"../input/us-holiday-dates-2004-2021/US Holiday Dates (2004-2021).csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:52:13.164012Z","iopub.execute_input":"2022-04-21T22:52:13.164387Z","iopub.status.idle":"2022-04-21T22:52:13.182037Z","shell.execute_reply.started":"2022-04-21T22:52:13.16435Z","shell.execute_reply":"2022-04-21T22:52:13.181295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_events.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:53:22.791863Z","iopub.execute_input":"2022-04-21T22:53:22.79278Z","iopub.status.idle":"2022-04-21T22:53:22.80536Z","shell.execute_reply.started":"2022-04-21T22:53:22.792737Z","shell.execute_reply":"2022-04-21T22:53:22.804369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"holidays_events = holidays_events.drop(['WeekDay', 'Month', 'Day', 'Year'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:20.562511Z","iopub.execute_input":"2022-04-21T03:53:20.562832Z","iopub.status.idle":"2022-04-21T03:53:20.569036Z","shell.execute_reply.started":"2022-04-21T03:53:20.562796Z","shell.execute_reply":"2022-04-21T03:53:20.568241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count = daily_sales_count.merge(holidays_events, how = 'left', on = 'Date')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:24.184605Z","iopub.execute_input":"2022-04-21T03:53:24.184943Z","iopub.status.idle":"2022-04-21T03:53:24.206691Z","shell.execute_reply.started":"2022-04-21T03:53:24.184906Z","shell.execute_reply":"2022-04-21T03:53:24.205747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count_back_up = daily_sales_count","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:26.623426Z","iopub.execute_input":"2022-04-21T03:53:26.623746Z","iopub.status.idle":"2022-04-21T03:53:26.628139Z","shell.execute_reply.started":"2022-04-21T03:53:26.623713Z","shell.execute_reply":"2022-04-21T03:53:26.626998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:29.113212Z","iopub.execute_input":"2022-04-21T03:53:29.114421Z","iopub.status.idle":"2022-04-21T03:53:29.130487Z","shell.execute_reply.started":"2022-04-21T03:53:29.114369Z","shell.execute_reply":"2022-04-21T03:53:29.128932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count[\"Holiday\"] = daily_sales_count[\"Holiday\"].fillna(\"Not a holiday\")","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:32.072975Z","iopub.execute_input":"2022-04-21T03:53:32.073499Z","iopub.status.idle":"2022-04-21T03:53:32.079266Z","shell.execute_reply.started":"2022-04-21T03:53:32.073455Z","shell.execute_reply":"2022-04-21T03:53:32.078227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count[\"Holiday\"]=daily_sales_count[\"Holiday\"].apply(lambda x: \"non Holiday\" if x==\"Not a holiday\" else \"Holiday\")","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:34.662746Z","iopub.execute_input":"2022-04-21T03:53:34.66388Z","iopub.status.idle":"2022-04-21T03:53:34.670908Z","shell.execute_reply.started":"2022-04-21T03:53:34.663779Z","shell.execute_reply":"2022-04-21T03:53:34.669956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_count.head(100)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:37.012107Z","iopub.execute_input":"2022-04-21T03:53:37.012959Z","iopub.status.idle":"2022-04-21T03:53:37.028271Z","shell.execute_reply.started":"2022-04-21T03:53:37.012905Z","shell.execute_reply":"2022-04-21T03:53:37.027426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax1 = plt.subplots(figsize=(20, 8))\n\nsns.scatterplot(data=daily_sales_count, ax=ax1, x = \"Date\", y = \"daily_sales_count\", hue = \"Holiday\")\n\nax1.set_xticks(range(0, 701, 100))\nax1.set_xticklabels([daily_sales_count[\"Date\"][i] for i in range(0, 701, 100)])\nax1.set_xlabel(\"date\")\nax1.set_ylabel(\"total count\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T03:53:40.646669Z","iopub.execute_input":"2022-04-21T03:53:40.646987Z","iopub.status.idle":"2022-04-21T03:53:42.09516Z","shell.execute_reply.started":"2022-04-21T03:53:40.646944Z","shell.execute_reply":"2022-04-21T03:53:42.094446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_volume = transactions.groupby(\"t_dat\")[\"price\"].sum().reset_index()\ndaily_sales_volume.rename(columns={\"t_dat\":\"date\", \"price\":\"daily_sales_volume\"}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:41:34.206703Z","iopub.execute_input":"2022-04-21T22:41:34.206982Z","iopub.status.idle":"2022-04-21T22:41:37.528349Z","shell.execute_reply.started":"2022-04-21T22:41:34.206953Z","shell.execute_reply":"2022-04-21T22:41:37.52736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"period = (\n    daily_sales_volume\n    .groupby('date').mean()\n    .squeeze()\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:52:32.766282Z","iopub.execute_input":"2022-04-21T04:52:32.766704Z","iopub.status.idle":"2022-04-21T04:52:32.777338Z","shell.execute_reply.started":"2022-04-21T04:52:32.766662Z","shell.execute_reply":"2022-04-21T04:52:32.776418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"period","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:35:59.304393Z","iopub.execute_input":"2022-04-21T04:35:59.305438Z","iopub.status.idle":"2022-04-21T04:35:59.319683Z","shell.execute_reply.started":"2022-04-21T04:35:59.30538Z","shell.execute_reply":"2022-04-21T04:35:59.318812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_volume['weekday'] = pd.DatetimeIndex(daily_sales_volume['date']).weekday\ndaily_sales_volume['week'] = pd.DatetimeIndex(daily_sales_volume['date']).week\ndaily_sales_volume['day'] = pd.DatetimeIndex(daily_sales_volume['date']).day\ndaily_sales_volume['month'] = pd.DatetimeIndex(daily_sales_volume['date']).month \ndaily_sales_volume['year'] = pd.DatetimeIndex(daily_sales_volume['date']).year\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:41:43.301265Z","iopub.execute_input":"2022-04-21T22:41:43.301529Z","iopub.status.idle":"2022-04-21T22:41:43.327324Z","shell.execute_reply.started":"2022-04-21T22:41:43.301502Z","shell.execute_reply":"2022-04-21T22:41:43.326198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_sales_volume.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T22:41:46.75678Z","iopub.execute_input":"2022-04-21T22:41:46.757361Z","iopub.status.idle":"2022-04-21T22:41:46.772376Z","shell.execute_reply.started":"2022-04-21T22:41:46.75731Z","shell.execute_reply":"2022-04-21T22:41:46.771138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seasonal_plot(daily_sales_volume, y='daily_sales_volume', period='week', freq='weekday');","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:49:15.559211Z","iopub.execute_input":"2022-04-21T04:49:15.559867Z","iopub.status.idle":"2022-04-21T04:49:28.396491Z","shell.execute_reply.started":"2022-04-21T04:49:15.559811Z","shell.execute_reply":"2022-04-21T04:49:28.395348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seasonal_plot(daily_sales_volume, y='daily_sales_volume', period='month', freq='day');","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:01:33.136515Z","iopub.execute_input":"2022-04-21T05:01:33.137338Z","iopub.status.idle":"2022-04-21T05:01:45.203852Z","shell.execute_reply.started":"2022-04-21T05:01:33.137299Z","shell.execute_reply":"2022-04-21T05:01:45.202919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seasonal_plot(daily_sales_volume, y='daily_sales_volume', period='year', freq='day');","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:03:02.225825Z","iopub.execute_input":"2022-04-21T05:03:02.226431Z","iopub.status.idle":"2022-04-21T05:03:05.506958Z","shell.execute_reply.started":"2022-04-21T05:03:02.226388Z","shell.execute_reply":"2022-04-21T05:03:05.505809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (8,4))\nax = sns.histplot(data=daily_sales_volume, y='daily_sales_volume', color='green')\nax.set_xlabel('count')\nax.set_ylabel('daily_sales_volume')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:36:28.844629Z","iopub.execute_input":"2022-04-21T04:36:28.845308Z","iopub.status.idle":"2022-04-21T04:36:29.38159Z","shell.execute_reply.started":"2022-04-21T04:36:28.845264Z","shell.execute_reply":"2022-04-21T04:36:29.380024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,7))\nax = sns.boxplot(data=daily_sales_volume, x='daily_sales_volume', color='green')\nax.set_xlabel('Distribution of daily sales')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:36:33.04935Z","iopub.execute_input":"2022-04-21T04:36:33.049766Z","iopub.status.idle":"2022-04-21T04:36:33.327946Z","shell.execute_reply.started":"2022-04-21T04:36:33.049718Z","shell.execute_reply":"2022-04-21T04:36:33.326897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,7))\norder = [\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]\nax = sns.boxplot(x=\"weekday\", y=\"daily_sales_volume\", hue=\"weekday\", data=daily_sales_volume, showfliers = False)    \nplt.show()\n\nplt.clf()\nplt.close()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:50:34.841985Z","iopub.execute_input":"2022-04-21T04:50:34.842817Z","iopub.status.idle":"2022-04-21T04:50:35.281878Z","shell.execute_reply.started":"2022-04-21T04:50:34.842766Z","shell.execute_reply":"2022-04-21T04:50:35.279678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,7))\nax = sns.boxplot(x=\"month\", y=\"daily_sales_volume\", data=daily_sales_volume, showfliers = False)    \nplt.show()\n\nplt.clf()\nplt.close()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:50:53.158594Z","iopub.execute_input":"2022-04-21T04:50:53.158989Z","iopub.status.idle":"2022-04-21T04:50:53.57715Z","shell.execute_reply.started":"2022-04-21T04:50:53.158952Z","shell.execute_reply":"2022-04-21T04:50:53.57548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Highest daily sales volume on June; but outliers on April, September, and November","metadata":{}},{"cell_type":"markdown","source":"# Time Series check","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\nfrom learntools.time_series.style import *  # plot style settings\nfrom learntools.time_series.utils import plot_periodogram, seasonal_plot\n\nfrom sklearn.linear_model import LinearRegression\nfrom statsmodels.tsa.deterministic import CalendarFourier, DeterministicProcess","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:50:57.958976Z","iopub.execute_input":"2022-04-21T04:50:57.959288Z","iopub.status.idle":"2022-04-21T04:50:57.964825Z","shell.execute_reply.started":"2022-04-21T04:50:57.959256Z","shell.execute_reply":"2022-04-21T04:50:57.963948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"period.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:51:21.845749Z","iopub.execute_input":"2022-04-21T04:51:21.846037Z","iopub.status.idle":"2022-04-21T04:51:21.856233Z","shell.execute_reply.started":"2022-04-21T04:51:21.846009Z","shell.execute_reply":"2022-04-21T04:51:21.85507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_periodogram(period);","metadata":{"execution":{"iopub.status.busy":"2022-04-21T04:51:29.48816Z","iopub.execute_input":"2022-04-21T04:51:29.488507Z","iopub.status.idle":"2022-04-21T04:51:30.43064Z","shell.execute_reply.started":"2022-04-21T04:51:29.488472Z","shell.execute_reply":"2022-04-21T04:51:30.429377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom statsmodels.tsa.stattools import adfuller\nfrom statsmodels.tsa.seasonal import seasonal_decompose\nfrom statsmodels.tsa.arima_model import ARIMA\nfrom pandas.plotting import register_matplotlib_converters\nregister_matplotlib_converters()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:08:58.140251Z","iopub.execute_input":"2022-04-21T05:08:58.141391Z","iopub.status.idle":"2022-04-21T05:08:58.355467Z","shell.execute_reply.started":"2022-04-21T05:08:58.141322Z","shell.execute_reply":"2022-04-21T05:08:58.354493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax1 = plt.subplots(figsize=(20, 8))\n\nsns.scatterplot(data=daily_sales_volume, ax=ax1, x = \"date\", y = \"daily_sales_volume\")\n\nax1.set_xticks(range(0, 701, 100))\nax1.set_xticklabels([daily_sales_volume[\"date\"][i] for i in range(0, 701, 100)])\nax1.set_xlabel(\"date\")\nax1.set_ylabel(\"total count\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:11:09.019003Z","iopub.execute_input":"2022-04-21T05:11:09.019315Z","iopub.status.idle":"2022-04-21T05:11:10.70159Z","shell.execute_reply.started":"2022-04-21T05:11:09.01928Z","shell.execute_reply":"2022-04-21T05:11:10.700697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = daily_sales_volume[['date','daily_sales_volume']]","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:15:25.495191Z","iopub.execute_input":"2022-04-21T05:15:25.495532Z","iopub.status.idle":"2022-04-21T05:15:25.50332Z","shell.execute_reply.started":"2022-04-21T05:15:25.495497Z","shell.execute_reply":"2022-04-21T05:15:25.50203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.set_index('date', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:22:51.017378Z","iopub.execute_input":"2022-04-21T05:22:51.01772Z","iopub.status.idle":"2022-04-21T05:22:51.025612Z","shell.execute_reply.started":"2022-04-21T05:22:51.017684Z","shell.execute_reply":"2022-04-21T05:22:51.024722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.xlabel('date')\nplt.ylabel('daily_sales_volume')\nplt.plot(df)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:23:45.378629Z","iopub.execute_input":"2022-04-21T05:23:45.379084Z","iopub.status.idle":"2022-04-21T05:24:17.706805Z","shell.execute_reply.started":"2022-04-21T05:23:45.379036Z","shell.execute_reply":"2022-04-21T05:24:17.705902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rolling_mean = df.rolling(window = 7).mean()\nrolling_std = df.rolling(window = 7).std()\nplt.plot(df, color = 'blue', label = 'Original')\nplt.plot(rolling_mean, color = 'red', label = 'Rolling Mean')\nplt.plot(rolling_std, color = 'black', label = 'Rolling Std')\nplt.legend(loc = 'best')\nplt.title('Rolling Mean & Rolling Standard Deviation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:22:53.977174Z","iopub.execute_input":"2022-04-21T05:22:53.977509Z","iopub.status.idle":"2022-04-21T05:23:17.520159Z","shell.execute_reply.started":"2022-04-21T05:22:53.977473Z","shell.execute_reply":"2022-04-21T05:23:17.519319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = adfuller(df['daily_sales_volume'])\nprint('ADF Statistic: {}'.format(result[0]))\nprint('p-value: {}'.format(result[1]))\nprint('Critical Values:')\nfor key, value in result[4].items():\n    print('\\t{}: {}'.format(key, value))","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:24:56.045208Z","iopub.execute_input":"2022-04-21T05:24:56.045602Z","iopub.status.idle":"2022-04-21T05:24:56.110389Z","shell.execute_reply.started":"2022-04-21T05:24:56.045555Z","shell.execute_reply":"2022-04-21T05:24:56.109269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The ADF Statistic is close to the critical values and the p-value is smaller than the threshold (0.05). Thus, we can conclude that the time series is stationary.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom statsmodels.tsa.stattools import acf, pacf\nfrom statsmodels.graphics.tsaplots import plot_acf, plot_pacf\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import rcParams\nfrom cycler import cycler","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:38:05.700758Z","iopub.execute_input":"2022-04-21T05:38:05.701251Z","iopub.status.idle":"2022-04-21T05:38:05.711796Z","shell.execute_reply.started":"2022-04-21T05:38:05.701215Z","shell.execute_reply":"2022-04-21T05:38:05.710488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['volume_diff'] = df['daily_sales_volume'].diff(periods=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:39:52.188864Z","iopub.execute_input":"2022-04-21T05:39:52.18966Z","iopub.status.idle":"2022-04-21T05:39:52.196047Z","shell.execute_reply.started":"2022-04-21T05:39:52.189617Z","shell.execute_reply":"2022-04-21T05:39:52.195324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['volume_diff'] ","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:39:57.66856Z","iopub.execute_input":"2022-04-21T05:39:57.668889Z","iopub.status.idle":"2022-04-21T05:39:57.678876Z","shell.execute_reply.started":"2022-04-21T05:39:57.668857Z","shell.execute_reply":"2022-04-21T05:39:57.67767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:40:22.349418Z","iopub.execute_input":"2022-04-21T05:40:22.350566Z","iopub.status.idle":"2022-04-21T05:40:22.359985Z","shell.execute_reply.started":"2022-04-21T05:40:22.3505Z","shell.execute_reply":"2022-04-21T05:40:22.358827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acf_values = acf(df['volume_diff'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:40:25.093104Z","iopub.execute_input":"2022-04-21T05:40:25.093466Z","iopub.status.idle":"2022-04-21T05:40:25.100783Z","shell.execute_reply.started":"2022-04-21T05:40:25.09343Z","shell.execute_reply":"2022-04-21T05:40:25.099745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acf_values","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:40:29.487839Z","iopub.execute_input":"2022-04-21T05:40:29.48829Z","iopub.status.idle":"2022-04-21T05:40:29.495762Z","shell.execute_reply.started":"2022-04-21T05:40:29.488255Z","shell.execute_reply":"2022-04-21T05:40:29.494811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_acf(df['volume_diff'], lags=30);","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:40:33.103952Z","iopub.execute_input":"2022-04-21T05:40:33.10426Z","iopub.status.idle":"2022-04-21T05:40:33.549779Z","shell.execute_reply.started":"2022-04-21T05:40:33.104228Z","shell.execute_reply":"2022-04-21T05:40:33.548303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pacf_values = pacf(df['volume_diff'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:41:38.760183Z","iopub.execute_input":"2022-04-21T05:41:38.76055Z","iopub.status.idle":"2022-04-21T05:41:38.779699Z","shell.execute_reply.started":"2022-04-21T05:41:38.760511Z","shell.execute_reply":"2022-04-21T05:41:38.777517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_acf(df['volume_diff'], lags=30);","metadata":{"execution":{"iopub.status.busy":"2022-04-21T05:42:15.979088Z","iopub.execute_input":"2022-04-21T05:42:15.979436Z","iopub.status.idle":"2022-04-21T05:42:16.411396Z","shell.execute_reply.started":"2022-04-21T05:42:15.979403Z","shell.execute_reply":"2022-04-21T05:42:16.410485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AR of order 1, differencing of order 1 and MA of order 1.","metadata":{}},{"cell_type":"markdown","source":"In a nutshell:\n* If the ACF plot declines gradually and the PACF drops instantly, use Auto Regressive model.\n* If the ACF plot drops instantly and the PACF declines gradually, use Moving Average model.\n* If both ACF and PACF decline gradually, combine Auto Regressive and Moving Average models (ARMA).\n* If both ACF and PACF drop instantly (no significant lags), it’s likely you won’t be able to model the time series.","metadata":{}},{"cell_type":"markdown","source":"* Recall, that ACF can be used to figure out the best order of the MA model.\n* ecall, that PACF can be used to figure out the best order of the AR model. \n* If the lag-1 autocorrelation is -0.5 or more negative, the series may be overdifferenced.  BEWARE OF OVERDIFFERENCING!!","metadata":{}}]}