{"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":"# 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":"2022-07-15T14:29:36.604306Z","iopub.execute_input":"2022-07-15T14:29:36.604645Z","iopub.status.idle":"2022-07-15T14:29:36.611575Z","shell.execute_reply.started":"2022-07-15T14:29:36.604579Z","shell.execute_reply":"2022-07-15T14:29:36.610896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pmdarima","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:13:21.133173Z","iopub.execute_input":"2022-07-15T14:13:21.133592Z","iopub.status.idle":"2022-07-15T14:13:31.333950Z","shell.execute_reply.started":"2022-07-15T14:13:21.133510Z","shell.execute_reply":"2022-07-15T14:13:31.333057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n%matplotlib inline\nfrom tensorflow import keras\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:29:40.902308Z","iopub.execute_input":"2022-07-15T14:29:40.902605Z","iopub.status.idle":"2022-07-15T14:29:48.444444Z","shell.execute_reply.started":"2022-07-15T14:29:40.902582Z","shell.execute_reply":"2022-07-15T14:29:48.443523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = 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-15T14:19:01.528998Z","iopub.execute_input":"2022-07-15T14:19:01.529361Z","iopub.status.idle":"2022-07-15T14:19:03.728542Z","shell.execute_reply.started":"2022-07-15T14:19:01.529332Z","shell.execute_reply":"2022-07-15T14:19:03.727543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:19:27.448476Z","iopub.execute_input":"2022-07-15T14:19:27.448881Z","iopub.status.idle":"2022-07-15T14:19:27.459073Z","shell.execute_reply.started":"2022-07-15T14:19:27.448851Z","shell.execute_reply":"2022-07-15T14:19:27.457527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes, test.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:19:59.448543Z","iopub.execute_input":"2022-07-15T14:19:59.448917Z","iopub.status.idle":"2022-07-15T14:19:59.455832Z","shell.execute_reply.started":"2022-07-15T14:19:59.448892Z","shell.execute_reply":"2022-07-15T14:19:59.455083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly_data = train.pivot_table(\n    index = ['shop_id','item_id'],\n    values = ['item_cnt_day'],\n    columns = ['date_block_num'],\n    fill_value = 0,\n    aggfunc='sum')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:24:44.328919Z","iopub.execute_input":"2022-07-15T14:24:44.329226Z","iopub.status.idle":"2022-07-15T14:24:46.406609Z","shell.execute_reply.started":"2022-07-15T14:24:44.329203Z","shell.execute_reply":"2022-07-15T14:24:46.405550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:25:12.163189Z","iopub.execute_input":"2022-07-15T14:25:12.163504Z","iopub.status.idle":"2022-07-15T14:25:12.192514Z","shell.execute_reply.started":"2022-07-15T14:25:12.163480Z","shell.execute_reply":"2022-07-15T14:25:12.191720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly_data.tail(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:25:25.127364Z","iopub.execute_input":"2022-07-15T14:25:25.127697Z","iopub.status.idle":"2022-07-15T14:25:25.151629Z","shell.execute_reply.started":"2022-07-15T14:25:25.127669Z","shell.execute_reply":"2022-07-15T14:25:25.150689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly_data.reset_index(inplace = True)\nmonthly_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:25:38.168434Z","iopub.execute_input":"2022-07-15T14:25:38.168781Z","iopub.status.idle":"2022-07-15T14:25:38.197228Z","shell.execute_reply.started":"2022-07-15T14:25:38.168751Z","shell.execute_reply":"2022-07-15T14:25:38.196130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = monthly_data.drop(columns= ['shop_id','item_id'], level=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:25:53.287498Z","iopub.execute_input":"2022-07-15T14:25:53.287898Z","iopub.status.idle":"2022-07-15T14:25:53.349912Z","shell.execute_reply.started":"2022-07-15T14:25:53.287873Z","shell.execute_reply":"2022-07-15T14:25:53.348988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:26:13.867646Z","iopub.execute_input":"2022-07-15T14:26:13.868520Z","iopub.status.idle":"2022-07-15T14:26:13.891286Z","shell.execute_reply.started":"2022-07-15T14:26:13.868479Z","shell.execute_reply":"2022-07-15T14:26:13.890166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.fillna(0,inplace = True)\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:26:26.843595Z","iopub.execute_input":"2022-07-15T14:26:26.843976Z","iopub.status.idle":"2022-07-15T14:26:26.868335Z","shell.execute_reply.started":"2022-07-15T14:26:26.843953Z","shell.execute_reply":"2022-07-15T14:26:26.867452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.expand_dims(train_data.values[:,:-1],axis = 2)\ny_train = train_data.values[:,-1:]","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:26:48.338294Z","iopub.execute_input":"2022-07-15T14:26:48.338648Z","iopub.status.idle":"2022-07-15T14:26:48.344246Z","shell.execute_reply.started":"2022-07-15T14:26:48.338619Z","shell.execute_reply":"2022-07-15T14:26:48.343286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rows = monthly_data.merge(\n    test,\n    on = ['item_id','shop_id'],\n    how = 'right')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:27:12.913507Z","iopub.execute_input":"2022-07-15T14:27:12.913884Z","iopub.status.idle":"2022-07-15T14:27:13.047103Z","shell.execute_reply.started":"2022-07-15T14:27:12.913859Z","shell.execute_reply":"2022-07-15T14:27:13.046008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rows.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:27:24.976514Z","iopub.execute_input":"2022-07-15T14:27:24.976824Z","iopub.status.idle":"2022-07-15T14:27:25.005522Z","shell.execute_reply.started":"2022-07-15T14:27:24.976800Z","shell.execute_reply":"2022-07-15T14:27:25.004921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_rows.drop(test_rows.columns[:5], axis=1).drop('ID', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:27:46.808379Z","iopub.execute_input":"2022-07-15T14:27:46.808685Z","iopub.status.idle":"2022-07-15T14:27:46.835518Z","shell.execute_reply.started":"2022-07-15T14:27:46.808661Z","shell.execute_reply":"2022-07-15T14:27:46.834906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:27:58.008830Z","iopub.execute_input":"2022-07-15T14:27:58.009649Z","iopub.status.idle":"2022-07-15T14:27:58.055346Z","shell.execute_reply.started":"2022-07-15T14:27:58.009623Z","shell.execute_reply":"2022-07-15T14:27:58.054571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.fillna(0,inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:28:10.963459Z","iopub.execute_input":"2022-07-15T14:28:10.963770Z","iopub.status.idle":"2022-07-15T14:28:10.995184Z","shell.execute_reply.started":"2022-07-15T14:28:10.963740Z","shell.execute_reply":"2022-07-15T14:28:10.994355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:28:22.837500Z","iopub.execute_input":"2022-07-15T14:28:22.837824Z","iopub.status.idle":"2022-07-15T14:28:22.873953Z","shell.execute_reply.started":"2022-07-15T14:28:22.837802Z","shell.execute_reply":"2022-07-15T14:28:22.873126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.expand_dims(x_test,axis = 2)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:28:41.773534Z","iopub.execute_input":"2022-07-15T14:28:41.773860Z","iopub.status.idle":"2022-07-15T14:28:41.778567Z","shell.execute_reply.started":"2022-07-15T14:28:41.773837Z","shell.execute_reply":"2022-07-15T14:28:41.777783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape,y_train.shape,x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:28:53.132201Z","iopub.execute_input":"2022-07-15T14:28:53.132495Z","iopub.status.idle":"2022-07-15T14:28:53.137870Z","shell.execute_reply.started":"2022-07-15T14:28:53.132473Z","shell.execute_reply":"2022-07-15T14:28:53.136975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential()    \nmodel.add(LSTM(64, input_shape=(33, 1), return_sequences=False))\nmodel.add(Dense(1))\n    \nmodel.compile(\n    loss = 'mse',\n    optimizer = 'adam', \n    metrics = ['mean_squared_error']        \n)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:30:08.974236Z","iopub.execute_input":"2022-07-15T14:30:08.974870Z","iopub.status.idle":"2022-07-15T14:30:09.307754Z","shell.execute_reply.started":"2022-07-15T14:30:08.974840Z","shell.execute_reply":"2022-07-15T14:30:09.306652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x_train, \n    y_train, \n    epochs=10, \n    batch_size=4096,\n    verbose=1, \n    shuffle=True,\n    validation_split=0.4)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:30:38.893940Z","iopub.execute_input":"2022-07-15T14:30:38.894264Z","iopub.status.idle":"2022-07-15T14:34:55.947225Z","shell.execute_reply.started":"2022-07-15T14:30:38.894239Z","shell.execute_reply":"2022-07-15T14:34:55.946524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"], color=\"r\")\nplt.plot(history.history[\"val_loss\"], color=\"g\")\nplt.legend([\"Training\", \"Validation\"])\nplt.xlabel(\"epochs\")\nplt.ylabel(\"loss\")\nplt.title('Evaluation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:36:42.964450Z","iopub.execute_input":"2022-07-15T14:36:42.964775Z","iopub.status.idle":"2022-07-15T14:36:43.125023Z","shell.execute_reply.started":"2022-07-15T14:36:42.964741Z","shell.execute_reply":"2022-07-15T14:36:43.124427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:36:46.242547Z","iopub.execute_input":"2022-07-15T14:36:46.243574Z","iopub.status.idle":"2022-07-15T14:37:29.247195Z","shell.execute_reply.started":"2022-07-15T14:36:46.243540Z","shell.execute_reply":"2022-07-15T14:37:29.246211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ID':test['ID'],'item_cnt_month':test_predict.ravel()})\nsubmission['item_cnt_month'] = submission['item_cnt_month']\nsubmission.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T14:37:52.327578Z","iopub.execute_input":"2022-07-15T14:37:52.327961Z","iopub.status.idle":"2022-07-15T14:37:52.658117Z","shell.execute_reply.started":"2022-07-15T14:37:52.327935Z","shell.execute_reply":"2022-07-15T14:37:52.657194Z"},"trusted":true},"execution_count":null,"outputs":[]}]}