Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<load_from_csv>
import numpy as np
import pandas as pd
import tensorflow as tf
train_df = pd.read_csv('.. /input/train.csv')
 print('Rows: ',train_df.shape[0],'Columns: ',train_df.shape[1])
 train_df.info()<count_values>
Activation, LeakyReLU, Flatten, Dropout, BatchNormalization
train_df['target'].value_counts()<load_from_csv>
train_datas=pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 val_datas = pd.read_csv(".. /input/Kannada-MNIST/Dig-MNIST.csv" )
test_df = pd.read_csv('.. /input/test.csv' )<drop_column>
datas = pd.concat([train_datas,val_datas],axis=0)
 print(datas.shape)
 datas_X = np.array(datas.drop("label",axis=1),dtype=np.float32)
 datas_Y = np.array(datas[["label"]],dtype=np.int32)
 train_X,val_X,train_Y,val_Y = train_test_split(datas_X,datas_Y,test_size=0.2,shuffle=True )
X_test = test_df.drop('ID_code',axis=1 )<prepare_x_and_y>
train_X = train_X / 255.0
val_X = val_X / 255.0
train_X = np.reshape(train_X,(-1,28,28,1))
val_X = np.reshape(val_X,(-1,28,28,1))
X = train_df.drop(['ID_code','target'],axis=1)
 y = train_df['target']<choose_model_class>
from tensorflow.keras.layers import Activation,GlobalAveragePooling2D
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )<init_hyperparams>
model = Sequential()
model.add(Conv2D(64,(7,7), padding='same',activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3,3), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Conv2D(128,(3,3), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(2, 2))
model.add(Conv2D(256,(1,1), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Conv2D(256,(3,3), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3,3), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(512,(1,1), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Conv2D(128,(5,5), padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(GlobalAveragePooling2D())
 model.add(Dense(256,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64,activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss="sparse_categorical_crossentropy",metrics=["accuracy"])
 print(model.summary() )
params = {'num_leaves': 8,
'min_data_in_leaf': 42,
'objective': 'binary',
'max_depth': 16,
'learning_rate': 0.0123,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.8,
'feature_fraction': 0.8201,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 4}<train_model>
datagen = ImageDataGenerator(
 rotation_range = 20,
width_shift_range = 0.3,
height_shift_range = 0.3,
shear_range = 0.2,
zoom_range = 0.3,
horizontal_flip = False )
prediction = np.zeros(len(X_test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/5<train_model>
annealer = LearningRateScheduler(lambda x: 0.008 * 0.95 ** x)
 epochs = 10
batch_size = 128
prediction1 = np.zeros(len(X_test))
m = CatBoostClassifier(loss_function="Logloss",eval_metric="AUC",
boosting_type = 'Ordered')
 for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = Pool(X_train, label=y_train)
 valid_data = Pool(X_valid, label=y_valid)
 model1 = m.fit(train_data,eval_set=valid_data,use_best_model=True,verbose=300)
 prediction1 += model1.predict(X_test)/5<split>
model.fit_generator(datagen.flow(train_X,train_Y, batch_size=batch_size),
epochs = epochs, steps_per_epoch = math.ceil(train_X.shape[0]*1.0/batch_size),
validation_data =(val_X,val_Y), callbacks=[annealer],verbose=1 )
mod = xgb.XGBClassifier(max_depth=4,n_estimators=999999, colsample_bytree=0.7,subsample = 0.7,
min_child_weight = 50, eval_metric = "auc",gamma = 5,alpha = 0,
booster = "gbtree",colsample_bylevel = 0.7, learning_rate=0.1,
objective='binary:logistic', n_jobs=-1)
 prediction2 = np.zeros(len(X_test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
model2 = mod.fit(X_train, y_train, eval_set=[(X_valid, y_valid)],verbose=200, eval_metric='auc',
early_stopping_rounds=200)
 prediction2 += model2.predict(X_test, ntree_limit=model2.best_ntree_limit)/5<save_to_csv>
test_csv = pd.read_csv(".. /input/Kannada-MNIST/test.csv" )
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )<save_to_csv>
data_test = np.array(test_csv.drop("id",axis=1),dtype=np.float32)
 data_test = np.reshape(data_test,(-1,28,28,1))
X_test = data_test / 255.0
print(X_test.shape,X_test.dtype)
sub["target"] = prediction1
sub.to_csv("submission1.csv", index=False )<save_to_csv>
results = np.zeros(( X_test.shape[0],10))
results = model.predict(X_test)
 results = np.argmax(results,axis = 1 )
<save_to_csv><EOS> 
submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = results
submission.to_csv("submission.csv",index=False )