Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<init_hyperparams>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout, BatchNormalization
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop, Adam, Adagrad, Adamax, Adadelta
from sklearn.metrics import classification_report, confusion_matrix
LGB_BO.probe(
 params={'feature_fraction': 0.1403,
'lambda_l1': 4.218,
'lambda_l2': 1.734,
'learning_rate': 0.07,
'max_depth': 14,
'min_data_in_leaf': 17,
'min_gain_to_split': 0.1501,
'min_sum_hessian_in_leaf': 0.000446,
'num_leaves': 6},
lazy=True,
)<find_best_params>
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv" )
LGB_BO.maximize(init_points=0, n_iter=0 )<train_model>
train_X, train_y = train.drop(columns=['label']), train["label"]
for i, res in enumerate(LGB_BO.res):
print("Iteration {}:
\t{}".format(i, res))<init_hyperparams>
train_X = np.array(train_X)
 train_y = np.array(train_y )
param_lgb = {
'num_leaves': int(LGB_BO.max['params']['num_leaves']),
'max_bin': 63,
'min_data_in_leaf': int(LGB_BO.max['params']['min_data_in_leaf']),
'learning_rate': LGB_BO.max['params']['learning_rate'],
'min_sum_hessian_in_leaf': LGB_BO.max['params']['min_sum_hessian_in_leaf'],
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': LGB_BO.max['params']['feature_fraction'],
'lambda_l1': LGB_BO.max['params']['lambda_l1'],
'lambda_l2': LGB_BO.max['params']['lambda_l2'],
'min_gain_to_split': LGB_BO.max['params']['min_gain_to_split'],
'max_depth': int(LGB_BO.max['params']['max_depth']),
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}<define_variables>
testx = test.drop(columns=['id'] )
nfold = 5<set_options>
testx = np.array(testx )
gc.collect()<choose_model_class>
train_X = train_X / 255.0
testx = testx / 255.0
skf = StratifiedKFold(n_splits=nfold, shuffle=True, random_state=2019 )<split>
train_y = to_categorical(train_y, num_classes = len(np.unique(train["label"])) )
oof = np.zeros(len(train_df))
predictions = np.zeros(( len(test_df),nfold))
i = 1
for train_index, valid_index in skf.split(train_df, train_df.target.values):
print("
fold {}".format(i))
xg_train = lgb.Dataset(train_df.iloc[train_index][predictors].values,
label=train_df.iloc[train_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train_df.iloc[valid_index][predictors].values,
label=train_df.iloc[valid_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 clf = lgb.train(param_lgb, xg_train, 5000, valid_sets = [xg_valid], verbose_eval=250, early_stopping_rounds = 50)
 oof[valid_index] = clf.predict(train_df.iloc[valid_index][predictors].values, num_iteration=clf.best_iteration)
 predictions[:,i-1] += clf.predict(test_df[predictors], num_iteration=clf.best_iteration)
 i = i + 1
print("
CV AUC: {:<0.2f}".format(metrics.roc_auc_score(train_df.target.values, oof)) )<feature_engineering>
X_train, X_test, y_train, y_test = train_test_split(train_X, train_y, test_size=0.25, random_state=42, stratify= train_y, shuffle=True )
print("Rank averaging on", nfold, "fold predictions")
 rank_predictions = np.zeros(( predictions.shape[0],1))
for i in range(nfold):
rank_predictions[:, 0] = np.add(rank_predictions[:, 0], rankdata(predictions[:, i].reshape(-1,1)) /rank_predictions.shape[0])
 rank_predictions /= nfold<create_dataframe>
epochs = 20
batch_size = 16
sub_df = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub_df["target"] = rank_predictions
sub_df[:10]<save_to_csv>
def create_model() :
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 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(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 256, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 256, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 256, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Dense(10, activation = "softmax"))
return model
sub_df.to_csv("Customer_Transaction_rank_predictions.csv", index=False )<import_modules>
optimizer = Adamax()
model.compile(optimizer=optimizer, loss = 'categorical_crossentropy', metrics=['accuracy'])
 lr_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 es = EarlyStopping(monitor='val_loss',
min_delta=0,
patience=5,
verbose=0, mode='auto')
import os
import gc
import pickle
from pathlib import Path
import numpy as np
import pandas as pd
import random
from sklearn.preprocessing import minmax_scale
from sklearn.model_selection import GridSearchCV, StratifiedKFold 
<define_variables>
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.12,
height_shift_range=0.12,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
MODEL_NAME = "santander_2"
RESCALE = False
MIN_MAX =(-1,1 )<load_from_csv>
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs,
validation_data =(X_test,y_test),
steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[lr_reduction, es],
shuffle=True )
input_path = Path(".. /input")
 train_csv = str(input_path / "train.csv")
 test_csv = str(input_path / "test.csv" )<load_from_csv>
model.evaluate(X_train, y_train), model.evaluate(X_test, y_test )
train_df = pd.read_csv(train_csv)
 test_df = pd.read_csv(test_csv)
 train_df.head()<categorify>
def create_sub() :
results = model.predict(testx)
 results = np.argmax(results,axis = 1)
 df_sub = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 df_sub['label'] = results
df_sub.to_csv("submission.csv",index=False )
<choose_model_class><EOS> 
create_sub()