{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7391,"databundleVersionId":44328,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-29T15:55:25.960929Z","iopub.execute_input":"2023-12-29T15:55:25.961305Z","iopub.status.idle":"2023-12-29T15:55:26.337243Z","shell.execute_reply.started":"2023-12-29T15:55:25.961272Z","shell.execute_reply":"2023-12-29T15:55:26.336301Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\nimport py7zr\nfrom subprocess import check_output\n\nfor dirname, _, filenames in os.walk('/kaggle/input/favorita-grocery-sales-forecasting'):\n    for filename in filenames:\n        archive = py7zr.SevenZipFile(os.path.join(dirname, filename), mode='r')\n        archive.extractall(path=\"/kaggle/working\")\n        archive.close()\n\nprint(check_output([\"ls\", \"../working\"]).decode(\"utf8\"))\nimport warnings\nwarnings.simplefilter('ignore')\n#Tip 1.3. Ignore all warnings about later execution ¶\nwarnings.filterwarnings(\"ignore\", category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:55:26.338746Z","iopub.execute_input":"2023-12-29T15:55:26.339130Z","iopub.status.idle":"2023-12-29T15:56:59.427869Z","shell.execute_reply.started":"2023-12-29T15:55:26.339097Z","shell.execute_reply":"2023-12-29T15:56:59.426763Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Table of content\n* [load data](#0)\n* [stores](#stores)\n* [items](#items)\n* [transactions](#transactions)\n* [oil](#4)\n* [holidays_events](#5)\n* [train](#6)\n\n\n\n\n","metadata":{}},{"cell_type":"markdown","source":"  ## load data  <a class=\"anchor\"  id=\"0\"></a>","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv(\"../working/test.csv\")\ndf_sub = pd.read_csv(\"../working/sample_submission.csv\")\ndf_stores = pd.read_csv(\"../working/stores.csv\")\ndf_items = pd.read_csv(\"../working/items.csv\")\ndf_transactions = pd.read_csv(\"../working/transactions.csv\", parse_dates=['date'])\ndf_oil = pd.read_csv(\"../working/oil.csv\", parse_dates=['date'])\ndf_holiday = pd.read_csv(\"../working/holidays_events.csv\", parse_dates=['date'])\n# the full training data's output: \"125,497,040 rows | 6 columns\"\n#Therefore I will only load approx 5% of the data just to get a rough idea of what is in store for us.\nimport random\nrandom.seed(4321)\ndf_train = pd.read_csv(\"../working/train.csv\",  parse_dates=['date'],\n                    skiprows=lambda x: x > 0 and random.random() >=0.05  )","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:56:59.429298Z","iopub.execute_input":"2023-12-29T15:56:59.429576Z","iopub.status.idle":"2023-12-29T15:58:36.398932Z","shell.execute_reply.started":"2023-12-29T15:56:59.429546Z","shell.execute_reply":"2023-12-29T15:58:36.398139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\">\n<b>Comment:</b> the full training data's output: \"125,497,040 rows | 6 columns\"\n#Therefore I will only load approx 5% of the data just to get a rough idea of what is in store for us.\n</div>","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport numpy as np\nimport matplotlib.pyplot as plt\npd.options.display.float_format = '{:.2f}'.format","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:36.400901Z","iopub.execute_input":"2023-12-29T15:58:36.401197Z","iopub.status.idle":"2023-12-29T15:58:37.395367Z","shell.execute_reply.started":"2023-12-29T15:58:36.401172Z","shell.execute_reply":"2023-12-29T15:58:37.394576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df_train.merge(df_stores, left_on='store_nbr', right_on='store_nbr',\n                 how='left' )\nx.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:37.396466Z","iopub.execute_input":"2023-12-29T15:58:37.396740Z","iopub.status.idle":"2023-12-29T15:58:38.504236Z","shell.execute_reply.started":"2023-12-29T15:58:37.396716Z","shell.execute_reply":"2023-12-29T15:58:38.503221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<code style=\"background:yellow;color:red\">\nLet's study\n</code>","metadata":{}},{"cell_type":"markdown","source":"  ## 1-stores  <a class=\"anchor\"  id=\"stores\"></a>\n","metadata":{}},{"cell_type":"code","source":"df_stores.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:38.505616Z","iopub.execute_input":"2023-12-29T15:58:38.505960Z","iopub.status.idle":"2023-12-29T15:58:38.521078Z","shell.execute_reply.started":"2023-12-29T15:58:38.505926Z","shell.execute_reply":"2023-12-29T15:58:38.520159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_countplot(df, colName: str):\n    sns.set_theme(style=\"whitegrid\")\n    val = df[colName].value_counts().sort_values(ascending=False)\n    # val=df[colName].value_counts().sort_index(ascending=True)\n\n    ax = sns.countplot(df, y=colName # or y\n                       , palette=\"pastel\"\n                       # , stat=\"percent\"\n                       , order=val.index\n                       )\n\n    summ = val.sum()\n\n    title = val.index\n    lbls = [f'{p[0] * 100 / summ:.1f}%' for p in zip(val)]\n    \n    #vetical label\n    ax.bar_label(container=ax.containers[0], labels=lbls)\n    # hor label\n    # for p, label in zip(ax.patches, lbls):\n    #     ax.annotate(label, (p.get_x(), p.get_height() + 0.15))\n    \n   \n\n    plt.tight_layout()\n    plt.show()\n\n\nshow_countplot(df_stores, \"state\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:38.522316Z","iopub.execute_input":"2023-12-29T15:58:38.522584Z","iopub.status.idle":"2023-12-29T15:58:39.095158Z","shell.execute_reply.started":"2023-12-29T15:58:38.522562Z","shell.execute_reply":"2023-12-29T15:58:39.094278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nPichancha has the most count of store in states\n</div>\n","metadata":{}},{"cell_type":"code","source":"show_countplot(df_stores, \"city\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:39.096131Z","iopub.execute_input":"2023-12-29T15:58:39.096422Z","iopub.status.idle":"2023-12-29T15:58:39.776961Z","shell.execute_reply.started":"2023-12-29T15:58:39.096396Z","shell.execute_reply":"2023-12-29T15:58:39.775997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b>\nQuito has the most count of store in cities\n</div>","metadata":{}},{"cell_type":"code","source":"show_countplot(df_stores, \"type\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:39.778238Z","iopub.execute_input":"2023-12-29T15:58:39.778547Z","iopub.status.idle":"2023-12-29T15:58:40.082645Z","shell.execute_reply.started":"2023-12-29T15:58:39.778521Z","shell.execute_reply":"2023-12-29T15:58:40.081740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b>\nthe most count of store are in the D type\n</div>","metadata":{}},{"cell_type":"code","source":"show_countplot(df_stores, \"cluster\")","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:40.087194Z","iopub.execute_input":"2023-12-29T15:58:40.087497Z","iopub.status.idle":"2023-12-29T15:58:40.663918Z","shell.execute_reply.started":"2023-12-29T15:58:40.087471Z","shell.execute_reply":"2023-12-29T15:58:40.662923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b>\nthe most count of store are in the 3 cluster\n</div>","metadata":{}},{"cell_type":"code","source":"df=df_stores\nx_var= \"state\"\ny_var= \"type\"\n\nplt.figure(figsize=(5, 5))\ndf_grouped = df.groupby(x_var)[y_var].value_counts(normalize=True).unstack(y_var)\ndf_grouped=df_grouped.fillna(0)\ncolorr=sns.color_palette(\"pastel\",n_colors=df_grouped.shape[1])\ndf_grouped.plot.barh(stacked=True,color=colorr )\n\nplt.legend(loc='center left', bbox_to_anchor=(1, 0.5))\nfor ix, row in df_grouped.reset_index(drop=True).iterrows():\n    cumulative = 0\n    for element in row:\n        if element == element and element > 0.1 and (element is not None):\n            plt.text(\n                cumulative + element / 2,\n\n                ix,\n                f\"{int(element * 100)} %\",\n                va=\"center\",\n                ha=\"center\",\n                fontsize=10,\n            )\n        cumulative += element\nplt.title(\"Percent distribution of type in states\",fontsize = 20)\nplt.tight_layout()\nplt.show()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:40.665166Z","iopub.execute_input":"2023-12-29T15:58:40.665468Z","iopub.status.idle":"2023-12-29T15:58:41.408236Z","shell.execute_reply.started":"2023-12-29T15:58:40.665442Z","shell.execute_reply":"2023-12-29T15:58:41.407394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b>\nIn some state there is just one type</div>","metadata":{}},{"cell_type":"markdown","source":"  ## 2-items  <a class=\"anchor\"  id=\"items\"></a>\n","metadata":{}},{"cell_type":"code","source":"# df_items.info()\ndf_items.head()\n# df_items[\"class\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:41.409477Z","iopub.execute_input":"2023-12-29T15:58:41.409741Z","iopub.status.idle":"2023-12-29T15:58:41.419126Z","shell.execute_reply.started":"2023-12-29T15:58:41.409718Z","shell.execute_reply":"2023-12-29T15:58:41.418205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colName=\"family\"\n\nsns.set_theme(style=\"whitegrid\")\nval = df_items[colName].value_counts().sort_values(ascending=False)\nplt.figure(figsize=(8, 10))\n\nax = sns.countplot(df_items, y=colName # or y\n                   , palette=\"pastel\"\n                   ,hue =\"perishable\"\n                   # , stat=\"percent\"\n                   , order=val.index\n                   )\nplt.rcParams[\"figure.figsize\"] = (20,25)\nsumm = val.sum()\n\ntitle = val.index\nlbls = [f'{p[0] * 100 / summ:.1f}%' for p in zip(val)]\n\n#vetical label\nax.bar_label(container=ax.containers[0], labels=lbls)\n\nplt.legend([\"not perishable\", \"perishable\"],loc='center left', bbox_to_anchor=(1, 0.5))#out\nplt.title(\"Distribution of perishable item\",\n          fontsize = 18)\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T16:01:29.743331Z","iopub.execute_input":"2023-12-29T16:01:29.743656Z","iopub.status.idle":"2023-12-29T16:01:30.790237Z","shell.execute_reply.started":"2023-12-29T16:01:29.743629Z","shell.execute_reply":"2023-12-29T16:01:30.789286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b>\nthe most count of sell item is grocerry\n</div>","metadata":{}},{"cell_type":"markdown","source":"  ## 3-transactions  <a class=\"anchor\"  id=\"transactions\"></a>","metadata":{}},{"cell_type":"code","source":"df_transactions[\"year\"]=df_transactions['date']. dt.year\ndf_transactions[\"month\"]=df_transactions['date']. dt. month\n#format YYYY-MM\ndf_transactions[\"year-month\"]=df_transactions['date'].dt.strftime('%Y-%m')\n# df_transactions.head()\ndf_transactions.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:42.464795Z","iopub.execute_input":"2023-12-29T15:58:42.465165Z","iopub.status.idle":"2023-12-29T15:58:43.143182Z","shell.execute_reply.started":"2023-12-29T15:58:42.465127Z","shell.execute_reply":"2023-12-29T15:58:43.142149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 10))\nresult =df_transactions.groupby(['store_nbr']).agg(\n             sum_transactions = ('transactions','sum'),\n             ).sort_values(by='sum_transactions',ascending=True).plot.barh()\n\nplt.title(\"Sale_nbr(id) and total sale\",\n          fontsize = 20)\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:43.144665Z","iopub.execute_input":"2023-12-29T15:58:43.144975Z","iopub.status.idle":"2023-12-29T15:58:44.118054Z","shell.execute_reply.started":"2023-12-29T15:58:43.144947Z","shell.execute_reply":"2023-12-29T15:58:44.117155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b>\nstore with id=44(store_nbr) has the most sell\n</div>","metadata":{}},{"cell_type":"code","source":"df_transactions_nbr44=df_transactions[df_transactions.store_nbr==44]\ndf_transactions_nbr44","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:44.119200Z","iopub.execute_input":"2023-12-29T15:58:44.119516Z","iopub.status.idle":"2023-12-29T15:58:44.136807Z","shell.execute_reply.started":"2023-12-29T15:58:44.119490Z","shell.execute_reply":"2023-12-29T15:58:44.135909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nsns.lineplot(data=df_transactions_nbr44,x = \"date\", y = \"transactions\")\nplt.title(\"Store_nbr(44) and transactions on time\",\n          fontsize = 20)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:44.137900Z","iopub.execute_input":"2023-12-29T15:58:44.138203Z","iopub.status.idle":"2023-12-29T15:58:44.868781Z","shell.execute_reply.started":"2023-12-29T15:58:44.138177Z","shell.execute_reply":"2023-12-29T15:58:44.867878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nThe trend is almost in definded range\n</div>\n","metadata":{}},{"cell_type":"code","source":"df_group=df_transactions_nbr44.groupby(['year-month']).agg(\n       transactions=('transactions', 'sum'),\n       ).sort_values(by='year-month',ascending=True).reset_index()\n\ndf_group","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:44.869925Z","iopub.execute_input":"2023-12-29T15:58:44.870215Z","iopub.status.idle":"2023-12-29T15:58:44.890481Z","shell.execute_reply.started":"2023-12-29T15:58:44.870189Z","shell.execute_reply":"2023-12-29T15:58:44.889591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The last month 2017 is uncompleted so we delete it( upto 2017-08-15)","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(8, 5)) \ndf_group.drop(df_group.tail(1).index,inplace=True)\ngfg=sns.lineplot(data=df_group,x = \"year-month\", y = \"transactions\")\ngfg.set_xticks(range(1,6)) \nxtick_loc = ['2013-01', '2014-01', '2015-01','2016-01','2017-01']\nax.set_xticks(xtick_loc)\nplt.title(\"Store_nbr(44) and transactions on time\",\n          fontsize = 20)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:44.891613Z","iopub.execute_input":"2023-12-29T15:58:44.891956Z","iopub.status.idle":"2023-12-29T15:58:45.326938Z","shell.execute_reply.started":"2023-12-29T15:58:44.891924Z","shell.execute_reply":"2023-12-29T15:58:45.326021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#The last year 2017 is uncompleted so we delete it( upto 2017-08-15)\ndf_group=df_transactions_nbr44[df_transactions_nbr44[\"year\"]<2017].groupby(['year']).agg(\n       transactions=('transactions', 'sum'),\n       ).sort_values(by='year',ascending=False).reset_index()\n\nplt.figure(figsize=(5, 5))\nsns.barplot(data=df_group, x='year',\n                 y='transactions', palette=\"pastel\")\nplt.title(\"Store_nbr(44) and transactions on time\",\n          fontsize = 20)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:45.328134Z","iopub.execute_input":"2023-12-29T15:58:45.328427Z","iopub.status.idle":"2023-12-29T15:58:45.673738Z","shell.execute_reply.started":"2023-12-29T15:58:45.328401Z","shell.execute_reply":"2023-12-29T15:58:45.672298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nThe trend is almost in definded range\n</div>\n","metadata":{}},{"cell_type":"markdown","source":"  ## 4-oil  <a class=\"anchor\"  id=\"4\"></a>","metadata":{}},{"cell_type":"code","source":"df_oil.dropna(inplace=True)\ndf_oil[\"year\"]=df_oil['date']. dt.year\ndf_oil[\"month\"]=df_oil['date']. dt. month\n#format YYYY-MM\ndf_oil[\"year-month\"]=df_oil['date'].dt.strftime('%Y-%m')\ndf_oil.info()\n# df_oil.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:45.676066Z","iopub.execute_input":"2023-12-29T15:58:45.676547Z","iopub.status.idle":"2023-12-29T15:58:45.710830Z","shell.execute_reply.started":"2023-12-29T15:58:45.676503Z","shell.execute_reply":"2023-12-29T15:58:45.709983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sns.lineplot(data=df_transactions_nbr44[df_transactions_nbr44.year==2014],x = \"date\", y = \"transactions\")\nplt.figure(figsize=(10, 5))\n\nsns.lineplot(data=df_oil[df_oil.year==2014],x = \"date\", y = \"dcoilwtico\")\nplt.title(\"oil on time\",\n          fontsize = 20)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:45.711911Z","iopub.execute_input":"2023-12-29T15:58:45.712188Z","iopub.status.idle":"2023-12-29T15:58:46.263784Z","shell.execute_reply.started":"2023-12-29T15:58:45.712164Z","shell.execute_reply":"2023-12-29T15:58:46.262775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nin 2014/07 the price dopped sharp\n</div>\n","metadata":{}},{"cell_type":"code","source":"def normalize_max_min(col):\n    a_min = col.min()\n    a_max = col.max()\n    norm_data = (col - a_min) / (a_max - a_min )\n    return norm_data\n\nplt.figure(figsize=(10, 5))\ndf_transactions_nbr44[\"transactions_norm\"]=normalize_max_min(df_transactions_nbr44[\"transactions\"])\ndf_oil[\"dcoilwtico_norm\"]=normalize_max_min(df_oil[\"dcoilwtico\"])\n\nsns.lineplot(data=df_transactions_nbr44[df_transactions_nbr44.year==2014],x = \"date\",\n             y = \"transactions_norm\", label='df_transactions_nbr44')\nsns.lineplot(data=df_oil[df_oil.year==2014],x = \"date\", y = \"dcoilwtico_norm\" ,label='df_oil')\n# sns.lineplot(data=df_transactions_nbr44 ,x=df.index,y = \"a\", label='line one')\n# sns.lineplot(data=df ,x=df.index,y = \"b\", label='line one')\nplt.title(\"comparission between oil and transaction od strore_nbr(44) on time (normalized var)\",\n          fontsize = 20)\nplt.legend(loc=\"center left\")\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:46.265078Z","iopub.execute_input":"2023-12-29T15:58:46.265410Z","iopub.status.idle":"2023-12-29T15:58:46.805716Z","shell.execute_reply.started":"2023-12-29T15:58:46.265373Z","shell.execute_reply":"2023-12-29T15:58:46.804624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nThere is no relationship\n</div>\n","metadata":{}},{"cell_type":"markdown","source":"  ## 5-holidays_events  <a class=\"anchor\"  id=\"5\"></a>","metadata":{}},{"cell_type":"code","source":"df_holiday.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:46.807214Z","iopub.execute_input":"2023-12-29T15:58:46.807869Z","iopub.status.idle":"2023-12-29T15:58:46.819307Z","shell.execute_reply.started":"2023-12-29T15:58:46.807834Z","shell.execute_reply":"2023-12-29T15:58:46.818318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\ncolName=\"type\"\nval = df_holiday[colName].value_counts().sort_values(ascending=False)\nf, axs = plt.subplots(1, 1, figsize=(10, 5))\nax = sns.countplot(df_holiday, x=colName # or y\n                   , palette=\"pastel\"\n                   , order=val.index\n                   ,ax=axs\n                   )\n\nsumm = val.sum()\n\ntitle = val.index\nlbls = [f'{p[0] * 100 / summ:.1f}%' for p in zip(val)]\n\n\n# hor label\nfor p, label in zip(ax.patches, lbls):\n    ax.annotate(label, (p.get_x(), p.get_height() + 0.15))\n    \nplt.title(\"holiday distribution\",\n          fontsize = 20)    \n\nplt.tight_layout()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:46.820733Z","iopub.execute_input":"2023-12-29T15:58:46.821579Z","iopub.status.idle":"2023-12-29T15:58:47.134111Z","shell.execute_reply.started":"2023-12-29T15:58:46.821539Z","shell.execute_reply":"2023-12-29T15:58:47.133242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"  ## train  <a class=\"anchor\"  id=\"6\"></a>","metadata":{}},{"cell_type":"code","source":"df_train[\"year\"]=df_train['date']. dt.year\ndf_train[\"month\"]=df_train['date']. dt. month\n#format YYYY-MM\ndf_train[\"year-month\"]=df_train['date'].dt.strftime('%Y-%m')\ndf_train['month_name'] = df_train['date'].dt.strftime(\"%b\") #Jan\ndf_train['weekday'] = df_train['date'].dt.strftime(\"%a\") #Wed\ndf_train.info()\n# df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T15:58:47.135823Z","iopub.execute_input":"2023-12-29T15:58:47.136229Z","iopub.status.idle":"2023-12-29T16:01:02.746747Z","shell.execute_reply.started":"2023-12-29T15:58:47.136193Z","shell.execute_reply":"2023-12-29T16:01:02.745767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_group=df_train.groupby(['date']).agg(\n       unit_sales_sum=('unit_sales', 'sum'),\n       ).sort_values(by='date',ascending=True).reset_index()\n\n\nplt.figure(figsize=(10, 5))\n\n\nsns.lineplot(data=df_group, x='date',\n                 y='unit_sales_sum')\n\n\nplt.title(\"unit_sales on time\",\n          fontsize = 20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T16:01:02.748156Z","iopub.execute_input":"2023-12-29T16:01:02.748567Z","iopub.status.idle":"2023-12-29T16:01:03.413153Z","shell.execute_reply.started":"2023-12-29T16:01:02.748529Z","shell.execute_reply":"2023-12-29T16:01:03.412152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nThe trend is positive\n</div>\n","metadata":{}},{"cell_type":"code","source":"#chart setting\ncolName= \"onpromotion\"\ndf_cahrt=df_train\n\nsns.set_theme(style=\"whitegrid\")\nval = df_cahrt[colName].value_counts().sort_values(ascending=False)\n# val=df[colName].value_counts().sort_index(ascending=True)\nplt.figure(figsize=(10, 5))\nax = sns.countplot(df_cahrt, x=colName \n                   , palette=\"pastel\"\n                   # , stat=\"percent\"\n                   , order=val.index\n                   )\n\nsumm = val.sum()\n\ntitle = val.index\nlbls = [f'{p[0] * 100 / summ:.1f}%' for p in zip(val)]\n\n# vetical label x=col\nfor p, label in zip(ax.patches, lbls):\n    ax.annotate(label, (p.get_x(), p.get_height() + 0.15))\nplt.title(\"promotion percent of sold item\",\n          fontsize = 20)\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T16:01:03.419356Z","iopub.execute_input":"2023-12-29T16:01:03.419752Z","iopub.status.idle":"2023-12-29T16:01:16.122235Z","shell.execute_reply.started":"2023-12-29T16:01:03.419724Z","shell.execute_reply":"2023-12-29T16:01:16.121302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\n7.5 % of sales are promotion\n</div>\n","metadata":{}},{"cell_type":"code","source":"df_group=df_train.groupby(['store_nbr']).agg(\n       unit_sales_sum=('unit_sales', 'sum'),\n       ).sort_values(by='unit_sales_sum',ascending=False).reset_index()\n\nplt.figure(figsize=(10, 5))\nax = sns.barplot(data=df_group, x='store_nbr',\n                 y='unit_sales_sum', palette=\"pastel\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-29T16:01:16.123583Z","iopub.execute_input":"2023-12-29T16:01:16.123879Z","iopub.status.idle":"2023-12-29T16:01:17.238446Z","shell.execute_reply.started":"2023-12-29T16:01:16.123853Z","shell.execute_reply":"2023-12-29T16:01:17.237442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nstore_nbr=44 have the most amount of sale\n</div>\n","metadata":{}},{"cell_type":"code","source":"df_train_store_nbr44=df_train[df_train[\"store_nbr\"]==44]\ndf_chart=df_train_store_nbr44[df_train_store_nbr44[\"unit_sales\"]>0]\ndf_chart.describe()\nplt.figure(figsize=(10, 5))\nsns.set_style(\"whitegrid\", {'axes.grid': False})\nsns.histplot( data=df_chart,x='unit_sales', kde=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-29T16:01:17.239539Z","iopub.execute_input":"2023-12-29T16:01:17.239831Z","iopub.status.idle":"2023-12-29T16:01:28.683147Z","shell.execute_reply.started":"2023-12-29T16:01:17.239805Z","shell.execute_reply":"2023-12-29T16:01:28.682216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\n Sale is not normal distribution on this store\n</div>\n","metadata":{}},{"cell_type":"code","source":"df_pos_sale=df_train[df_train['unit_sales']>0].groupby(['store_nbr']).agg(\n       pos_unit_sales_sum=('unit_sales', 'sum'),\n       pos_unit_sales_count=('unit_sales', 'count'),\n       ).sort_values(by='store_nbr',ascending=True).reset_index()\n\n\ndf_neg_sale=df_train[df_train['unit_sales']<0].groupby(['store_nbr']).agg(\n       neg_unit_sales_sum=('unit_sales', 'sum'),\n       neg_unit_sales_count=('unit_sales', 'count'),\n       ).sort_values(by='store_nbr',ascending=True).reset_index()\n\n\ndf_merge=df_pos_sale.merge(df_neg_sale, left_on='store_nbr', right_on='store_nbr')\ndf_merge[\"neg_unit_sales_sum\"]=-1*df_merge[\"neg_unit_sales_sum\"]\ndf_merge[\"pos_sum_sale_per\"]=100*df_merge[\"pos_unit_sales_sum\"]/(df_merge[\"neg_unit_sales_sum\"]+df_merge[\"pos_unit_sales_sum\"])\ndf_merge[\"pos_count_sale_per\"]=100*df_merge[\"pos_unit_sales_count\"]/(df_merge[\"neg_unit_sales_count\"]+df_merge[\"pos_unit_sales_count\"])\n\ndf_merge","metadata":{"execution":{"iopub.status.busy":"2023-12-29T16:01:28.684666Z","iopub.execute_input":"2023-12-29T16:01:28.685037Z","iopub.status.idle":"2023-12-29T16:01:29.739501Z","shell.execute_reply.started":"2023-12-29T16:01:28.685003Z","shell.execute_reply":"2023-12-29T16:01:29.738598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-success\">\n<b>Insight:</b><br>\nNumber and emount of returns of that particular item is almost 0%\n</div>\n","metadata":{}}]}