Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<init_hyperparams>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
parameters2 = {
'n_estimators': 350,
'tree_method': 'exact',
'learning_rate': 0.03,
'colsample_bytree': 0.9,
'subsample': 0.9,
'min_child_weight': 9,
'max_depth': 11,
'n_jobs': -1
}<train_on_grid>
batch_size = 86
num_nets = 15
<choose_model_class>
digits_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 digits_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
<train_model>
X = digits_train.drop(columns="label" ).values.reshape(digits_train.shape[0],28,28,1)/ 255.0
Y = to_categorical(digits_train["label"], num_classes=10)
 X_test = digits_test.values.reshape(digits_test.shape[0],28,28,1)/ 255.0
final_model = XGBRegressor(tree_method='hist', min_child_weight=9, max_depth=11, n_jobs=-1, colsample_bytree=0.5, learning_rate=0.01, n_estimators=1500)
 final_model.fit(X_train, y_train, early_stopping_rounds=10, eval_set=[(X_test, y_test)], verbose=False)
 prediction = final_model.predict(X_test)
 mse = mean_squared_error(y_test, prediction,squared=False)
 print(mse)
<save_to_csv>
datagen_train = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1
)
submission['target'] = final_model.predict(test)
 submission.to_csv('xgb_reg.csv' )<import_modules>
model = [0] * num_nets
for j in range(num_nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation = "relu", input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides = 2, padding = "same", activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size = 3, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides = 2, padding = "same", activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size = 4, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation = "softmax"))
model[j].compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["accuracy"] )
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib_venn import venn2
import shap
from optuna.integration import _lightgbm_tuner as lgb_tuner
import optuna
from catboost import CatBoost
from catboost import Pool
from catboost import cv
import category_encoders as ce
from tqdm import tqdm
import lightgbm as lgb
import xgboost as xgb
import joblib
import os
import logging
import datetime
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold
from sklearn.model_selection import StratifiedKFold
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
from sklearn.decomposition import PCA
from scipy import stats
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
from catboost import CatBoostRegressor<load_from_csv>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * num_nets
epochs = 45
for j in range(num_nets):
x_train, x_val, y_train, y_val = train_test_split(X, Y, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen_train.flow(x_train, y_train, batch_size = 64),
epochs = epochs,
steps_per_epoch = x_train.shape[0] // 64,
validation_data =(x_val, y_val),
callbacks = [annealer],
verbose = 0)
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )
<prepare_x_and_y><EOS> 
results = np.zeros(( X_test.shape[0], 10))
for j in range(num_nets):
results = results + model[j].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.ImageId = submission.ImageId.astype(int)
 submission.to_csv("prediction.csv", index = False )