Kaggle competition: Kannada MNIST
x_train_second_layer1 = pd.DataFrame(x_train_second_layer)
 x_test_second_layer1 = pd.DataFrame(x_test_second_layer )<set_options>
sample_submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 x = train.iloc[:,1:].values
y = train.iloc[:,0].values
x_test = test.drop('id', axis=1 ).iloc[:,:].values
x = x.reshape(x.shape[0], 28, 28, 1)
 y = to_categorical(y, 10)
 x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
 x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.10, random_state=42)
 train_datagen = ImageDataGenerator(rescale=1./255.,
rotation_range=10,
width_shift_range=0.10,
height_shift_range=0.10,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(rescale=1./255.)
warnings.filterwarnings('ignore')
<init_hyperparams>
optimizer = RMSprop(lr=0.001)
 model= Sequential()
model.add(Conv2D(24,kernel_size = 3, padding = "same", activation = "relu",input_shape =(28,28,1)))
 model.add(Conv2D(24,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(48 ,kernel_size = 5, padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.1))
model.add(Conv2D(48,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(48,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(96 ,kernel_size = 5, padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.1))
model.add(Conv2D(96,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(96,kernel_size = 3, padding = "same", activation = "relu"))
model.add(Conv2D(192 ,kernel_size = 5, padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256))
model.add(Dropout(0.2))
model.add(Dense(10, activation="softmax"))
model.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics = ["accuracy"] )
SEED = 0
param = {
'min_child_weight':6.790,
"subsample_for_bin":50000,
'bagging_seed': 0,
'boost_from_average':'true',
'boost': 'gbdt',
'feature_fraction': 0.450,
'bagging_fraction': 0.343,
'learning_rate': 0.025,
'max_depth': 10,
'metric':'auc',
'min_data_in_leaf': 78,
'min_sum_hessian_in_leaf': 8,
'num_leaves': 18,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1,
'lambda_l1': 7.961,
'lambda_l2': 7.781
}
mlp16_params = {'activation':'relu','solver':'lbfgs','tol':1e-06, 'hidden_layer_sizes':(16,), 'random_state':42}
knn_params ={'n_neighbors':17, 'p':2.9,'n_jobs':-1}
lgbm_meta_model = LgbmWrapper(params=param, num_rounds = 2000, ealry_stopping=100)
 mlp_meta_model = SklearnWrapper(neural_network.MLPClassifier,mlp16_params)
 knn_meta_model = SklearnWrapper(neighbors.KNeighborsClassifier,knn_params)
 third_number = 4
oof_train_5 = pd.DataFrame()
oof_test_5 = pd.DataFrame()
third_oof = np.zeros(len(train_df))
third_pred = np.zeros(len(test_df))
third_oof1 = np.zeros(len(train_df))
third_pred1 = np.zeros(len(test_df))
third_oof2 = np.zeros(len(train_df))
third_pred2 = np.zeros(len(test_df))
third_oof3 = np.zeros(len(train_df))
third_pred3 = np.zeros(len(test_df))
second_oof, second_pred, second_score = get_oof(lgbm_meta_model, x_train_second_layer1, train_df['target'], x_test_second_layer1, eval_func=roc_auc_score, NFOLDS=5, kfold_random_sate= SEED)
 second_oof1, second_pred1, second_score1 = get_oof(mlp_meta_model, x_train_second_layer1, train_df['target'], x_test_second_layer1, eval_func=roc_auc_score, NFOLDS=5, kfold_random_sate= SEED)
 second_oof2, second_pred2, second_score2 = get_oof(knn_meta_model, x_train_second_layer1, train_df['target'], x_test_second_layer1, eval_func=roc_auc_score, NFOLDS=5, kfold_random_sate= SEED)
 third_oof += second_oof
third_pred += second_pred
print(second_score)
 third_oof1 += second_oof1
third_pred1 += second_pred1
print(second_score1)
 third_oof2 += second_oof2
third_pred2 += second_pred2
print(second_score2)
 print("")
 oof_train_5['lgb'] = third_oof * 4
oof_test_5['lgb'] = third_pred * 4
oof_train_5['mlp'] = third_oof1 * 4
oof_test_5['mlp'] = third_pred1 * 4
oof_train_5['knn'] = third_oof2 * 4
oof_test_5['knn'] = third_pred2 * 4<concatenate>
learning_rate_reduction= ReduceLROnPlateau(monitor='val_loss',
patience=1,
verbose=1,
factor=0.5,
min_lr=0.0001)
 early = EarlyStopping(monitor='val_loss', min_delta=0.0005, mode='min', verbose=1, patience=2)
 history = model.fit_generator(train_datagen.flow(x_train, y_train, batch_size=1024),
steps_per_epoch=100,
epochs=30,
validation_data=valid_datagen.flow(x_valid, y_valid),
validation_steps=50,
callbacks=[learning_rate_reduction] )
x_train_second_layer = oof_train_1 + oof_train_2 + oof_train_3 + oof_train_4
x_test_second_layer = oof_test_1 + oof_test_2 + oof_test_3 + oof_test_4
x_train_second_layer = pd.concat([x_train_second_layer,oof_train_5],axis=1)
 x_test_second_layer = pd.concat([x_test_second_layer,oof_test_5],axis=1)
 print('Ensemble', roc_auc_score(train_df['target'], x_train_second_layer.mean(1)) )<save_to_csv>
predictions = model.predict_classes(x_test/255.)
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predictions
submission.to_csv("submission.csv", index=False )