Kaggle competition: Predict Future Sales
y_pred = model.predict(X_val, batch_size = 64)
 y_pred = np.argmax(y_pred,axis = 1)
 y_pred = pd.Series(y_pred,name="Label")
 y_pred<predict_on_test>
items = pd.read_csv('.. /input/items.csv')
 shops = pd.read_csv('.. /input/shops.csv')
 cats = pd.read_csv('.. /input/item_categories.csv')
 train = pd.read_csv('.. /input/sales_train.csv')
 test = pd.read_csv('.. /input/test.csv' ).set_index('ID' )
y_pred1 = model.predict(test, batch_size = 64)
 y_pred1 = np.argmax(y_pred1,axis = 1)
 y_pred1 = pd.Series(y_pred1,name="Label")
 y_pred1<save_to_csv>
train = train[train.item_price<100000]
train = train[train.item_cnt_day<1001]
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),y_pred1],axis = 1)
 submission.to_csv("submission.csv",index=False )<import_modules>
median = train[(train.shop_id==32)&(train.item_id==2973)&(train.date_block_num==4)&(train.item_price>0)].item_price.median()
train.loc[train.item_price<0, 'item_price'] = median
from matplotlib import pyplot as plt
import math, os, re, time, random
import numpy as np, pandas as pd, seaborn as sns
import tensorflow as tf
from sklearn.model_selection import train_test_split<define_search_space>
train.loc[train.shop_id == 0, 'shop_id'] = 57
test.loc[test.shop_id == 0, 'shop_id'] = 57
train.loc[train.shop_id == 1, 'shop_id'] = 58
test.loc[test.shop_id == 1, 'shop_id'] = 58
train.loc[train.shop_id == 10, 'shop_id'] = 11
test.loc[test.shop_id == 10, 'shop_id'] = 11
rank_0_tensor = tf.constant(1)
 print(rank_0_tensor); print('')
 rank_1_tensor = tf.constant([1, 0, 0])
 print(rank_1_tensor); print('')
 rank_2_tensor = tf.constant([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
 print(rank_2_tensor )<categorify>
shops.loc[shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'
shops['city_code'] = LabelEncoder().fit_transform(shops['city'])
 shops = shops[['shop_id','city_code']]
cats['split'] = cats['item_category_name'].str.split('-')
 cats['type'] = cats['split'].map(lambda x: x[0].strip())
 cats['type_code'] = LabelEncoder().fit_transform(cats['type'])
 cats['subtype'] = cats['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])
 cats = cats[['item_category_id','type_code', 'subtype_code']]
items.drop(['item_name'], axis=1, inplace=True )
rank_0_tensor = tf.constant(1, dtype = tf.float16)
 print(rank_0_tensor); print('')
 rank_1_tensor = tf.constant([1, 0, 0], dtype = tf.float32)
 print(rank_1_tensor); print('')
 rank_2_tensor = tf.constant([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]], dtype = tf.int32)
 print(rank_2_tensor )<data_type_conversions>
len(list(set(test.item_id)- set(test.item_id ).intersection(set(train.item_id)))) , len(list(set(test.item_id))), len(test )
print(type(rank_2_tensor.numpy()))
print(rank_2_tensor.numpy()); print('')
 tensor_to_array = np.add(rank_2_tensor, 1)
 print(type(tensor_to_array))
print(tensor_to_array); print('')
 array_to_tensor = tf.add(rank_2_tensor.numpy() , 1)
 print(array_to_tensor )<choose_model_class>
ts = time.time()
matrix = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train[train.date_block_num==i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix['date_block_num'] = matrix['date_block_num'].astype(np.int8)
 matrix['shop_id'] = matrix['shop_id'].astype(np.int8)
 matrix['item_id'] = matrix['item_id'].astype(np.int16)
 matrix.sort_values(cols,inplace=True)
 time.time() - ts
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(256, activation='relu', input_shape =(784,)))
 model.add(tf.keras.layers.Dense(128, activation='swish'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["categorical_accuracy"])
 model.summary()<choose_model_class>
train['revenue'] = train['item_price'] * train['item_cnt_day']
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(256, activation = 'relu', input_shape =(784,)) ,
tf.keras.layers.Dense(128, activation = 'swish'),
tf.keras.layers.Dense(10, activation = 'softmax')
 ])
 model.compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["categorical_accuracy"])
 model.summary()<choose_model_class>
ts = time.time()
group = train.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=cols, how='left')
 matrix['item_cnt_month'] =(matrix['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))
time.time() - ts
inputs = tf.keras.Input(shape =(784,))
x = tf.keras.layers.Dense(256, activation = 'relu' )(inputs)
 x = tf.keras.layers.Dense(128, activation = 'swish' )(x)
 outputs = tf.keras.layers.Dense(10, activation = 'softmax' )(x)
 model = tf.keras.Model(inputs = inputs, outputs = outputs)
 model.compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["categorical_accuracy"])
 model.summary()<load_from_csv>
test['date_block_num'] = 34
test['date_block_num'] = test['date_block_num'].astype(np.int8)
 test['shop_id'] = test['shop_id'].astype(np.int8)
 test['item_id'] = test['item_id'].astype(np.int16 )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train.head()<drop_column>
ts = time.time()
matrix = pd.concat([matrix, test], ignore_index=True, sort=False, keys=cols)
 matrix.fillna(0, inplace=True)
 time.time() - ts
labels = train['label']
train = train.drop('label', axis = 1)
 train = train / 255.0
test = test / 255.0<categorify>
ts = time.time()
matrix = pd.merge(matrix, shops, on=['shop_id'], how='left')
 matrix = pd.merge(matrix, items, on=['item_id'], how='left')
 matrix = pd.merge(matrix, cats, on=['item_category_id'], how='left')
 matrix['city_code'] = matrix['city_code'].astype(np.int8)
 matrix['item_category_id'] = matrix['item_category_id'].astype(np.int8)
 matrix['type_code'] = matrix['type_code'].astype(np.int8)
 matrix['subtype_code'] = matrix['subtype_code'].astype(np.int8)
 time.time() - ts
labels = tf.one_hot(labels, depth = 10 ).numpy()<normalization>
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range = 20,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1
)<define_variables>
ts = time.time()
group = matrix.groupby(['date_block_num'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num'], how='left')
 matrix['date_avg_item_cnt'] = matrix['date_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_avg_item_cnt')
 matrix.drop(['date_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
EPOCHS = 45
BATCH_SIZE = 64
NUM_NETS = 25
VERBOSE = 0<choose_model_class>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_cnt'] = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3,6,12], 'date_item_avg_item_cnt')
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
model = [0] * NUM_NETS
for j in range(NUM_NETS):
model[j] = tf.keras.models.Sequential()
model[j].add(tf.keras.layers.Conv2D(32, kernel_size = 3, activation = 'relu', input_shape =(28, 28, 1)))
 model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Conv2D(32, kernel_size = 3, activation = 'relu'))
model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Conv2D(32, kernel_size = 5, strides = 2, padding = 'same', activation = 'relu'))
model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Dropout(0.4))
model[j].add(tf.keras.layers.Conv2D(64, kernel_size = 3, activation = 'relu'))
model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Conv2D(64, kernel_size = 3, activation = 'relu'))
model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Conv2D(64, kernel_size = 5, strides = 2, padding = 'same', activation = 'relu'))
model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Dropout(0.4))
model[j].add(tf.keras.layers.Conv2D(128, kernel_size = 4, activation = 'relu'))
model[j].add(tf.keras.layers.BatchNormalization())
 model[j].add(tf.keras.layers.Flatten())
 model[j].add(tf.keras.layers.Dropout(0.4))
