Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_values>
from matplotlib import pyplot as plt
import os
import scipy
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import seaborn as sns
from sklearn.model_selection import train_test_split
import cv2
app_train['TARGET'].value_counts()<define_variables>
main_path = r".. /input/digit-recognizer"
train_df = pd.read_csv(os.path.join(main_path, "train.csv"))
test_df = pd.read_csv(os.path.join(main_path, "test.csv"))
columns = ['AMT_INCOME_TOTAL','AMT_CREDIT', 'AMT_ANNUITY', 'AMT_GOODS_PRICE', 'DAYS_BIRTH', 'DAYS_EMPLOYED', 'DAYS_ID_PUBLISH',
'DAYS_REGISTRATION', 'DAYS_LAST_PHONE_CHANGE', 'CNT_FAM_MEMBERS', 'REGION_RATING_CLIENT', 'EXT_SOURCE_1',
'EXT_SOURCE_2', 'EXT_SOURCE_3', 'AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY', 'AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON', 'AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR']
show_hist_by_target(app_train, columns )<feature_engineering>
x_train = train_df.drop(labels=["label"], axis=1)
 y_train = train_df["label"]
y_train.head()
app_train['DAYS_BIRTH']=abs(app_train['DAYS_BIRTH'])
 app_train['DAYS_BIRTH'].corr(app_train['TARGET'] )<count_missing_values>
x_train = x_train.to_numpy() / 255.0
x_test = test_df.to_numpy() / 255.0
x_train = x_train.reshape(-1, 28, 28, 1)
 x_test = x_test.reshape(-1, 28, 28, 1)
 y_train = to_categorical(y_train)
 plt.imshow(x_train[125, :, :, :] )
app_train[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].isnull().sum()<count_values>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.15, random_state=2)
 datagen = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.2
)
app_train['EXT_SOURCE_3'].value_counts(dropna=False )<count_values>
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=64, 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=64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
model.summary()
cond_1 =(app_train['TARGET'] == 1)
 cond_0 =(app_train['TARGET'] == 0)
 print(app_train['CODE_GENDER'].value_counts() /app_train.shape[0])
 print('
연체인 경우
',app_train[cond_1]['CODE_GENDER'].value_counts() /app_train[cond_1].shape[0])
 print('
연체가 아닌 경우
',app_train[cond_0]['CODE_GENDER'].value_counts() /app_train[cond_0].shape[0] )<load_from_csv>
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('val_accuracy')> 0.9955):
print("Stop training!")
 self.model.stop_training = True
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 reduce_lr = ReduceLROnPlateau(
 monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 
)
 epoch_end = myCallback()
app_train = pd.read_csv('.. /input/home-credit-default-risk/application_train.csv')
 app_test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv' )<concatenate>
history = model.fit(datagen.flow(x_train, y_train, batch_size=256),
epochs=200, validation_data=(x_val, y_val),
verbose=1, steps_per_epoch=x_train.shape[0]/256,
callbacks=[reduce_lr, epoch_end] )
<feature_engineering><EOS> 
results = model.predict(x_test)
 results = np.argmax(results, axis=1)
 submission = pd.read_csv(os.path.join(main_path, "sample_submission.csv"))
image_id = range(1, x_test.shape[0]+1)
 submission = pd.DataFrame({'Imageid':image_id, 'Label':results})
 submission.to_csv('cnn2_submission.csv', index=False )