{"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":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.simplefilter('ignore')\n\nimport seaborn as sns\nimport sys\nimport itertools\nimport gc\nimport datetime\n\nfrom sklearn.model_selection import train_test_split\nfrom lightgbm import LGBMRegressor\n\nimport csv\nfrom collections import OrderedDict\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-21T16:21:18.190051Z","iopub.execute_input":"2022-07-21T16:21:18.191477Z","iopub.status.idle":"2022-07-21T16:21:20.844730Z","shell.execute_reply.started":"2022-07-21T16:21:18.191347Z","shell.execute_reply":"2022-07-21T16:21:20.843480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items = pd.read_csv(\"../input/competitive-data-science-predict-future-sales/items.csv\")\nshops = pd.read_csv(\"../input/competitive-data-science-predict-future-sales/shops.csv\")\ncats = pd.read_csv('../input/competitive-data-science-predict-future-sales/item_categories.csv')\ntrain = pd.read_csv(\"../input/competitive-data-science-predict-future-sales/sales_train.csv\")\ntest = pd.read_csv(\"../input/competitive-data-science-predict-future-sales/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:21:37.126122Z","iopub.execute_input":"2022-07-21T16:21:37.126515Z","iopub.status.idle":"2022-07-21T16:21:40.025322Z","shell.execute_reply.started":"2022-07-21T16:21:37.126475Z","shell.execute_reply":"2022-07-21T16:21:40.024279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_df(dataframe,head=5):\n    print(\"#### Shape #### \")\n    print(dataframe.shape)\n    print(\"### Types ###\")\n    print(dataframe.dtypes)\n    print(\"### Head ###\")\n    print(dataframe.head(head))\n    print(\"### Tail ###\")\n    print(dataframe.tail(head))\n    print(\"### NA ###\")\n    print(dataframe.isnull().sum())\n    print(\"### Quantiles ###\")\n    print(dataframe.describe([0, 0.05,0.5,0.95,0.99,1]).T)\n\ncheck_df(train)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:22:18.193796Z","iopub.execute_input":"2022-07-21T16:22:18.194206Z","iopub.status.idle":"2022-07-21T16:22:19.108108Z","shell.execute_reply.started":"2022-07-21T16:22:18.194173Z","shell.execute_reply":"2022-07-21T16:22:19.107037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"date\"] = pd.to_datetime(train[\"date\"], format=\"%d.%m.%Y\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:23:23.888122Z","iopub.execute_input":"2022-07-21T16:23:23.888560Z","iopub.status.idle":"2022-07-21T16:23:24.418904Z","shell.execute_reply.started":"2022-07-21T16:23:23.888525Z","shell.execute_reply":"2022-07-21T16:23:24.417786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"date\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:23:31.385059Z","iopub.execute_input":"2022-07-21T16:23:31.385484Z","iopub.status.idle":"2022-07-21T16:23:31.396070Z","shell.execute_reply.started":"2022-07-21T16:23:31.385446Z","shell.execute_reply":"2022-07-21T16:23:31.395079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[(train[\"item_price\"] > 0) & (train[\"item_price\"] < 10000)]\ntrain = train[(train[\"item_cnt_day\"] > 0) & (train[\"item_cnt_day\"] < 1000)]","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:25:57.829161Z","iopub.execute_input":"2022-07-21T16:25:57.829570Z","iopub.status.idle":"2022-07-21T16:25:58.111990Z","shell.execute_reply.started":"2022-07-21T16:25:57.829542Z","shell.execute_reply":"2022-07-21T16:25:58.110970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:26:01.508835Z","iopub.execute_input":"2022-07-21T16:26:01.509221Z","iopub.status.idle":"2022-07-21T16:26:01.526264Z","shell.execute_reply.started":"2022-07-21T16:26:01.509189Z","shell.execute_reply":"2022-07-21T16:26:01.525063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['month_year'] = train['date'].dt.to_period('M')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:28:42.664130Z","iopub.execute_input":"2022-07-21T16:28:42.670362Z","iopub.status.idle":"2022-07-21T16:28:42.996352Z","shell.execute_reply.started":"2022-07-21T16:28:42.670314Z","shell.execute_reply":"2022-07-21T16:28:42.995535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_df = train.groupby(['month_year'])['month_year','item_cnt_day'].agg({'item_cnt_day':'sum'})\ngrouped_df = grouped_df.reset_index()\ngrouped_df.set_index(['month_year'], inplace=True)\ngrouped_df.rename(columns = {'item_cnt_day':'item_cnt_month'}, inplace = True)\n# grouped_df = grouped_df.to_timestamp()\ngrouped_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:29:20.977604Z","iopub.execute_input":"2022-07-21T16:29:20.977968Z","iopub.status.idle":"2022-07-21T16:29:21.065269Z","shell.execute_reply.started":"2022-07-21T16:29:20.977941Z","shell.execute_reply":"2022-07-21T16:29:21.064074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pmdarima > /dev/null","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:31:12.089897Z","iopub.execute_input":"2022-07-21T16:31:12.090269Z","iopub.status.idle":"2022-07-21T16:31:26.745477Z","shell.execute_reply.started":"2022-07-21T16:31:12.090241Z","shell.execute_reply":"2022-07-21T16:31:26.744413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pmdarima as pm\nfrom pmdarima.arima import auto_arima\nmodel = auto_arima(\n    y=grouped_df,\n    seasonal=True,\n    start_p = 1, max_p =5,\n    start_q =1, max_q =5,\n    d = None,\n    start_P = 1, max_P =5,\n    start_Q =1, max_Q =5,\n    D = None,\n    m=12,)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:31:35.525358Z","iopub.execute_input":"2022-07-21T16:31:35.525764Z","iopub.status.idle":"2022-07-21T16:31:38.262965Z","shell.execute_reply.started":"2022-07-21T16:31:35.525734Z","shell.execute_reply":"2022-07-21T16:31:38.261565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:31:44.437972Z","iopub.execute_input":"2022-07-21T16:31:44.438346Z","iopub.status.idle":"2022-07-21T16:31:44.452982Z","shell.execute_reply.started":"2022-07-21T16:31:44.438318Z","shell.execute_reply":"2022-07-21T16:31:44.451584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction, confint = model.predict(n_periods=12, return_conf_int=True)\nconfint_df = pd.DataFrame(confint)\nprediction","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:31:51.345672Z","iopub.execute_input":"2022-07-21T16:31:51.346092Z","iopub.status.idle":"2022-07-21T16:31:51.362964Z","shell.execute_reply.started":"2022-07-21T16:31:51.346058Z","shell.execute_reply":"2022-07-21T16:31:51.362091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"period_index = pd.period_range(\n    start = grouped_df.index[-1],\n    periods = 12,\n    freq='M'\n)\npredicted_df = pd.DataFrame({'value':prediction}, index=period_index)\npredicted_df","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:31:58.873478Z","iopub.execute_input":"2022-07-21T16:31:58.873885Z","iopub.status.idle":"2022-07-21T16:31:58.888660Z","shell.execute_reply.started":"2022-07-21T16:31:58.873843Z","shell.execute_reply":"2022-07-21T16:31:58.887222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 8))\nplt.plot(grouped_df.to_timestamp(), label='Actual data')\nplt.plot(predicted_df.to_timestamp(), color='orange', label='Predicted data')\nplt.fill_between(period_index.to_timestamp(), confint_df[0], confint_df[1],color='grey',alpha=.2, label='Confidence Intervals Area')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:32:07.017749Z","iopub.execute_input":"2022-07-21T16:32:07.018838Z","iopub.status.idle":"2022-07-21T16:32:07.341358Z","shell.execute_reply.started":"2022-07-21T16:32:07.018802Z","shell.execute_reply":"2022-07-21T16:32:07.339966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'sales last month: {grouped_df.values[-1][0]}')\nprint(f'sales next month: {prediction[0]}')","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:32:14.836100Z","iopub.execute_input":"2022-07-21T16:32:14.836507Z","iopub.status.idle":"2022-07-21T16:32:14.842259Z","shell.execute_reply.started":"2022-07-21T16:32:14.836475Z","shell.execute_reply":"2022-07-21T16:32:14.841386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group_pair_train = train.groupby(['shop_id', 'item_id'])['date', 'item_cnt_day'].agg({'item_cnt_day':'sum'})\ngroup_pair_train = group_pair_train.reset_index()\ngroup_pair_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:32:36.759134Z","iopub.execute_input":"2022-07-21T16:32:36.759548Z","iopub.status.idle":"2022-07-21T16:32:37.157929Z","shell.execute_reply.started":"2022-07-21T16:32:36.759519Z","shell.execute_reply":"2022-07-21T16:32:37.156746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['item_cnt_month'] = (prediction[0]*len(test)/len(group_pair_train))/len(test)\nsubmission  = test.drop(['shop_id', 'item_id'], axis=1)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T16:32:59.450665Z","iopub.execute_input":"2022-07-21T16:32:59.451073Z","iopub.status.idle":"2022-07-21T16:33:00.487518Z","shell.execute_reply.started":"2022-07-21T16:32:59.451009Z","shell.execute_reply":"2022-07-21T16:33:00.486641Z"},"trusted":true},"execution_count":null,"outputs":[]}]}