Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 print('Shape of the testing dataset:', test_df.shape)
 print('Shape of the training dataset:', train_df.shape )
model = rnn_model(lr=lr_init, decay=lr_decay)
 model.fit(train_X, train_y, epochs=epochs, batch_size=BATCH_SIZE, verbose=2 )<feature_engineering>
train_labels = train_df['label']
train_images = train_df.drop('label', axis=1)
 train_images = train_images.astype('float32')/255
test_images = test_df.astype('float32')/255
train_images = np.array(train_images ).reshape(42000,28,28,1)
 test_images = np.array(test_images ).reshape(28000,28,28,1)
 train_images, validation_images, train_labels, validation_labels = train_test_split(train_images, train_labels, test_size=0.1, random_state=0)
 train_labels = to_categorical(train_labels)
 validation_labels = to_categorical(validation_labels )
dev_df['general_cat'], dev_df['subcat_1'], dev_df['subcat_2'] = \
zip(*dev_df['category_name'].apply(lambda x: split_cat(x)))
 handle_missing_inplace(dev_df)
 cutting(dev_df)
 text_length_feature(dev_df)
 normalize_dataset_text(dev_df)
 delete_unseen(dev_df)
 to_categorical(dev_df )<categorify>
datagen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.1,
zoom_range=0.05,
horizontal_flip=False,
fill_mode='nearest')
 datagen.fit(train_images )
dev_df['encoded_brand_name'] = le_brand.transform(dev_df['brand_name'])
 dev_df['encoded_general_cat'] = le_general_cat.transform(dev_df['general_cat'])
 dev_df['encoded_subcat_1'] = le_subcat_1.transform(dev_df['subcat_1'])
 dev_df['encoded_subcat_2'] = le_subcat_2.transform(dev_df['subcat_2'])
 dev_df['seq_item_description'] = tok_desc.texts_to_sequences(dev_df["item_description"].values)
 dev_df['seq_name'] = tok_name.texts_to_sequences(dev_df["name"].values)
 dev_X = get_rnn_data(dev_df)
 preds_rnn = model.predict(dev_X)
 print("RNN dev RMSLE:", rmsle(np.expm1(dev_y), np.expm1(preds_rnn.flatten())) )<feature_engineering>
def createModel() :
model = models.Sequential()
model.add(layers.Conv2D(32,(3, 3), activation='relu', padding='same', input_shape=(28, 28, 1)))
 model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(64,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(128,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(256,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Flatten())
 model.add(layers.Dense(256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.2))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer=optimizers.Adam(lr=1e-3),
loss='categorical_crossentropy',
metrics=['acc'])
 return model
test_df['general_cat'], test_df['subcat_1'], test_df['subcat_2'] = \
zip(*test_df['category_name'].apply(lambda x: split_cat(x)))
 test_df.drop('category_name', axis=1, inplace=True)
 handle_missing_inplace(test_df)
 cutting(test_df)
 text_length_feature(test_df)
 normalize_dataset_text(test_df)
 delete_unseen(test_df)
 to_categorical(test_df )<predict_on_test>
lr_reduction = ReduceLROnPlateau(monitor = 'val_acc', factor = 0.5, min_lr = 1e-6)
 checkpoint = ModelCheckpoint('./trainedModel.hdf5',monitor = 'val_acc', mode = "max", save_best_model = True )
test_df['encoded_brand_name'] = le_brand.transform(test_df['brand_name'])
 test_df['encoded_general_cat'] = le_general_cat.transform(test_df['general_cat'])
 test_df['encoded_subcat_1'] = le_subcat_1.transform(test_df['subcat_1'])
 test_df['encoded_subcat_2'] = le_subcat_2.transform(test_df['subcat_2'])
 test_df['seq_item_description'] = tok_desc.texts_to_sequences(test_df["item_description"].values)
 test_df['seq_name'] = tok_name.texts_to_sequences(test_df["name"].values)
 test_X = get_rnn_data(test_df)
 preds_rnn_test = model.predict(test_X )<concatenate>
num_epochs = 50
num_models = 10
batch_size = 100
list_histories = []
list_models = []
for i in range(num_models):
model = createModel()
history = model.fit(datagen.flow(train_images, train_labels, batch_size=batch_size),
epochs=num_epochs,
validation_data=(validation_images, validation_labels),
callbacks = [lr_reduction, checkpoint])
 list_histories.append(history)
 list_models.append(model )
preds_rnn_test = np.expm1(preds_rnn_test.flatten() )<prepare_output>
pred = np.zeros(( len(test_images),10))
for i in range(num_models):
pred += list_models[i].predict(test_images )
submission = test_df[["test_id"]]
submission["price"] = preds_rnn_test<save_to_csv>
pred = np.argmax(pred, axis=-1)
 sub = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 sub['Label'] = pred
sub.head()
submission.to_csv("submission.csv", index=False )<save_to_csv>
sub.to_csv('submission.csv', index=False )
submission.to_csv("submission.csv", index=False )<set_options>
sub.to_csv('submission.csv', index=False )