Kaggle competition: Digit Recognizer
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )<data_type_conversions>
def clean_inputs(train, test, img_shape =(-1,28,28,1), num_classes = 10):
t_X = train.drop("label", axis=1)
 t_Y = train["label"]
t_X = t_X / 255
test_x = test.values / 255
t_X = np.reshape(t_X.values, img_shape)
 test_x = np.reshape(test_x, img_shape)
 t_Y = keras.utils.to_categorical(t_Y, num_classes = num_classes)
 train_x, dev_x, train_y, dev_y = train_test_split(t_X, t_Y, test_size = 0.15, random_state = 0)
 return train_x, train_y, dev_x, dev_y, test_x
all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0] )<drop_column>
train_x, train_y, dev_x, dev_y, test_x = clean_inputs(train, test )
all_data = all_data.drop(['Utilities'], axis=1 )<data_type_conversions>
def model(inp_shape):
X = Input(inp_shape, name='input')
 A = Conv2D(6,(7, 7), strides=(1, 1), padding='Same', activation='relu', name='C1' )(X)
 A = MaxPooling2D(pool_size=2, padding='valid' )(A)
 A = Conv2D(16,(5, 5), strides=(1, 1), padding='Same', activation='relu', name='C2' )(A)
 A = MaxPooling2D(pool_size=2, padding='valid' )(A)
 A = Flatten()(A)
 A = BatchNormalization()(A)
 A = Dense(120, activation='relu', kernel_regularizer=regularizers.l2(0.01), name='FC1' )(A)
 A = Dense(84, activation='relu', kernel_regularizer=regularizers.l2(0.01), name='FC2' )(A)
 A = Dense(10, activation='softmax', name='Final' )(A)
 model = Model(inputs=X, outputs=A, name='LeNet')
 return model
all_data["Functional"] = all_data["Functional"].fillna("Typ" )<data_type_conversions>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(train_x )
all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0] )<data_type_conversions>
train_x_pad = np.pad(train_x,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant', constant_values=0 ).astype(float)
 dev_x_pad = np.pad(dev_x,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant', constant_values=0 ).astype(float)
 test_x_pad = np.pad(test_x,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant', constant_values=0 ).astype(float)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1,factor=0.5, min_lr=0.00001)
 model = model(train_x_pad.shape[1:])
 model.summary()
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])
 history = model.fit_generator(datagen.flow(train_x_pad, train_y, batch_size=32), validation_data=(dev_x_pad, dev_y), steps_per_epoch=len(train_x_pad)//32, epochs=25, callbacks=[learning_rate_reduction] )
all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0] )<categorify>
def model2(num_classes = 10):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = "softmax"))
return model;
all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0] )<data_type_conversions>
start = time.time()
model2 = model2(10)
 learning_rate_reduction2 = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1,factor=0.5, min_lr=0.00001)
 model2.summary()
model2.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])
 history2 = model2.fit_generator(datagen.flow(train_x, train_y, batch_size=32), validation_data=(dev_x, dev_y), steps_per_epoch=len(train_x)//32, epochs=25, callbacks=[learning_rate_reduction2])
 timeRecord = time.time() - start
print("--- %s seconds ---" %(timeRecord))
all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0] )<data_type_conversions>
def model3(num_classes = 10):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'same', activation ='relu', input_shape =(28,28,1)))
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dense(num_classes, activation = "softmax"))
return model;
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None" )<sort_values>
start = time.time()
model3 = model3(10)
 learning_rate_reduction3 = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1,factor=0.5, min_lr=0.00001)
 model3.summary()
model3.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics=['accuracy'])
 history3 = model3.fit_generator(datagen.flow(train_x, train_y, batch_size=64),
validation_data=(dev_x, dev_y), steps_per_epoch=len(train_x)//64, epochs=25,
callbacks=[learning_rate_reduction3])
 timeRecord = time.time() - start
print("--- %s seconds ---" %(timeRecord))
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head()<data_type_conversions>
prediction = model2.predict(test_x)
 prediction = np.argmax(prediction, axis=1)
 prediction = pd.Series(prediction, name="Label")
 submission = pd.concat([pd.Series(range(1,28001), name = "ImageId"), prediction],axis = 1)
 submission.to_csv('mnist-submission.csv', index = False)
 print(submission )