Kaggle competition: Digit Recognizer
train_df['Family']=train_df['Parch'] + 1 + train_df['SibSp']
train_df['Parch']=train_df['Parch'].apply(lambda x: is_var(x))
train_df['SibSp']=train_df['SibSp'].apply(lambda x: is_var(x))<define_variables>
train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 print(train_data.head() )
sel_cols=['Fare',
'Pclass', 'SibSp',
'Parch', 'C', 'Q',
'S', 'female', 'male', '18-24', '25-34', '35-44', '45+', '<13','13-18', 'Family'
]<correct_missing_values>
X_train = train_data.drop('label',axis=1)
 X_train = X_train.values
X_train = X_train.astype('float32')
 y_train = train_data['label']
y_train = y_train.values
y_train = y_train.astype('float32')
 X_test = test_data.astype('float32')
 print("The MNIST dataset has a training set of %d examples." % len(X_train))
print("The MNIST database has a test set of %d examples." % len(X_test))
train_df.fillna(0,inplace=True )<prepare_x_and_y>
X_train = X_train.astype('float32')/255
X_test = X_test.astype('float32')/255
X=train_df[sel_cols]<prepare_x_and_y>
datagen_train = ImageDataGenerator(
 width_shift_range=0.2,
height_shift_range=0.2,
rotation_range = 20,
horizontal_flip=True,
vertical_flip = True,
shear_range = 0.1)
 datagen_train.fit(X_train )
y=train_df['Survived']<split>
random_seed = 46
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.25, random_state=random_seed)
 print('Integer-valued labels:')
 print(y_train[:10])
 y_train = np_utils.to_categorical(y_train, 10)
 y_val = np_utils.to_categorical(y_val, 10)
 print('One-hot labels:')
 print(y_train[:10] )
train_X,val_X,train_y,val_y=train_test_split(X,y,test_size=0.3,random_state=1 )<train_model>
model = Sequential()
model.add(Conv2D(32,(5, 5), padding='same',
input_shape=X_train.shape[1:]))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,(5, 5), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(64,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
optimizer = optimizers.Adam(lr=0.001)
 model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
 model.summary()
print("input shape ",model.input_shape)
 print("output shape ",model.output_shape )
lr=LogisticRegression(max_iter=400)
 lr.fit(train_X,train_y )<compute_test_metric>
batch_size = 128
nb_epoch = 20
checkpointer = [
ReduceLROnPlateau(monitor='val_loss',
patience=3,
verbose=1,
factor=0.5,
min_lr = 0.00001,
cooldown=0),
ModelCheckpoint('mnist_model_best.hdf5',
monitor='val_accuracy',
save_best_only=True,
mode='max',
verbose=1)
 ]
hist = model.fit(X_train, y_train, epochs=nb_epoch, batch_size=batch_size, validation_data=(X_val,y_val), callbacks=[checkpointer],verbose=1, shuffle=True )
lr.score(train_X,train_y )<compute_test_metric>
model = load_model('mnist_model_best.hdf5')
 score = model.evaluate(X_val, y_val, verbose=0)
 accuracy = 100*score[1]
print('Validation accuracy: %.4f%%' % accuracy )
lr.score(val_X,val_y )<load_from_csv>
predictions = model.predict_classes(X_test, verbose=1)
 submissions = pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("mnist_MLP_test.csv",index=False )