{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.025028,"end_time":"2022-07-22T19:34:37.774958","exception":false,"start_time":"2022-07-22T19:34:37.749930","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:10:21.533547Z","iopub.execute_input":"2022-07-29T05:10:21.534098Z","iopub.status.idle":"2022-07-29T05:10:21.575570Z","shell.execute_reply.started":"2022-07-29T05:10:21.533989Z","shell.execute_reply":"2022-07-29T05:10:21.574543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let us import required libraries for the project","metadata":{"papermill":{"duration":0.005896,"end_time":"2022-07-22T19:34:37.787266","exception":false,"start_time":"2022-07-22T19:34:37.781370","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport 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":{"papermill":{"duration":8.178106,"end_time":"2022-07-22T19:34:45.971977","exception":false,"start_time":"2022-07-22T19:34:37.793871","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:10:22.898651Z","iopub.execute_input":"2022-07-29T05:10:22.899148Z","iopub.status.idle":"2022-07-29T05:10:28.938549Z","shell.execute_reply.started":"2022-07-29T05:10:22.899106Z","shell.execute_reply":"2022-07-29T05:10:28.937316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_categories_df = pd.read_csv(\"/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv\")\nshops_df = pd.read_csv(\"/kaggle/input/competitive-data-science-predict-future-sales/shops.csv\")\nsales_train_df = pd.read_csv(\"/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv\")\nitems_df = pd.read_csv(\"/kaggle/input/competitive-data-science-predict-future-sales/items.csv\")\ntest_df = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv')","metadata":{"papermill":{"duration":2.337527,"end_time":"2022-07-22T19:34:48.316058","exception":false,"start_time":"2022-07-22T19:34:45.978531","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:11:15.134481Z","iopub.execute_input":"2022-07-29T05:11:15.134843Z","iopub.status.idle":"2022-07-29T05:11:17.706475Z","shell.execute_reply.started":"2022-07-29T05:11:15.134810Z","shell.execute_reply":"2022-07-29T05:11:17.705501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let us check the train data","metadata":{"papermill":{"duration":0.005679,"end_time":"2022-07-22T19:34:48.327963","exception":false,"start_time":"2022-07-22T19:34:48.322284","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sales_train_df.head()","metadata":{"papermill":{"duration":0.03278,"end_time":"2022-07-22T19:34:48.366710","exception":false,"start_time":"2022-07-22T19:34:48.333930","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:11:20.944496Z","iopub.execute_input":"2022-07-29T05:11:20.945093Z","iopub.status.idle":"2022-07-29T05:11:20.967194Z","shell.execute_reply.started":"2022-07-29T05:11:20.945056Z","shell.execute_reply":"2022-07-29T05:11:20.966307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check the dimensions,datatyes, and other statistics of the data","metadata":{"papermill":{"duration":0.005883,"end_time":"2022-07-22T19:34:48.378769","exception":false,"start_time":"2022-07-22T19:34:48.372886","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sales_train_df.shape","metadata":{"papermill":{"duration":0.016893,"end_time":"2022-07-22T19:34:48.401798","exception":false,"start_time":"2022-07-22T19:34:48.384905","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:11:28.734830Z","iopub.execute_input":"2022-07-29T05:11:28.735188Z","iopub.status.idle":"2022-07-29T05:11:28.741500Z","shell.execute_reply.started":"2022-07-29T05:11:28.735157Z","shell.execute_reply":"2022-07-29T05:11:28.740494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_train_df.info()\n","metadata":{"papermill":{"duration":0.035147,"end_time":"2022-07-22T19:34:48.443955","exception":false,"start_time":"2022-07-22T19:34:48.408808","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:11:30.665021Z","iopub.execute_input":"2022-07-29T05:11:30.665968Z","iopub.status.idle":"2022-07-29T05:11:30.688563Z","shell.execute_reply.started":"2022-07-29T05:11:30.665920Z","shell.execute_reply":"2022-07-29T05:11:30.687321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_train_df.describe()","metadata":{"papermill":{"duration":0.497829,"end_time":"2022-07-22T19:34:48.948303","exception":false,"start_time":"2022-07-22T19:34:48.450474","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:11:31.099342Z","iopub.execute_input":"2022-07-29T05:11:31.099636Z","iopub.status.idle":"2022-07-29T05:11:31.521466Z","shell.execute_reply.started":"2022-07-29T05:11:31.099608Z","shell.execute_reply":"2022-07-29T05:11:31.520377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_train_df.isnull().sum()","metadata":{"papermill":{"duration":0.167405,"end_time":"2022-07-22T19:34:49.122425","exception":false,"start_time":"2022-07-22T19:34:48.955020","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:11:31.524014Z","iopub.execute_input":"2022-07-29T05:11:31.524542Z","iopub.status.idle":"2022-07-29T05:11:31.824015Z","shell.execute_reply.started":"2022-07-29T05:11:31.524504Z","shell.execute_reply":"2022-07-29T05:11:31.823014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Now let's change the date format to pandas date format.","metadata":{"papermill":{"duration":0.00658,"end_time":"2022-07-22T19:34:49.135892","exception":false,"start_time":"2022-07-22T19:34:49.129312","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sales_train_df['date'] = pd.to_datetime(sales_train_df['date'])\nsales_train_df['date']","metadata":{"papermill":{"duration":0.389726,"end_time":"2022-07-22T19:34:49.532771","exception":false,"start_time":"2022-07-22T19:34:49.143045","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:28.884749Z","iopub.execute_input":"2022-07-29T05:16:28.885400Z","iopub.status.idle":"2022-07-29T05:16:29.352693Z","shell.execute_reply.started":"2022-07-29T05:16:28.885365Z","shell.execute_reply":"2022-07-29T05:16:29.351560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_train_df['month_year'] = sales_train_df['date'].dt.to_period('M')\nsales_train_df['month_year']","metadata":{"papermill":{"duration":0.350477,"end_time":"2022-07-22T19:34:49.894018","exception":false,"start_time":"2022-07-22T19:34:49.543541","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:30.345044Z","iopub.execute_input":"2022-07-29T05:16:30.345998Z","iopub.status.idle":"2022-07-29T05:16:30.636597Z","shell.execute_reply.started":"2022-07-29T05:16:30.345947Z","shell.execute_reply":"2022-07-29T05:16:30.635612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Now let's consider only valid item_price and item_cnt_day","metadata":{"papermill":{"duration":0.007221,"end_time":"2022-07-22T19:34:49.914156","exception":false,"start_time":"2022-07-22T19:34:49.906935","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sales_train_df = sales_train_df[sales_train_df['item_price']>0]\nsales_train_df = sales_train_df[sales_train_df['item_cnt_day']>0]","metadata":{"papermill":{"duration":0.290125,"end_time":"2022-07-22T19:34:50.212954","exception":false,"start_time":"2022-07-22T19:34:49.922829","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:30.638386Z","iopub.execute_input":"2022-07-29T05:16:30.639055Z","iopub.status.idle":"2022-07-29T05:16:30.892977Z","shell.execute_reply.started":"2022-07-29T05:16:30.639016Z","shell.execute_reply":"2022-07-29T05:16:30.891930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_train_df","metadata":{"papermill":{"duration":0.029551,"end_time":"2022-07-22T19:34:50.249514","exception":false,"start_time":"2022-07-22T19:34:50.219963","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:30.894883Z","iopub.execute_input":"2022-07-29T05:16:30.895343Z","iopub.status.idle":"2022-07-29T05:16:30.915571Z","shell.execute_reply.started":"2022-07-29T05:16:30.895306Z","shell.execute_reply":"2022-07-29T05:16:30.914687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly_data = sales_train_df.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":{"papermill":{"duration":2.846854,"end_time":"2022-07-22T19:34:53.103526","exception":false,"start_time":"2022-07-22T19:34:50.256672","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:30.916952Z","iopub.execute_input":"2022-07-29T05:16:30.917605Z","iopub.status.idle":"2022-07-29T05:16:33.729234Z","shell.execute_reply.started":"2022-07-29T05:16:30.917561Z","shell.execute_reply":"2022-07-29T05:16:33.728153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly_data.reset_index(inplace = True)","metadata":{"papermill":{"duration":0.026356,"end_time":"2022-07-22T19:34:53.137064","exception":false,"start_time":"2022-07-22T19:34:53.110708","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:33.731336Z","iopub.execute_input":"2022-07-29T05:16:33.731699Z","iopub.status.idle":"2022-07-29T05:16:33.745996Z","shell.execute_reply.started":"2022-07-29T05:16:33.731644Z","shell.execute_reply":"2022-07-29T05:16:33.745158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = monthly_data.drop(columns= ['shop_id','item_id'], level=0)","metadata":{"papermill":{"duration":0.14371,"end_time":"2022-07-22T19:34:53.288627","exception":false,"start_time":"2022-07-22T19:34:53.144917","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:33.747348Z","iopub.execute_input":"2022-07-29T05:16:33.747805Z","iopub.status.idle":"2022-07-29T05:16:33.870097Z","shell.execute_reply.started":"2022-07-29T05:16:33.747759Z","shell.execute_reply":"2022-07-29T05:16:33.869092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.fillna(0,inplace = True)","metadata":{"papermill":{"duration":0.021483,"end_time":"2022-07-22T19:34:53.317388","exception":false,"start_time":"2022-07-22T19:34:53.295905","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:33.871620Z","iopub.execute_input":"2022-07-29T05:16:33.872215Z","iopub.status.idle":"2022-07-29T05:16:33.880674Z","shell.execute_reply.started":"2022-07-29T05:16:33.872178Z","shell.execute_reply":"2022-07-29T05:16:33.879682Z"},"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":{"papermill":{"duration":0.015587,"end_time":"2022-07-22T19:34:53.340192","exception":false,"start_time":"2022-07-22T19:34:53.324605","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:33.882271Z","iopub.execute_input":"2022-07-29T05:16:33.882644Z","iopub.status.idle":"2022-07-29T05:16:33.889023Z","shell.execute_reply.started":"2022-07-29T05:16:33.882608Z","shell.execute_reply":"2022-07-29T05:16:33.887899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rows = monthly_data.merge(\n    test_df,\n    on = ['item_id','shop_id'],\n    how = 'right')","metadata":{"papermill":{"duration":0.176176,"end_time":"2022-07-22T19:34:53.523599","exception":false,"start_time":"2022-07-22T19:34:53.347423","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:33.892129Z","iopub.execute_input":"2022-07-29T05:16:33.892628Z","iopub.status.idle":"2022-07-29T05:16:34.026718Z","shell.execute_reply.started":"2022-07-29T05:16:33.892589Z","shell.execute_reply":"2022-07-29T05:16:34.025697Z"},"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)\nx_test.fillna(0,inplace = True)","metadata":{"papermill":{"duration":0.086338,"end_time":"2022-07-22T19:34:53.617201","exception":false,"start_time":"2022-07-22T19:34:53.530863","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:34.028116Z","iopub.execute_input":"2022-07-29T05:16:34.029171Z","iopub.status.idle":"2022-07-29T05:16:34.097086Z","shell.execute_reply.started":"2022-07-29T05:16:34.029130Z","shell.execute_reply":"2022-07-29T05:16:34.096101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.expand_dims(x_test,axis = 2)\nprint(x_train.shape,y_train.shape,x_test.shape)","metadata":{"papermill":{"duration":0.016813,"end_time":"2022-07-22T19:34:53.641279","exception":false,"start_time":"2022-07-22T19:34:53.624466","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:16:34.098491Z","iopub.execute_input":"2022-07-29T05:16:34.099166Z","iopub.status.idle":"2022-07-29T05:16:34.105724Z","shell.execute_reply.started":"2022-07-29T05:16:34.099127Z","shell.execute_reply":"2022-07-29T05:16:34.104526Z"},"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":{"papermill":{"duration":0.450335,"end_time":"2022-07-22T19:34:54.099319","exception":false,"start_time":"2022-07-22T19:34:53.648984","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-29T05:17:19.049513Z","iopub.execute_input":"2022-07-29T05:17:19.050173Z","iopub.status.idle":"2022-07-29T05:17:22.218166Z","shell.execute_reply.started":"2022-07-29T05:17:19.050136Z","shell.execute_reply":"2022-07-29T05:17:22.216614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x_train, \n    y_train, \n    epochs=1000, \n    batch_size=4096,\n    verbose=1, \n    shuffle=True,\n    validation_split=0.4)","metadata":{"papermill":{"duration":285.57587,"end_time":"2022-07-22T19:39:39.682663","exception":false,"start_time":"2022-07-22T19:34:54.106793","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-28T15:27:14.457741Z","iopub.execute_input":"2022-07-28T15:27:14.458252Z","iopub.status.idle":"2022-07-28T15:29:38.389229Z","shell.execute_reply.started":"2022-07-28T15:27:14.458205Z","shell.execute_reply":"2022-07-28T15:29:38.387695Z"},"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":{"papermill":{"duration":0.27057,"end_time":"2022-07-22T19:39:39.993752","exception":false,"start_time":"2022-07-22T19:39:39.723182","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-28T15:29:38.391375Z","iopub.execute_input":"2022-07-28T15:29:38.392521Z","iopub.status.idle":"2022-07-28T15:29:38.669900Z","shell.execute_reply.started":"2022-07-28T15:29:38.392463Z","shell.execute_reply":"2022-07-28T15:29:38.668698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = model.predict(x_test)\nsubmission = pd.DataFrame({'ID':test_df['ID'],'item_cnt_month':test_predict.ravel()})\nsubmission['item_cnt_month'] = submission['item_cnt_month']\nsubmission.to_csv('submission.csv',index = False)","metadata":{"papermill":{"duration":52.305508,"end_time":"2022-07-22T19:40:32.338565","exception":false,"start_time":"2022-07-22T19:39:40.033057","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-28T15:29:38.671465Z","iopub.execute_input":"2022-07-28T15:29:38.674168Z","iopub.status.idle":"2022-07-28T15:29:51.826902Z","shell.execute_reply.started":"2022-07-28T15:29:38.674122Z","shell.execute_reply":"2022-07-28T15:29:51.825818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.linear_model import LogisticRegression\n# from sklearn.metrics import classification_report, confusion_matrix","metadata":{"papermill":{"duration":0.048377,"end_time":"2022-07-22T19:40:32.426123","exception":false,"start_time":"2022-07-22T19:40:32.377746","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-28T15:29:51.828463Z","iopub.execute_input":"2022-07-28T15:29:51.829852Z","iopub.status.idle":"2022-07-28T15:29:51.835507Z","shell.execute_reply.started":"2022-07-28T15:29:51.829777Z","shell.execute_reply":"2022-07-28T15:29:51.833911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = LogisticRegression()\n# model.fit()","metadata":{"papermill":{"duration":0.046771,"end_time":"2022-07-22T19:40:32.512247","exception":false,"start_time":"2022-07-22T19:40:32.465476","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-28T15:29:51.837535Z","iopub.execute_input":"2022-07-28T15:29:51.838040Z","iopub.status.idle":"2022-07-28T15:29:51.850284Z","shell.execute_reply.started":"2022-07-28T15:29:51.837969Z","shell.execute_reply":"2022-07-28T15:29:51.848944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}