Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<filter>
%matplotlib inline
data[data['Fare'].isna() ]<sort_values>
train, test = pd.read_csv(".. /input/train.csv"), pd.read_csv(".. /input/test.csv" )
data_corr = data.corr().abs().unstack().sort_values(kind = "quicksort", ascending = False ).reset_index()
data_corr.loc[data_corr['level_0'] == 'Fare']<categorify>
train['label'].value_counts()
data['Fare'] = data['Fare'].fillna(data.groupby(['Pclass'])['Fare'].transform('median'))<count_missing_values>
train_pixels, test_pixels = train.iloc[:,1:].values.astype('float32'), test.values.astype('float32')
 train_labels = train.iloc[:,0].values.astype('int32')
 train_labels = train_labels.reshape(-1, 1)
 print(f'train_pixels shape = {train_pixels.shape}')
 print(f'test_pixels shape = {test_pixels.shape}')
 print(f'train_labels shape = {train_labels.shape}' )
data.isna().sum()<feature_engineering>
train_pixels, test_pixels = train_pixels.reshape(-1, 28, 28, 1), test_pixels.reshape(-1, 28, 28, 1)
 print(f'train_pixels shape = {train_pixels.shape}')
 print(f'test_pixels shape = {test_pixels.shape}' )
data['Title'] = data.Name.apply(lambda x: re.search('([A-Z][a-z]+)\.', x ).group(1))
data['Title'].unique()<count_values>
train_pixels, test_pixels = train_pixels / 255.0, test_pixels / 255.0
data['Title'].value_counts(normalize = True ).round(3 )<count_missing_values>
train_labels = to_categorical(train_labels, num_classes = 10)
 print(f'train_labels shape = {train_labels.shape}')
 train_labels
data['Age'].isna().sum()<filter>
train_pixels, val_pixels, train_labels, val_labels = train_test_split(train_pixels, train_labels, test_size = 0.1, random_state=None)
 train_pixels.shape, train_labels.shape, val_pixels.shape, val_labels.shape, test_pixels.shape
data[data['Age'] < 1]<feature_engineering>
m_train = train_pixels.shape[0]
m_val = val_pixels.shape[0]
m_test = test_pixels.shape[0]
n_x = test.shape[1]
n_y = train_labels.shape[1]
print(f" m_train = {m_train} / m_val = {m_val} / m_test = {m_test} / n_x = {n_x} / n_y = {n_y}" )
data.loc[data['Age'] < 1, 'Age'] = None
data['Age'].isna().sum()<sort_values>
datagen = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.1,
shear_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1 )
data_corr = data.corr().abs().unstack().sort_values(kind = "quicksort", ascending = False ).reset_index()
data_corr.loc[data_corr['level_0'] == 'Age']<data_type_conversions>
model = Sequential()
model.add(Conv2D(filters=6, kernel_size=5, padding='Same',
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=2, strides=2))
model.add(Conv2D(filters=16, kernel_size=5))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=2, strides=2))
model.add(Flatten())
 model.add(Dense(120))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(84))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Dense(10))
model.add(BatchNormalization())
 model.add(Activation('softmax'))
model.compile(optimizer=Adam(lr=1e-3),
loss='categorical_crossentropy',
metrics=['accuracy'])
 lr_decay = ReduceLROnPlateau(monitor='loss',
patience=1, verbose=1,
factor=0.5, min_lr=1e-7)
 History = model.fit(train_pixels, train_labels, epochs=40,
validation_data=(val_pixels, val_labels),
callbacks=[lr_decay], verbose=1)

train_loss, train_acc = model.evaluate(train_pixels, train_labels)
 val_loss, val_acc = model.evaluate(val_pixels, val_labels)
 print(f'model: train accuracy = {round(train_acc * 100, 4)}%')
 print(f'model: val accuracy = {round(val_acc * 100, 4)}%')
 print(f'model: val error = {round(( 1 - val_acc)* m_val)} examples')
<sort_values><EOS> 
predictions = model.predict_classes(test_pixels)
 submission = pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label" : predictions})
 submission.to_csv("submission.csv", index=False, header=True)
 submission.head()