Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<categorify>
import numpy as np
import pandas as pd
from time import time
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Flatten, Dense, Dropout
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
drop_elements = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp','Parch']
train = train.drop(drop_elements, axis = 1)
 test = test.drop(drop_elements, axis = 1)
 def checkNull_fillData(df):
for col in df.columns:
if len(df.loc[df[col].isnull() == True])!= 0:
if df[col].dtype == "float64" or df[col].dtype == "int64":
df.loc[df[col].isnull() == True,col] = df[col].mean()
else:
df.loc[df[col].isnull() == True,col] = df[col].mode() [0]
checkNull_fillData(train)
 checkNull_fillData(test)
 str_list = []
num_list = []
for colname, colvalue in train.iteritems() :
if type(colvalue[1])== str:
str_list.append(colname)
 else:
num_list.append(colname)
 train = pd.get_dummies(train, columns=str_list)
 test = pd.get_dummies(test, columns=str_list )<prepare_x_and_y>
data_train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 data_test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
y = train[TARGET]
X = train.drop([TARGET],axis=1)
 X_test = test
gc.collect()<split>
target_train = data_train.iloc[:, 0].values
image_train = data_train.iloc[:, 1:].values
image_test = data_test.values
X_train,X_val,y_train,y_val=train_test_split(X,y,test_size=TEST_SIZE,random_state=RANDOM_SEED )<train_on_grid>
X = image_train.reshape(-1,28,28,1 ).astype('float32')
 X_test = image_test.reshape(-1,28,28,1 ).astype('float32')
 y = to_categorical(target_train.reshape(-1,1), num_classes=10)
 X /= 255
X_test /= 255
plt.imshow(image_train[0].reshape(28,28))
def build_random_forest(hp):
model = ensemble.RandomForestClassifier(
 n_estimators=hp.Int('n_estimators', 10, 50, step=10),
max_depth=hp.Int('max_depth', 3, 10))
return model
tuner = kt.tuners.Sklearn(
 oracle=kt.oracles.BayesianOptimization(
 objective=kt.Objective('score', 'max'),
max_trials=10),
hypermodel= build_random_forest,
directory='.',
project_name='random_forest')
 tuner.search(X_train.values, y_train.values.ravel())
 best_hp = tuner.get_best_hyperparameters(num_trials=1)[0]<train_model>
datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.1)
 datagen.fit(X )
model = tuner.hypermodel.build(best_hp)
 model.fit(X_train, y_train.values )<predict_on_test>
num_models = 15
model = [None] * num_models
for i in range(num_models):
model[i] = Sequential()
model[i].add(Conv2D(32,(3, 3), activation='relu', input_shape=(28,28,1)))
 model[i].add(BatchNormalization())
 model[i].add(Conv2D(32,(3, 3), activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(MaxPooling2D(( 2, 2), strides=2))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(64,(3, 3), activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(64,(3, 3), activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(MaxPooling2D(( 2, 2), strides=2))
model[i].add(Flatten())
 model[i].add(BatchNormalization())
 model[i].add(Dense(256, activation='relu'))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.5))
model[i].add(Dense(10, activation='softmax'))
model[i].compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 for i in range(num_models):
t = time()
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=i)
 history = model[i].fit_generator(datagen.flow(X_train, y_train, batch_size=512), epochs=50, verbose=0,
validation_data=(X_val, y_val))
print(history.history['val_accuracy'][-1], time() -t )
pred_val = model.predict(X_val)
 print(accuracy_score(y_val, pred_val))<predict_on_test>
y_pred = np.zeros(( X_test.shape[0], 10))
for i in range(num_models):
y_pred += model[i].predict(X_test, batch_size=512, verbose=1)
 y_pred = np.argmax(y_pred, axis=1)
 submissions = pd.DataFrame({'ImageId': list(range(1,len(y_pred)+1)) ,
'Label': y_pred})
 submissions.to_csv('submission.csv', index=False, header=True )
<save_to_csv><EOS> 
y_pred_val = np.zeros(( X_val.shape[0], 10))
for i in range(num_models):
y_pred_val += model[i].predict(X_val, batch_size=512, verbose=1)
 y_pred_val = np.argmax(y_pred_val, axis=1)
 y_true_val = np.argmax(y_val, axis=1)
 print(classification_report(y_true_val, y_pred_val, digits=4))