Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<find_best_score>
import itertools
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense, Conv2D, Lambda, MaxPooling2D, Dropout, BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
study.best_value<find_best_params>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
best_params = study.best_params
best_params<prepare_x_and_y>
train_df.sample(frac=1)
 cols = list(train_df.columns)
 cols.remove('label')
 X = train_df[cols]
Y = train_df['label']
X_train, X_dev, Y_train, Y_dev = train_test_split(X, Y, test_size=0.1, random_state=0)
 X_test = test_df[cols]
def objective(trial):
hyper_params = {
'num_leaves': trial.suggest_int('num_leaves', 1, 63),
'min_data_in_leaf': trial.suggest_int('min_data_in_leaf ', 1, 100)
 }
scores = []
kf = KFold(5)
 for i,(train_idx, test_idx)in enumerate(kf.split(data)) :
X_train = data.iloc[train_idx][preds]
y_train = data.iloc[train_idx][target]
X_test = data.iloc[test_idx][preds]
y_test = data.iloc[test_idx][target]
hyper_params.update(best_params)
 estimator = LGBMRegressor(**hyper_params)
 estimator.fit(X_train,
y_train,
eval_set=(X_test, y_test),
eval_metric='rmse',
verbose=0)
 y_pred = estimator.predict(X_test)
 rmse = mean_squared_error(y_test, y_pred, squared=False)
 scores.append(rmse)
 return np.mean(scores )<find_best_params>
X_train = X_train.values.reshape(-1, 28, 28)
 X_dev = X_dev.values.reshape(-1, 28, 28)
 X_test = X_test.values.reshape(-1, 28, 28 )
study = optuna.create_study(direction='minimize')
 study.optimize(objective, timeout=3600*2.5 )<find_best_score>
X_train = np.expand_dims(X_train, axis=-1)/ 255
X_dev = np.expand_dims(X_dev, axis=-1)/ 255
X_test = np.expand_dims(X_test, axis=-1)/ 255
study.best_value<find_best_params>
def data_augmentation(x_data, y_data, batch_size):
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.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False 
)
 datagen.fit(x_data)
 train_data = datagen.flow(x_data, y_data, batch_size=batch_size, shuffle=True)
 return train_data
best_params.update(study.best_params)
 best_params<prepare_x_and_y>
BATCH_SIZE = 64
aug_train_data = data_augmentation(X_train, Y_train, BATCH_SIZE )
def objective(trial):
hyper_params = {
'bagging_freq': trial.suggest_int('bagging_freq', 1, 100),
'bagging_fraction': trial.suggest_float('bagging_fraction ', 0, 1.0),
'feature_fraction': trial.suggest_float('feature_fraction', 0, 1.0)
 }
scores = []
kf = KFold(5)
 for i,(train_idx, test_idx)in enumerate(kf.split(data)) :
X_train = data.iloc[train_idx][preds]
y_train = data.iloc[train_idx][target]
X_test = data.iloc[test_idx][preds]
y_test = data.iloc[test_idx][target]
hyper_params.update(best_params)
 estimator = LGBMRegressor(**hyper_params)
 estimator.fit(X_train,
y_train,
eval_set=(X_test, y_test),
eval_metric='rmse',
verbose=0)
 y_pred = estimator.predict(X_test)
 rmse = mean_squared_error(y_test, y_pred, squared=False)
 scores.append(rmse)
 return np.mean(scores )<find_best_params>
layers = [
Conv2D(filters=96, kernel_size=(11, 11), strides=2, activation='relu', input_shape=(28, 28, 1)) ,
MaxPooling2D(pool_size=(3, 3), strides=2),
Conv2D(filters=256, kernel_size=(5, 5), padding='same', activation='relu'),
Flatten() ,
Dense(9216, activation='relu'),
Dense(4096, activation='relu'),
Dense(4096, activation='relu'),
Dense(10, activation='softmax'),
]
study = optuna.create_study(direction='minimize')
 study.optimize(objective, timeout=3600*2 )<find_best_score>
model = Sequential(layers)
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
 model.compile(
 optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] 
)
 callbacks = [
tf.keras.callbacks.ReduceLROnPlateau(monitor="loss",factor=0.1, patience=2, min_lr=0.000001, verbose=1),
]
hist = model.fit(
 aug_train_data,
steps_per_epoch=X_train.shape[0] // BATCH_SIZE,
batch_size=BATCH_SIZE,
validation_data=(X_dev, Y_dev),
epochs=50,
callbacks=callbacks
)
study.best_value<find_best_params>
predictions = Y_pred
print(predictions[0])
 print('Predicted digit is: ' + str(np.argmax(predictions[0])))
 print('Accuracy is: ' + str(np.max(predictions[0] * 100)) + '%')
 plt.imshow(X_dev[0].reshape(( 28, 28)) , cmap=plt.cm.binary )
best_params.update(study.best_params)
 best_params<prepare_x_and_y>
predictions = model.predict(X_test )
def objective(trial):
hyper_params = {
'lambda_l1': trial.suggest_float('lambda_l1', 1E-12, 20, log=True),
'lambda_l2': trial.suggest_float('lambda_l2', 1E-12, 20, log=True)
 }
scores = []
kf = KFold(5)
 for i,(train_idx, test_idx)in enumerate(kf.split(data)) :
X_train = data.iloc[train_idx][preds]
y_train = data.iloc[train_idx][target]
X_test = data.iloc[test_idx][preds]
y_test = data.iloc[test_idx][target]
hyper_params.update(best_params)
 estimator = LGBMRegressor(**hyper_params)
 estimator.fit(X_train,
y_train,
eval_set=(X_test, y_test),
eval_metric='rmse',
verbose=0)
 y_pred = estimator.predict(X_test)
 rmse = mean_squared_error(y_test, y_pred, squared=False)
 scores.append(rmse)
 return np.mean(scores )<find_best_params>
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.head()
study = optuna.create_study(direction='minimize')
 study.optimize(objective, timeout=3600*2 )<find_best_score>
for i in submission.index:
submission['Label'][i] = np.argmax(predictions[i] )
study.best_value<find_best_params>
submission.to_csv("sample_submission.csv", index=False )
best_params.update(study.best_params)
 best_params<train_model>
model.save('model')
 model.save('model.h5' )