{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"path=\"../input/\"\ndf=pd.read_csv(path+'train.csv',parse_dates=['date'],index_col='date')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"549bdba88c0b4c681a3c616ec15dbf9e697e9d37"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0972ec502dbad24905c887070c51ee572bce3d31"},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d553977d5ed63bba964302d16fb50059a281796f"},"cell_type":"code","source":"plt.plot(df.index,df['sales'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe082899ff0a337562392e28d852f5b1cbc0feaf"},"cell_type":"code","source":"def expand_date_field(store_df):\n    store_df['day']=store_df.index.day\n    store_df['month']=store_df.index.month\n    store_df['year']=store_df.index.year\n    store_df['days_of_week']=store_df.index.dayofweek\n    return store_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e28ab621a708448039d641b5a73cd48cb813620a"},"cell_type":"code","source":"ngen_dataframe=expand_date_field(df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"363c847a560cf2da52815b4235b302fd85825115"},"cell_type":"code","source":"ngen_dataframe.store.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce5208e66d721af9da66ec1f7d920307e4fa1377"},"cell_type":"code","source":"pd.set_option('display.max_rows', 12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a7b9f47dcd72ed0d780b23c94417b0cbfc76dc3"},"cell_type":"code","source":"grounp_df=ngen_dataframe.groupby('store')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b5b3ebf47f72b00caee8ae0a937233d95b2c5fb"},"cell_type":"code","source":"import numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72b6f19744c5013b350abd3656a6ff8dce6eb17e"},"cell_type":"code","source":"agg_year_item = pd.pivot_table(ngen_dataframe, index='year', columns='item',\n                               values='sales', aggfunc=np.mean).values\nagg_year_store = pd.pivot_table(ngen_dataframe,index='year', columns='store',\n                          values='sales', aggfunc=np.mean).values\nplt.figure(figsize=(12, 5))\nplt.subplot(121)\nplt.plot(agg_year_item / agg_year_item.mean(0)[np.newaxis])\nplt.title(\"Items\")\nplt.xlabel(\"Year\")\nplt.ylabel(\"Relative Sales\")\nplt.subplot(122)\nplt.plot(agg_year_store / agg_year_store.mean(0)[np.newaxis])\nplt.title(\"Stores\")\nplt.xlabel(\"Year\")\nplt.ylabel(\"Relative Sales\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3a72d02917d3f3922a6677ba59e15a3f5c2175a4"},"cell_type":"code","source":"grand_avg = ngen_dataframe.sales.mean()\n\n# Monthly pattern\nmonth_table = pd.pivot_table(ngen_dataframe, index='month', values='sales', aggfunc=np.mean)\nmonth_table.sales /= grand_avg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bfe2dc02f37e8c4971eb6bd71be32240408395ed"},"cell_type":"code","source":"# Day of week pattern\ndow_table = pd.pivot_table(ngen_dataframe, index='days_of_week', values='sales', aggfunc=np.mean)\ndow_table.sales /= grand_avg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5ed3e02ce0473770e4428b29ea6623d7def02c2"},"cell_type":"code","source":"def slightly_better(test, submission):\n    submission[['sales']] = submission[['sales']].astype(np.float64)\n    for _, row in test.iterrows():\n        dow, month, year = row.name.dayofweek, row.name.month, row.name.year\n        item, store = row['item'], row['store']\n        base_sales = store_item_table.at[store, item]\n        mul = month_table.at[month, 'sales'] * dow_table.at[dow, 'sales']\n        pred_sales = base_sales * mul * annual_growth(year)\n        submission.at[row['id'], 'sales'] = pred_sales\n    return submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e3ee65e5db33f29ae1338ae0c1fcc44c3be7466"},"cell_type":"code","source":"store_item_table = pd.pivot_table(ngen_dataframe, index='store', columns='item',\n                                  values='sales', aggfunc=np.mean)\nstore_item_table","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c80ff76aab6af0dcf20edd1ab0f21dd3de00a641"},"cell_type":"code","source":"# Yearly growth pattern\nyear_table = pd.pivot_table(ngen_dataframe, index='year', values='sales', aggfunc=np.mean)\nyear_table /= grand_avg\n\nyears = np.arange(2013, 2019)\nannual_sales_avg = year_table.values.squeeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5b3795302f89782990365c9c8cfd91c6d84cf93"},"cell_type":"code","source":"p1 = np.poly1d(np.polyfit(years[:-1], annual_sales_avg, 1))\np2 = np.poly1d(np.polyfit(years[:-1], annual_sales_avg, 2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b957299ea61bbbb81121488d5911362d0efc0b8"},"cell_type":"code","source":"annual_growth = p2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac0241d42cfdeb4bfa1b3eb08cf381de46a7e1a0"},"cell_type":"code","source":"#test.csv\n\nsample_sub=pd.read_csv(path+'sample_submission.csv')#,parse_dates=['date'],index_col='date')\ntest=pd.read_csv(path+'test.csv',parse_dates=['date'],index_col='date')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5872b857f04070fb1b0c04a780d9b8beef588ea8"},"cell_type":"code","source":"slightly_better_pred = slightly_better(test, sample_sub.copy())\nslightly_better_pred.to_csv(\"result.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}