{"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":"markdown","source":"# 1. Import Packages","metadata":{}},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\nimport os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sbn\nfrom collections import Counter\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_error\nimport numpy as np\nimport datetime as dt\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import LSTM, Dropout, Dense, BatchNormalization\nfrom tensorflow.keras.models import Sequential\nimport warnings","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:45.127062Z","iopub.execute_input":"2022-07-05T02:32:45.128087Z","iopub.status.idle":"2022-07-05T02:32:53.541784Z","shell.execute_reply.started":"2022-07-05T02:32:45.127860Z","shell.execute_reply":"2022-07-05T02:32:53.540536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Load Files","metadata":{}},{"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\")\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:53.544012Z","iopub.execute_input":"2022-07-05T02:32:53.544939Z","iopub.status.idle":"2022-07-05T02:32:56.436574Z","shell.execute_reply.started":"2022-07-05T02:32:53.544894Z","shell.execute_reply":"2022-07-05T02:32:56.434971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Looking at the data. There is no daily transactions of the items. so, we need to focus on monthly sales**","metadata":{}},{"cell_type":"code","source":"#Shape\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:56.438850Z","iopub.execute_input":"2022-07-05T02:32:56.439299Z","iopub.status.idle":"2022-07-05T02:32:56.459794Z","shell.execute_reply.started":"2022-07-05T02:32:56.439254Z","shell.execute_reply":"2022-07-05T02:32:56.448459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dtypes\ntrain.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:56.464463Z","iopub.execute_input":"2022-07-05T02:32:56.464933Z","iopub.status.idle":"2022-07-05T02:32:56.482324Z","shell.execute_reply.started":"2022-07-05T02:32:56.464889Z","shell.execute_reply":"2022-07-05T02:32:56.480358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. EDA Analysis","metadata":{}},{"cell_type":"code","source":"#Train Statistics\ntrain.describe().style.background_gradient()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:56.485145Z","iopub.execute_input":"2022-07-05T02:32:56.485711Z","iopub.status.idle":"2022-07-05T02:32:57.008021Z","shell.execute_reply.started":"2022-07-05T02:32:56.485612Z","shell.execute_reply":"2022-07-05T02:32:57.006508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test Statistics\ntest.describe().style.background_gradient()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:57.010343Z","iopub.execute_input":"2022-07-05T02:32:57.010809Z","iopub.status.idle":"2022-07-05T02:32:57.050681Z","shell.execute_reply.started":"2022-07-05T02:32:57.010768Z","shell.execute_reply":"2022-07-05T02:32:57.049699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Frequency of Shop ID in train\nplt.figure(figsize=(20,5))\nax=sbn.countplot(data=train, x='shop_id')\nax.set_xticklabels(ax.get_xticklabels(), rotation=90)\nax.set_title(\"Frequency of Shop ID in train data\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:57.052392Z","iopub.execute_input":"2022-07-05T02:32:57.052848Z","iopub.status.idle":"2022-07-05T02:32:58.141957Z","shell.execute_reply.started":"2022-07-05T02:32:57.052819Z","shell.execute_reply":"2022-07-05T02:32:58.140562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This shows few of the shops have daily transaction(sold/cancelled) of the items over the period January 2013 to october 2015**","metadata":{}},{"cell_type":"code","source":"#Frequency of Shop ID in test\nplt.figure(figsize=(20,5))\nax=sbn.countplot(data=test, x='shop_id')\nax.set_xticklabels(ax.get_xticklabels(), rotation=90)\nax.set_title(\"Frequency of Shop ID in test data\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:58.143917Z","iopub.execute_input":"2022-07-05T02:32:58.144722Z","iopub.status.idle":"2022-07-05T02:32:58.777079Z","shell.execute_reply.started":"2022-07-05T02:32:58.144653Z","shell.execute_reply":"2022-07-05T02:32:58.775681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Some shops didn't sell the mentioned items in November 2015**","metadata":{}},{"cell_type":"code","source":"#10 most items returned by customers\ncounter = pd.DataFrame(Counter(train[train['item_cnt_day']<0]['item_id']).most_common(10))\ncounter.columns=['item_id', 'Counts']\ncounter['item_id']=counter['item_id'].astype('str')\nplt.figure(figsize=(20,5))\nplt.barh(data=counter, y='item_id', width='Counts', color='blue')\nplt.title(\"10 most Items returned by the customers\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:58.779655Z","iopub.execute_input":"2022-07-05T02:32:58.781125Z","iopub.status.idle":"2022-07-05T02:32:59.037992Z","shell.execute_reply.started":"2022-07-05T02:32:58.781077Z","shell.execute_reply":"2022-07-05T02:32:59.036683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Items returned to the shop by customers\nplt.figure(figsize=(20,5))\nax=sbn.countplot(data=train[train['item_cnt_day']<0], x='shop_id')\nax.set_xticklabels(ax.get_xticklabels(), rotation=90)\nax.set_title(\"Items returned to the shop by customers\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:32:59.044165Z","iopub.execute_input":"2022-07-05T02:32:59.044496Z","iopub.status.idle":"2022-07-05T02:33:00.044984Z","shell.execute_reply.started":"2022-07-05T02:32:59.044465Z","shell.execute_reply":"2022-07-05T02:33:00.043667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Items were returned to most of the shops**","metadata":{}},{"cell_type":"code","source":"#Total number of items sold/cancelled in the perood Jan 2013 to Oct 2015\nlen(train.item_id.unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:00.050363Z","iopub.execute_input":"2022-07-05T02:33:00.053382Z","iopub.status.idle":"2022-07-05T02:33:00.125960Z","shell.execute_reply.started":"2022-07-05T02:33:00.053336Z","shell.execute_reply":"2022-07-05T02:33:00.124675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Total number of items sold/cancelled in the Novemeber 2015\nlen(test.item_id.unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:00.131842Z","iopub.execute_input":"2022-07-05T02:33:00.139244Z","iopub.status.idle":"2022-07-05T02:33:00.157572Z","shell.execute_reply.started":"2022-07-05T02:33:00.139202Z","shell.execute_reply":"2022-07-05T02:33:00.155979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#List of Items sold/cancelled in Novemeber 2015 but not sold/cancelled in the perood Jan 2013 to Oct 2015 \ncolumns = set(test['item_id'])-set(train['item_id'])\ncolumns","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-05T02:33:00.159064Z","iopub.execute_input":"2022-07-05T02:33:00.162423Z","iopub.status.idle":"2022-07-05T02:33:01.103103Z","shell.execute_reply.started":"2022-07-05T02:33:00.162375Z","shell.execute_reply":"2022-07-05T02:33:01.101697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Data Preparation","metadata":{}},{"cell_type":"code","source":"#Remove unwanted data\ntest_data = test.copy(deep=True)\ntrain=train.drop(['date_block_num','item_price'], axis=1)\ntest=test.drop(['ID'], axis=1)\n#Since we have to do prediction on Novmber Sales so I assigned date_block_num as 34\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:01.105070Z","iopub.execute_input":"2022-07-05T02:33:01.106256Z","iopub.status.idle":"2022-07-05T02:33:01.185151Z","shell.execute_reply.started":"2022-07-05T02:33:01.106212Z","shell.execute_reply":"2022-07-05T02:33:01.183886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Missing Values in train data\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:01.188157Z","iopub.execute_input":"2022-07-05T02:33:01.188590Z","iopub.status.idle":"2022-07-05T02:33:01.591699Z","shell.execute_reply.started":"2022-07-05T02:33:01.188546Z","shell.execute_reply":"2022-07-05T02:33:01.590302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Missing Values in test data\ntest.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:01.593434Z","iopub.execute_input":"2022-07-05T02:33:01.594189Z","iopub.status.idle":"2022-07-05T02:33:01.614113Z","shell.execute_reply.started":"2022-07-05T02:33:01.594144Z","shell.execute_reply":"2022-07-05T02:33:01.607554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#type cast the date and change the date into monthlywise\ndef get_month(x):\n    return dt.datetime(x.year, x.month, 1)\ntrain['date']=[x.replace('.', '-') for x in train['date']]\ntrain['date']=pd.to_datetime(train['date'], format='%d-%m-%Y')\ntrain['date']=train['date'].apply(get_month)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:01.615549Z","iopub.execute_input":"2022-07-05T02:33:01.616245Z","iopub.status.idle":"2022-07-05T02:33:28.999084Z","shell.execute_reply.started":"2022-07-05T02:33:01.616201Z","shell.execute_reply":"2022-07-05T02:33:28.997619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#group by 'date_block_num', 'shop_id', 'item_id'\ntrain=train.groupby(['date', 'shop_id', 'item_id']).agg({'item_cnt_day': 'sum'})\ntrain.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:29.001346Z","iopub.execute_input":"2022-07-05T02:33:29.002221Z","iopub.status.idle":"2022-07-05T02:33:29.675798Z","shell.execute_reply.started":"2022-07-05T02:33:29.002176Z","shell.execute_reply":"2022-07-05T02:33:29.674469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Merge two columns\ntrain['shop_item']=train['shop_id'].astype('str')+'_'+train['item_id'].astype('str')\ntest['shop_item']=test['shop_id'].astype('str')+'_'+test['item_id'].astype('str')\ntrain = train.drop(['shop_id', 'item_id'], axis=1)\ntest = test.drop(['shop_id', 'item_id'], axis=1)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:29.677205Z","iopub.execute_input":"2022-07-05T02:33:29.678505Z","iopub.status.idle":"2022-07-05T02:33:35.083354Z","shell.execute_reply.started":"2022-07-05T02:33:29.678463Z","shell.execute_reply":"2022-07-05T02:33:35.082168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train.merge(test, how='outer', on='shop_item').fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:35.088507Z","iopub.execute_input":"2022-07-05T02:33:35.094204Z","iopub.status.idle":"2022-07-05T02:33:47.547774Z","shell.execute_reply.started":"2022-07-05T02:33:35.094140Z","shell.execute_reply":"2022-07-05T02:33:47.546394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings('ignore')\ndata.date[data.date==0]=pd.to_datetime('2015-10-01')\ndata","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:47.549727Z","iopub.execute_input":"2022-07-05T02:33:47.550399Z","iopub.status.idle":"2022-07-05T02:33:47.929501Z","shell.execute_reply.started":"2022-07-05T02:33:47.550339Z","shell.execute_reply":"2022-07-05T02:33:47.928077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.pivot_table(index='date', columns='shop_item').item_cnt_day.fillna(0)\ndata","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:47.931354Z","iopub.execute_input":"2022-07-05T02:33:47.932589Z","iopub.status.idle":"2022-07-05T02:33:54.889515Z","shell.execute_reply.started":"2022-07-05T02:33:47.932544Z","shell.execute_reply":"2022-07-05T02:33:54.888018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Select only test shop and item\ndata=data.loc[:,test['shop_item']]\ndata","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:54.891623Z","iopub.execute_input":"2022-07-05T02:33:54.892076Z","iopub.status.idle":"2022-07-05T02:33:55.197720Z","shell.execute_reply.started":"2022-07-05T02:33:54.892034Z","shell.execute_reply":"2022-07-05T02:33:55.196491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_past=1\nn_future=1\ntrainX=[]\ntrainY=[]\ntestX=[]\nfor i in range(n_past, len(np.array(data)) - n_future +1):\n    trainY.append(np.array(data)[i + n_future - 1:i + n_future])\n    trainX.append(np.array(data)[(i - n_past):i, 0:np.array(data).shape[1]])\ntrainX, trainY = np.array(trainX), np.array(trainY)\ntrainX.shape, trainY.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T02:33:55.199372Z","iopub.execute_input":"2022-07-05T02:33:55.200690Z","iopub.status.idle":"2022-07-05T02:33:57.823990Z","shell.execute_reply.started":"2022-07-05T02:33:55.200605Z","shell.execute_reply":"2022-07-05T02:33:57.822480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Model Building","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(LSTM(16, activation='tanh', input_shape=(trainX.shape[1], trainX.shape[2]), batch_input_shape=(1, trainX.shape[1], trainX.shape[2]),return_sequences=True, stateful=True))\nmodel.add(LSTM(16, activation='tanh', return_sequences=False, stateful=False))\nmodel.add(Dense(16, activation='tanh'))\nmodel.add(Dense(trainY.shape[2], activation='linear'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:31:07.836182Z","iopub.execute_input":"2022-07-05T03:31:07.836584Z","iopub.status.idle":"2022-07-05T03:31:08.518032Z","shell.execute_reply.started":"2022-07-05T03:31:07.836533Z","shell.execute_reply":"2022-07-05T03:31:08.516567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='huber_loss', optimizer=Adam(learning_rate=0.00005, clipnorm=1.0), metrics='mse')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:14:15.649675Z","iopub.execute_input":"2022-07-05T03:14:15.650085Z","iopub.status.idle":"2022-07-05T03:14:15.665941Z","shell.execute_reply.started":"2022-07-05T03:14:15.650056Z","shell.execute_reply":"2022-07-05T03:14:15.664352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(trainX, trainY, epochs=2000, batch_size=1, validation_split=0.10, verbose=1)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-05T03:14:24.667820Z","iopub.execute_input":"2022-07-05T03:14:24.668208Z","iopub.status.idle":"2022-07-05T03:28:49.589051Z","shell.execute_reply.started":"2022-07-05T03:14:24.668178Z","shell.execute_reply":"2022-07-05T03:28:49.587799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plot the loss curve\nplt.figure(figsize=(20,5))\nplt.plot(history.history['loss'], label='loss')\nplt.plot(history.history['val_loss'], label='val_loss')\nplt.legend(loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:28:49.592958Z","iopub.execute_input":"2022-07-05T03:28:49.593753Z","iopub.status.idle":"2022-07-05T03:28:49.834789Z","shell.execute_reply.started":"2022-07-05T03:28:49.593707Z","shell.execute_reply":"2022-07-05T03:28:49.833574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Submission to Kaggle","metadata":{}},{"cell_type":"code","source":"test_pr=np.array(data.loc['2015-10-01', :]).reshape(1,1,data.shape[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:29:03.397079Z","iopub.execute_input":"2022-07-05T03:29:03.397499Z","iopub.status.idle":"2022-07-05T03:29:03.408297Z","shell.execute_reply.started":"2022-07-05T03:29:03.397469Z","shell.execute_reply":"2022-07-05T03:29:03.406689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_pr, batch_size=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:29:15.508445Z","iopub.execute_input":"2022-07-05T03:29:15.508836Z","iopub.status.idle":"2022-07-05T03:29:15.561110Z","shell.execute_reply.started":"2022-07-05T03:29:15.508806Z","shell.execute_reply":"2022-07-05T03:29:15.559786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction on test set\ntest_data['item_cnt_month'] = pred.reshape(trainY.shape[2])\ntest_data['item_cnt_month']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:29:19.388104Z","iopub.execute_input":"2022-07-05T03:29:19.388945Z","iopub.status.idle":"2022-07-05T03:29:19.411014Z","shell.execute_reply.started":"2022-07-05T03:29:19.388873Z","shell.execute_reply":"2022-07-05T03:29:19.409553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[['ID', 'item_cnt_month']].to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T03:29:36.139571Z","iopub.execute_input":"2022-07-05T03:29:36.140033Z","iopub.status.idle":"2022-07-05T03:29:36.698837Z","shell.execute_reply.started":"2022-07-05T03:29:36.140005Z","shell.execute_reply":"2022-07-05T03:29:36.697310Z"},"trusted":true},"execution_count":null,"outputs":[]}]}