model[j].add(tf.keras.layers.Dense(10, activation = 'softmax'))
model[j].compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["categorical_accuracy"] )<train_model>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_avg_item_cnt'] = matrix['date_shop_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3,6,12], 'date_shop_avg_item_cnt')
 matrix.drop(['date_shop_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
lr_callback = tf.keras.callbacks.LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * NUM_NETS
for j in range(NUM_NETS):
X_train, X_val, y_train, y_val = train_test_split(train, labels, test_size = 0.1)
 STEPS_PER_EPOCH = X_train.shape[0] // 64
history[j] = model[j].fit_generator(datagen.flow(X_train, y_train, batch_size = BATCH_SIZE),
epochs = EPOCHS, steps_per_epoch = STEPS_PER_EPOCH,
validation_data =(X_val, y_val), callbacks = [lr_callback],
verbose = VERBOSE)
 print(f"CNN {j + 1}: Epochs={EPOCHS}, Train accuracy={max(history[j].history['categorical_accuracy'])}, Validation accuracy={max(history[j].history['val_categorical_accuracy'])}" )<save_to_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_cat_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_category_id'], how='left')
 matrix['date_cat_avg_item_cnt'] = matrix['date_cat_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_cat_avg_item_cnt')
 matrix.drop(['date_cat_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
preds = np.zeros(( test.shape[0],10))
for j in range(NUM_NETS):
preds += model[j].predict(test)/ NUM_NETS
probs = pd.DataFrame(preds)
 probs.to_csv('ensemble_probs')
 probs.columns = probs.columns.astype(str)
 print(probs.columns)
 probs.head()<save_to_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_cat_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 matrix['date_shop_cat_avg_item_cnt'] = matrix['date_shop_cat_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_cat_avg_item_cnt')
 matrix.drop(['date_shop_cat_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submission['Label'] = preds.argmax(axis = 1)
 submission.to_csv("ensemble.csv", index = False)
 submission.head(10 )<load_from_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_type_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'type_code'], how='left')
 matrix['date_shop_type_avg_item_cnt'] = matrix['date_shop_type_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_type_avg_item_cnt')
 matrix.drop(['date_shop_type_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
prev_cnn_probs = pd.read_csv('.. /input/mnistsavedprobs/ensemble_probs')
 prev_cnn_probs = prev_cnn_probs.drop('Unnamed: 0', axis = 1)
 print(prev_cnn_probs.columns)
 prev_cnn_probs.head()<merge>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix['date_shop_subtype_avg_item_cnt'] = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_subtype_avg_item_cnt')
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
new_probs = probs.add(prev_cnn_probs ).divide(2)
 new_probs.head()<save_to_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'city_code'], how='left')
 matrix['date_city_avg_item_cnt'] = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_city_avg_item_cnt')
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
submission2 = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submission2['Label'] = new_probs.values.argmax(axis = 1)
 submission2.to_csv("ensemble2.csv", index = False)
 submission2.head(10 )<set_options>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'city_code'], how='left')
 matrix['date_item_city_avg_item_cnt'] = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_item_city_avg_item_cnt')
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
%matplotlib inline 
<import_modules>
ts = time.time()
group = matrix.groupby(['date_block_num', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_type_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'type_code'], how='left')
 matrix['date_type_avg_item_cnt'] = matrix['date_type_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_type_avg_item_cnt')
 matrix.drop(['date_type_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
print(tf.version.VERSION )<load_from_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_subtype_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'subtype_code'], how='left')
 matrix['date_subtype_avg_item_cnt'] = matrix['date_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_subtype_avg_item_cnt')
 matrix.drop(['date_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<prepare_x_and_y>
ts = time.time()
group = train.groupby(['item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['item_id'], how='left')
 matrix['item_avg_item_price'] = matrix['item_avg_item_price'].astype(np.float16)
 group = train.groupby(['date_block_num','item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['date_item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_price'] = matrix['date_item_avg_item_price'].astype(np.float16)
 lags = [1,2,3,4,5,6]
matrix = lag_feature(matrix, lags, 'date_item_avg_item_price')
 for i in lags:
matrix['delta_price_lag_'+str(i)] = \ 
(matrix['date_item_avg_item_price_lag_'+str(i)] - matrix['item_avg_item_price'])/ matrix['item_avg_item_price']
def select_trend(row):
for i in lags:
if row['delta_price_lag_'+str(i)]:
return row['delta_price_lag_'+str(i)]
return 0
matrix['delta_price_lag'] = matrix.apply(select_trend, axis=1)
 matrix['delta_price_lag'] = matrix['delta_price_lag'].astype(np.float16)
 matrix['delta_price_lag'].fillna(0, inplace=True)
 fetures_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
fetures_to_drop += ['date_item_avg_item_price_lag_'+str(i)]
fetures_to_drop += ['delta_price_lag_'+str(i)]
matrix.drop(fetures_to_drop, axis=1, inplace=True)
 time.time() - ts
train_X = train.loc[:, "pixel0":"pixel783"]
train_y = train.loc[:, "label"] 
<filter>
ts = time.time()
group = train.groupby(['date_block_num','shop_id'] ).agg({'revenue': ['sum']})
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(['shop_id'] ).agg({'date_shop_revenue': ['mean']})
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['shop_id'], how='left')
 matrix['shop_avg_revenue'] = matrix['shop_avg_revenue'].astype(np.float32)
 matrix['delta_revenue'] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix['delta_revenue'] = matrix['delta_revenue'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'delta_revenue')
 matrix.drop(['date_shop_revenue','shop_avg_revenue','delta_revenue'], axis=1, inplace=True)
 time.time() - ts
treshhold=0.1
train_X[train_X<treshhold]=0
test[test<treshhold]=0<categorify>
matrix['month'] = matrix['date_block_num'] % 12
train_X = train_X / 255.0
test_X = test / 255.0
train_X = train_X.values.reshape(-1,28,28,1)
 test_X = test_X.values.reshape(-1,28,28,1)
 train_y = to_categorical(train_y, num_classes = 10 )<choose_model_class>
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8 )
def build_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='swish', input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='swish'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='swish'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='swish'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='swish'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='swish'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='swish'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model<init_hyperparams>
ts = time.time()
cache = {}
matrix['item_shop_last_sale'] = -1
matrix['item_shop_last_sale'] = matrix['item_shop_last_sale'].astype(np.int8)
 for idx, row in matrix.iterrows() :
key = str(row.item_id)+' '+str(row.shop_id)
 if key not in cache:
if row.item_cnt_month!=0:
cache[key] = row.date_block_num
else:
last_date_block_num = cache[key]
matrix.at[idx, 'item_shop_last_sale'] = row.date_block_num - last_date_block_num
cache[key] = row.date_block_num
time.time() - ts
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )<choose_model_class>
ts = time.time()
cache = {}
matrix['item_last_sale'] = -1
matrix['item_last_sale'] = matrix['item_last_sale'].astype(np.int8)
 for idx, row in matrix.iterrows() :
key = row.item_id
if key not in cache:
if row.item_cnt_month!=0:
cache[key] = row.date_block_num
else:
last_date_block_num = cache[key]
if row.date_block_num>last_date_block_num:
matrix.at[idx, 'item_last_sale'] = row.date_block_num - last_date_block_num
cache[key] = row.date_block_num
time.time() - ts
learning_rate_reduction = ReduceLROnPlateau(monitor='accuracy',
patience=3,
verbose=1,
factor=0.8,
min_lr=0.001)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 early_stop=EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto',
baseline=None, restore_best_weights=True )<split>
ts = time.time()
matrix['item_shop_first_sale'] = matrix['date_block_num'] - matrix.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 matrix['item_first_sale'] = matrix['date_block_num'] - matrix.groupby('item_id')['date_block_num'].transform('min')
 time.time() - ts
%%time
nets=10
model = [0] *nets
history = [0] * nets
skf = StratifiedKFold(n_splits=nets, shuffle = True, random_state=1)
 skf.get_n_splits(train_X, train['label'])
 print(skf)
 number=0
for train_index, test_index in skf.split(train_X, train['label']):
print("SPLIT ",number," TRAIN index:", train_index, "TEST index:", test_index)
 X_train, X_val = train_X[train_index], train_X[test_index]
y_train, y_val = train_y[train_index], train_y[test_index]
model[number]=build_model()
model[number].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 history[number] =model[number].fit(datagen.flow(X_train,y_train), epochs=100 ,validation_data =(X_val,y_val),
batch_size=100, verbose = 0,callbacks = [annealer,early_stop])
 metrics=pd.DataFrame(history[number].history)
 display(metrics)
 number+=1<save_model>
ts = time.time()
matrix = matrix[matrix.date_block_num > 11]
time.time() - ts
for number in range(0,nets):
model[number].save("StratifiedKFold_10_batch100_double_val_loss_"+str(number)+".h5" )<save_to_csv>
ts = time.time()
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
matrix = fill_na(matrix)
 time.time() - ts
results = np.zeros(( test_X.shape[0],10))
for j in range(nets):
results = results + model[j].predict(test_X)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("StratifiedKFold_10_batch100_double_val_loss.csv",index=False )<find_best_params>
matrix.to_pickle('data.pkl')
 del matrix
del cache
del group
del items
del shops
del cats
del train
gc.collect() ;
show_test_digits(range(500,530))<load_from_csv>
data = pd.read_pickle('data.pkl' )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )<data_type_conversions>
data = data[[
'date_block_num',
'shop_id',
'item_id',
'item_cnt_month',
'city_code',
'item_category_id',
'type_code',
'subtype_code',
'item_cnt_month_lag_1',
'item_cnt_month_lag_2',
'item_cnt_month_lag_3',
'item_cnt_month_lag_6',
'item_cnt_month_lag_12',
'date_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_2',
'date_item_avg_item_cnt_lag_3',
'date_item_avg_item_cnt_lag_6',
'date_item_avg_item_cnt_lag_12',
'date_shop_avg_item_cnt_lag_1',
'date_shop_avg_item_cnt_lag_2',
'date_shop_avg_item_cnt_lag_3',
'date_shop_avg_item_cnt_lag_6',
'date_shop_avg_item_cnt_lag_12',
'date_cat_avg_item_cnt_lag_1',
'date_shop_cat_avg_item_cnt_lag_1',
'date_city_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1',
'delta_price_lag',
'month',
'days',
'item_shop_last_sale',
'item_last_sale',
'item_shop_first_sale',
'item_first_sale',
]]
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train /= 255
X_test /= 255<categorify>
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )
y_train = train.pop("label")
 y_train = y_train.values
print(y_train.shape)
 y_train = to_categorical(y_train, num_classes=10)
 print(y_train.shape)
 print(y_train[0] )<import_modules>
del data
gc.collect() ;
import tensorflow as tf<define_search_model>
ts = time.time()
model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 time.time() - ts
<choose_model_class><EOS> 
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))
pickle.dump(Y_test, open('xgb_test.pickle', 'wb'))