Kaggle competition: Digit Recognizer
item_mean_features = []
all_data, item_mean_features = add_mean_features(df=all_data,
mean_features=item_mean_features,
idx_features=['date_block_num', 'item_id'])
 all_data, item_mean_features = add_mean_features(df=all_data,
mean_features=item_mean_features,
idx_features=['date_block_num', 'item_id', 'city'] )<drop_column>
print(tf.__version__)
shop_mean_features = []
all_data, shop_mean_features = add_mean_features(df=all_data,
mean_features=shop_mean_features,
idx_features=['date_block_num', 'shop_id', 'item_category_id'] )<feature_engineering>
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv')
 print("train data shape: {}".format(train_data.shape))
print("test data shape: {}".format(test_data.shape))
X, Y = train_data.drop(['label'], axis=1), train_data['label']
del train_data
def add_lag_features(df, lag_features_to_clip, idx_features,
lag_feature, nlags=3, clip=False):
df_temp = df[idx_features + [lag_feature]].copy()
for i in range(1, nlags+1):
lag_feature_name = lag_feature +'_lag' + str(i)
 df_temp.columns = idx_features + [lag_feature_name]
df_temp['date_block_num'] += i
df = df.merge(df_temp.drop_duplicates() ,
on=idx_features,
how='left')
 df[lag_feature_name] = df[lag_feature_name].fillna(0)
 if clip:
lag_features_to_clip.append(lag_feature_name)
 df = downcast(df, False)
 del df_temp
gc.collect()
return df, lag_features_to_clip<define_variables>
X = X/255.0
test_features = test_data/255.0
Y = keras.utils.to_categorical(Y, num_classes=10)
 X = X.values.reshape(-1,28,28,1)
 test_features = test_features.values.reshape(-1,28,28,1 )
lag_features_to_clip = []
idx_features = ['date_block_num', 'shop_id', 'item_id']
all_data, lag_features_to_clip = add_lag_features(df=all_data,
lag_features_to_clip=lag_features_to_clip,
idx_features=idx_features,
lag_feature='item_cnt_month',
nlags=3,
clip=True )<drop_column>
X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size = 0.1, random_state=2 )
all_data, lag_features_to_clip = add_lag_features(df=all_data,
lag_features_to_clip=lag_features_to_clip,
idx_features=idx_features,
lag_feature='item_count',
nlags=3)
 all_data, lag_features_to_clip = add_lag_features(df=all_data,
lag_features_to_clip=lag_features_to_clip,
idx_features=idx_features,
lag_feature='item_price_mean',
nlags=3 )<prepare_x_and_y>
datagen = keras.preprocessing.image.ImageDataGenerator(rotation_range=15,
width_shift_range=0.1,
zoom_range=0.1,
height_shift_range=0.1)
 datagen.fit(X_train )
X_test_temp = all_data[all_data['date_block_num'] == 34]
X_test_temp[item_mean_features].sum()<categorify>
input_shape =(28, 28, 1)
 model = keras.Sequential()
model.add(keras.layers.Conv2D(filters=32, kernel_size=(5, 5), activation="relu", input_shape=input_shape, padding="Same"))
model.add(keras.layers.Conv2D(filters=32, kernel_size=(5, 5), activation="relu", padding="Same"))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.MaxPooling2D(( 2, 2)))
 model.add(keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation="relu", padding="Same"))
model.add(keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation="relu", padding="Same"))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.MaxPooling2D(( 2, 2)))
 model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(10))
model.summary()
for item_mean_feature in item_mean_features:
all_data, lag_features_to_clip = add_lag_features(df=all_data,
lag_features_to_clip=lag_features_to_clip,
idx_features=idx_features,
lag_feature=item_mean_feature,
nlags=3)
 all_data = all_data.drop(item_mean_features, axis=1 )<feature_engineering>
adam_opt = keras.optimizers.Adam(learning_rate=0.0008, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False)
 model.compile(optimizer=adam_opt,
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics = ['accuracy'])
 history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=96),
steps_per_epoch=len(X_train)/96,
epochs=30,
validation_data=(X_val, y_val),
validation_freq=1,
shuffle=True )
<drop_column><EOS> 
submission = pd.DataFrame()
submission['ImageId'] = pd.Series(range(1,28001))
submission['Label'] = results
submission.to_csv('cnn_submission.csv',index=False )