{"cells":[{"outputs":[],"metadata":{"_uuid":"58a4f157127265fad7e7a8576a15b93d399266a0","_cell_guid":"eb39cd97-d8e1-4c38-b0f5-9bd0545e4ece"},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":1},{"metadata":{},"cell_type":"markdown","source":"Exploration of Training set\n---"},{"outputs":[],"metadata":{},"cell_type":"code","source":"df_train = pd.read_csv(\"../input/train.csv\")","execution_count":2},{"outputs":[],"metadata":{},"cell_type":"code","source":"df_train.head()","execution_count":3},{"outputs":[],"metadata":{},"cell_type":"code","source":"print(\"{} datapoints and {} features\".format(*df_train.shape))","execution_count":4},{"metadata":{},"cell_type":"markdown","source":"That is quite a few datapoints. But first, let's cast the date-feature to a pandas datetime so we can extract day, week, month, year and so on, as well as do some sampling :"},{"outputs":[],"metadata":{"collapsed":true},"cell_type":"code","source":"df_train[\"date\"] = pd.to_datetime(df_train[\"date\"])","execution_count":5},{"outputs":[],"metadata":{},"cell_type":"code","source":"ax = df_train.set_index(\"date\")[\"unit_sales\"].resample(\"M\").sum().plot(figsize=(12, 6))\n_ = ax.set_ylabel(\"unit_sales\")\n_ = ax.set_title(\"Monthly Sales Volum\")","execution_count":6},{"outputs":[],"metadata":{},"cell_type":"code","source":"ax = df_train.set_index(\"date\")[\"unit_sales\"].resample(\"W\").sum().plot(figsize=(12, 6))\n_ = ax.set_ylabel(\"unit_sales\")\n_ = ax.set_title(\"Weekly Sales Volum\")","execution_count":7},{"outputs":[],"metadata":{},"cell_type":"code","source":"ax = df_train.set_index(\"date\")[\"unit_sales\"].resample(\"D\").sum().plot(figsize=(12, 6))\n_ = ax.set_ylabel(\"unit_sales\")\n_ = ax.set_title(\"Daily Sales Volum\")","execution_count":8},{"metadata":{},"cell_type":"markdown","source":"#### We find:\n\n- A generally increasing trend, with a couple of significant dips specifically in june/july of 2014 and early 2015. The answer to that is probably somewhere in the other datasets we were provided"},{"outputs":[],"metadata":{},"cell_type":"code","source":"ax = df_train.set_index(\"date\")[\"unit_sales\"].resample(\"W\").mean().plot(figsize=(12, 6), label=\"Mean\")\nax = df_train.set_index(\"date\")[\"unit_sales\"].resample(\"W\").median().plot(figsize=(12, 6), label=\"Median\")\nax = df_train.set_index(\"date\")[\"unit_sales\"].resample(\"W\").std().plot(figsize=(12, 6), label=\"Std\")\n\n_ = ax.set_ylabel(\"unit_sales\")\n_ = plt.legend(loc=\"best\")","execution_count":9},{"metadata":{},"cell_type":"markdown","source":"#### We find:\n- Mean and median is fairly close all the way. The standard deviation has a couple of solid spikes early to mid and late 2016. A high std generally means high variation in the data. Maybe this was due to new products, or new stores opened or existing stores closed down."},{"outputs":[],"metadata":{},"cell_type":"code","source":"_ = df_train[\"unit_sales\"].apply(np.log).hist(bins=25, range=(-2, 6), figsize=(12, 6))\n_ = plt.xlabel(\"unit_sales log-transformed\")","execution_count":10},{"outputs":[],"metadata":{},"cell_type":"code","source":"_ = df_train[\"store_nbr\"].hist(bins=25, figsize=(12, 6))\n_ = plt.xlabel(\"Store ID\")","execution_count":11},{"outputs":[],"metadata":{"collapsed":true},"cell_type":"code","source":"df_train[\"year\"] = df_train[\"date\"].dt.year\ndf_train[\"week\"] = df_train[\"date\"].dt.week\ndata = pd.crosstab(df_train[\"year\"], df_train[\"week\"], df_train[\"unit_sales\"], aggfunc=\"sum\", normalize=True)\n\n_ = plt.figure(figsize=(14, 3))\n_ = sns.heatmap(data, cmap=\"viridis\")\n_ = plt.title(\"Heatmap of Sales Volume Year vs. Week\")","execution_count":null},{"outputs":[],"metadata":{"collapsed":true},"cell_type":"code","source":"","execution_count":null}],"nbformat_minor":1,"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"mimetype":"text/x-python","version":"3.6.3","nbconvert_exporter":"python","file_extension":".py","pygments_lexer":"ipython3","codemirror_mode":{"version":3,"name":"ipython"},"name":"python"}},"nbformat":4}