Kaggle competition: Digit Recognizer
if allow_tuning:
params_knn = {
'n_neighbors' : range(1, 10),
'weights' : ['uniform', 'distance'],
'algorithm' : ['auto', 'ball_tree','kd_tree'],
'p' : [1,2]
}
model_knn = knn()
search_knn = GridSearchCV(model_knn, params_knn, cv=5, scoring='accuracy', n_jobs=-1, verbose=1 ).fit(X_train, y_train)
 print(search_knn.best_params_ )<train_on_grid>
train = pd.read_csv(".. /input/train.csv")
 test= pd.read_csv(".. /input/test.csv")
 print("Train size:{}
Test size:{}".format(train.shape, test.shape))
x_train = train.drop(['label'], axis=1 ).values.astype('float32')
 y_train = train['label'].values.astype('int32')
 x_test = test.values.astype('float32')
 x_train = x_train.reshape(x_train.shape[0], 28, 28)/ 255.0
x_test = x_test.reshape(x_test.shape[0], 28, 28)/ 255.0
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.05, random_state=42)
 print(x_train.shape)
 print(x_val.shape)
 print(y_train.shape)
 print(x_test.shape )
if allow_tuning:
params_logistic = {
'max_iter': [2000],
'penalty': ['l1', 'l2'],
'C': np.logspace(-4, 4, 20),
'solver': ['liblinear']
}
model_logistic = LogisticRegression()
search_logistic = GridSearchCV(model_logistic, params_logistic, cv=5, scoring='accuracy', n_jobs=-1, verbose=1 ).fit(X_train, y_train)
 print(search_logistic.best_params_ )<train_on_grid>
x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
 x_val = x_val.reshape(x_val.shape[0], 28, 28,1)
 x_test = x_test.reshape(x_test.shape[0], 28, 28,1)
 print("Train size:{}
validation size:{}
Test size:{}".format(x_train.shape,x_val.shape, x_test.shape))
mean_px = x_train.mean().astype(np.float32)
 std_px = x_train.std().astype(np.float32)
if allow_tuning:
params_svc = [{'kernel': ['rbf'], 'gamma': [.01,.1,.5, 1, 2, 5, 10], 'C': [.1, 1, 10, 100, 1000], 'probability': [True]},
{'kernel': ['poly'], 'degree' : [2, 3, 4, 5], 'C': [.01,.1, 1, 10, 100, 1000], 'probability': [True]}]
model_svc = SVC()
search_svc = GridSearchCV(model_svc, params_svc, cv=5, scoring='accuracy', n_jobs=-1, verbose=1 ).fit(X_train, y_train)
 print(search_svc.best_params_ )<train_on_grid>
input = Input(shape=[28, 28, 1])
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv1' )(input)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch1' )(x)
 x = Activation('relu', name='relu1' )(x)
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv2' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2' )(x)
 x = Activation('relu', name='relu2' )(x)
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv2add' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2add' )(x)
 x = Activation('relu', name='relu2add' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPool2D(pool_size=2, strides=2, padding='same' )(x)
 x = Conv2D(64,(3, 3), strides=1, padding='same', name='conv3' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch3' )(x)
 x = Activation('relu', name='relu3' )(x)
 x = Conv2D(64,(3, 3), strides=1, padding='same', name='conv4' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch4' )(x)
 x = Activation('relu', name='relu4' )(x)
 x = Conv2D(32,(3, 3), strides=1, padding='same', name='conv5' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch5' )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPool2D(pool_size=2, strides=2 )(x)
 x = Flatten()(x)
 x = Dense(100, name='Dense30' )(x)
 x = Activation('relu', name='relu6' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(10, name='Dense10' )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs = input, outputs =x)
 print(model.summary() )
if allow_tuning:
params_svc = {'kernel': ['rbf'], 'gamma': [i/10000 for i in range(90, 110)], 'C': range(50, 80, 10), 'probability': [True]}
model_svc = SVC()
search_svc = GridSearchCV(model_svc, params_svc, cv=5, scoring='accuracy', n_jobs=-1, verbose=1 ).fit(X_train, y_train)
 print(search_svc.best_params_ )<choose_model_class>
checkpoint = ModelCheckpoint("best_weights.hdf5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 datagen = ImageDataGenerator(
 rotation_range= 8,
zoom_range = 0.13,
width_shift_range=0.13,
height_shift_range=0.13)
 epochs = 60
lr_initial = 0.0011
optimizer = Adam(lr=lr_initial, decay= lr_initial /(epochs*1.3))
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 datagen.fit(x_train)
 batch_size = 64
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_val,y_val),
verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size, callbacks=[checkpoint])
 model.load_weights("best_weights.hdf5")
<train_on_grid><EOS> 
results = model.predict(x_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("MNIST-CNN-ENSEMBLE.csv",index=False )