Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<filter>
import pandas as pd
import numpy as np
import time
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.models import Model
from keras.layers import Input, Conv2D, BatchNormalization, Activation
from keras.layers import Add, Flatten, AveragePooling2D, Dense, Dropout
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
[len(x_train[x_train['NumberOfTime30-59DaysPastDueNotWorse'] == 98]),
len(x_train[x_train['NumberOfTime60-89DaysPastDueNotWorse'] == 98]),
len(x_train[x_train['NumberOfTimes90DaysLate'] == 98]),
len(x_train[x_train['NumberOfTime30-59DaysPastDueNotWorse'] == 98][x_train['NumberOfTimes90DaysLate'] == 98][x_train['NumberOfTime60-89DaysPastDueNotWorse'] == 98])]<filter>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 X = train.drop(columns=['label'] ).values.reshape(-1, 28, 28, 1)/ 255
y = train['label'].values
test = test.values.reshape(-1, 28, 28, 1)/ 255
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1 )
[len(x_test[x_test['NumberOfTime30-59DaysPastDueNotWorse'] == 98]),
len(x_test[x_test['NumberOfTime60-89DaysPastDueNotWorse'] == 98]),
len(x_test[x_test['NumberOfTimes90DaysLate'] == 98]),
len(x_test[x_test['NumberOfTime30-59DaysPastDueNotWorse'] == 98][x_test['NumberOfTimes90DaysLate'] == 98][x_test['NumberOfTime60-89DaysPastDueNotWorse'] == 98])]<feature_engineering>
def residual_block(inputs, filters, strides=1):

 y = inputs
x = Conv2D(
 filters=filters,
kernel_size=3,
strides=strides,
padding='same',
)(inputs)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(
 filters=filters,
kernel_size=3,
strides=1,
padding='same',
)(x)
 x = BatchNormalization()(x)
 if strides > 1:
y = Conv2D(
 filters=filters,
kernel_size=3,
strides=strides,
padding='same',
)(y)
 y = BatchNormalization()(y)
 x = Add()([x, y])
 x = Activation('relu' )(x)
 return x
def resnet(input_shape, num_classes, filters, stages):

 inputs = Input(shape=input_shape)
 x = Conv2D(
 filters=filters,
kernel_size=7,
strides=1,
padding='same',
)(inputs)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 for stage in stages:
x = residual_block(x, filters, strides=2)
 for i in range(stage-1):
x = residual_block(x, filters)
 filters *= 2
x = AveragePooling2D(4 )(x)
 x = Flatten()(x)
 x = Dropout(0.3 )(x)
 x = Dense(int(filters/4), activation='relu' )(x)
 outputs = Dense(num_classes, activation='softmax' )(x)
 model = Model(inputs=inputs, outputs=outputs)
 return model
for dataset in combine:
dataset.loc[dataset['MonthlyIncome'].isnull() ,'DebtRatio'] = dataset.loc[dataset['MonthlyIncome'].isnull() ,'DebtRatio'] / 3915;
dataset.loc[dataset['MonthlyIncome'] == 1,'DebtRatio'] = dataset.loc[dataset['MonthlyIncome'] == 1,'DebtRatio'] / 3915;
dataset.loc[dataset['MonthlyIncome'] == 0,'DebtRatio'] = dataset.loc[dataset['MonthlyIncome'] == 0,'DebtRatio'] / 3915;<categorify>
def train_model(epochs, filters, stages, batch_size, visualize=False):

 model = resnet(
 input_shape=X[0].shape,
num_classes=np.unique(y ).shape[-1],
filters=filters,
stages=stages 
)
 model.compile(
 loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'] 
)
 checkpoint = ModelCheckpoint(
 filepath=f'resnet-{int(time.time())}.dhf5',
monitor='loss',
save_best_only=True 
)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.8**x)
 callbacks = [checkpoint, annealer]
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1 
)
 datagen.fit(X)
 history = model.fit_generator(
 datagen.flow(X_train, y_train, batch_size=batch_size),
validation_data=(X_test, y_test),
epochs=epochs,
verbose=2,
workers=12,
callbacks=callbacks 
)
 if visualize:
fig, axarr = plt.subplots(1, 2, figsize=(16, 8))
axarr[0].plot(history.history['accuracy'])
 axarr[0].plot(history.history['val_accuracy'])
 axarr[0].set_title('Model accuracy')
 axarr[0].set_ylabel('Accuracy')
 axarr[0].set_xlabel('Epoch')
 axarr[0].legend(['Train', 'Test'], loc='upper left')
 axarr[1].plot(history.history['loss'])
 axarr[1].plot(history.history['val_loss'])
 axarr[1].set_title('Model loss')
 axarr[1].set_ylabel('Loss')
 axarr[1].set_xlabel('Epoch')
 axarr[1].legend(['Train', 'Test'], loc='upper left')
 plt.show()
return model
for dataset in combine:
dataset.drop(columns = ['Unnamed: 0'], inplace = True)
 dataset['RevolvingUtilizationOfUnsecuredLines'].mask(dataset['RevolvingUtilizationOfUnsecuredLines'] > 2, 2, inplace=True)
 dataset['NumberOfTime60-89DaysPastDueNotWorse'].mask(dataset['NumberOfTime60-89DaysPastDueNotWorse'] > 95, 20, inplace=True)
 dataset['NumberOfTime30-59DaysPastDueNotWorse'].mask(dataset['NumberOfTime30-59DaysPastDueNotWorse'] > 95, 20, inplace=True)
 dataset['NumberOfTimes90DaysLate'].mask(dataset['NumberOfTimes90DaysLate'] > 95, 20, inplace=True)
 dataset['90_sum'] = dataset['NumberOfTime30-59DaysPastDueNotWorse'] + dataset['NumberOfTimes90DaysLate'] + dataset['NumberOfTime60-89DaysPastDueNotWorse']
dataset['DebtRatio'].mask(dataset['DebtRatio'] > 400, 400, inplace=True)
 dataset['MonthlyIncome'].replace(np.nan, 0, inplace=True)
 dataset['MonthlyIncome'].replace(1, 0, inplace=True)
 dataset['NumberOfDependents'].replace(np.nan, 0, inplace=True)
 x_train = x_train[x_train['age'] > 20]
x_train = x_train[x_train['MonthlyIncome'] <= 1e6]
x_train = x_train[x_train['NumberRealEstateLoansOrLines'] <= 50]<split>
models = []
for i in range(1):
print('-------------------------')
 print('Model: ', i+1)
 print('-------------------------')
 model = train_model(
 epochs=10,
filters=64,
stages=[3, 3, 3],
batch_size=128,
visualize=True 
)
 models.append(model )
result = x_train['SeriousDlqin2yrs' ]
data = x_train.drop(['SeriousDlqin2yrs'], axis = 1)
 train_data, test_data, train_result, test_result = train_test_split(data, result, test_size = 0.2, stratify = result, random_state=33 )<import_modules>
predictions = []
for model in models:
predictions.append(model.predict(test))
predictions = np.sum(predictions, axis=0)
 predictions = np.argmax(predictions, axis=1)
 submission = pd.DataFrame({'ImageId': np.arange(1, 28001, 1), 'Label': predictions})
 submission.to_csv('mnist_resnet_submission.csv', index=False )