text_html_output_1.png
from keras.layers import Dense

from keras.layers import LSTM

from keras.models import Sequential

import pandas as pd

train = pd.read_csv('../input/sales_train.csv')

test = pd.read_csv('../input/test.csv')

items_cats = pd.read_csv('../input/item_categories.csv')

items = pd.read_csv('../input/items.csv')

shops = pd.read_csv('../input/shops.csv')

train.columns.values

shops_train = train.groupby(['shop_id']).groups.keys()
len(shops_train)

item_train = train.groupby(['item_id']).groups.keys()
len(item_train)

shops_test = test.groupby(['shop_id']).groups.keys()
len(shops_test)

items_test = test.groupby(['item_id']).groups.keys()
len(items_test)

train_df = train.groupby(['shop_id', 'item_id', 'date_block_num']).sum().reset_index().sort_values(by=['item_id', 'shop_id'])

train_df['m1'] = train_df.groupby(['shop_id', 'item_id']).item_cnt_day.shift()
train_df['m1'].fillna(0, inplace=True)
train_df

train_df['m2'] = train_df.groupby(['shop_id', 'item_id']).m1.shift()
train_df['m2'].fillna(0, inplace=True)

train_df.rename(columns={'item_cnt_day': 'item_cnt_month'}, inplace=True)

finalDf = train_df[['shop_id', 'item_id', 'date_block_num', 'm1', 'm2', 'item_cnt_month']].reset_index()
finalDf.drop(['index'], axis=1, inplace=True)

newTest = pd.merge_asof(test, finalDf, left_index=True, right_index=True, on=['shop_id', 'item_id'])

model_lstm = Sequential()
model_lstm.add(LSTM(64, input_shape=(1, 4)))
model_lstm.add(Dense(1))
model_lstm.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

y_train = finalDf['item_cnt_month']
newTest.drop(['item_cnt_month'], axis=1, inplace=True)
x_train = finalDf[['shop_id', 'item_id', 'm1', 'm2']]

history = model_lstm.fit(x_train_reshaped, y_train, epochs=20, batch_size=100, shuffle=False)