Kaggle competition: Digit Recognizer
train.isnull().sum()<drop_column>
train = pd.read_csv(".. /input/train.csv")
 print(train.shape)
 train.head()
train = train.drop(["Cabin"],axis = 1)
 print(train.shape)
 train.head()<count_missing_values>
test= pd.read_csv(".. /input/test.csv")
 print(test.shape)
 test.head()
train.isnull().sum()<count_missing_values>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1 )
train.isnull().sum()<categorify>
X_train = X_train / 255.0
test = test / 255.0
print("x_train shape: ",X_train.shape)
 print("test shape: ",test.shape )
train['Embarked'].fillna(method = 'backfill', inplace = True )<data_type_conversions>
Y_train = to_categorical(Y_train, num_classes = 10 )
train['Age'].fillna(train['Age'].median() , inplace = True )<count_missing_values>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)
 print("x_train shape",X_train.shape)
 print("x_test shape",X_val.shape)
 print("y_train shape",Y_train.shape)
 print("y_test shape",Y_val.shape )
train.isnull().sum()<count_missing_values>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
train.isnull().sum()<data_type_conversions>
optimizer = RMSprop(lr=0.001,rho=0.9, epsilon=1e-08, decay=0.0 )
def str_to_cat(training_df):
for p,q in training_df.items() :
if is_string_dtype(q):
training_df[p] = q.astype('category' ).cat.as_ordered()
return training_df<categorify>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
def mydf_to_nums(training_df, feature, null_status):
if not is_numeric_dtype(feature):
training_df[null_status] = feature.cat.codes + 1
def mydf_imputer(training_df, feature, null_status, null_table):
if is_numeric_dtype(feature):
if pd.isnull(feature ).sum() or(null_status in null_table):
filler = null_table[null_status] if null_status in null_table else feature.median()
training_df[null_status] = feature.fillna(filler)
 null_table[null_status] = filler
return null_table
def mydf_preprocessor(training_df, null_table):

 if null_table is None:
null_table = dict()
for p,q in training_df.items() :
null_table = mydf_imputer(training_df, q, p, null_table)
 for p,q in training_df.items() :
mydf_to_nums(training_df, q, p)
 training_df = pd.get_dummies(training_df, dummy_na = True)
 res = [training_df, null_table]
return res<categorify>
epochs = 20
batch_size = 378
train = str_to_cat(train)
 train_df,my_table = mydf_preprocessor(train,null_table = None)
 print(train_df.shape)
 train_df.head()<count_duplicates>
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)
 datagen.fit(X_train )
train.duplicated().sum()<count_missing_values>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val), steps_per_epoch=X_train.shape[0] // batch_size )
test.isnull().sum()<count_missing_values>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<drop_column><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )