Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<categorify>
print(f'Using Tensorflow Version : {tf.__version__}')
 print(f'Using Keras Version : {keras.__version__}' )
ohe_columns = [
'NAME_CONTRACT_STATUS', 'NAME_CONTRACT_TYPE', 'CHANNEL_TYPE',
'NAME_TYPE_SUITE', 'NAME_YIELD_GROUP', 'PRODUCT_COMBINATION',
'NAME_PRODUCT_TYPE', 'NAME_CLIENT_TYPE']
prev, categorical_cols = one_hot_encoder(prev, ohe_columns, nan_as_category= False )<feature_engineering>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train.head()
prev['APPLICATION_CREDIT_DIFF'] = prev['AMT_APPLICATION'] - prev['AMT_CREDIT']
prev['APPLICATION_CREDIT_RATIO'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
prev['CREDIT_TO_ANNUITY_RATIO'] = prev['AMT_CREDIT']/prev['AMT_ANNUITY']
prev['DOWN_PAYMENT_TO_CREDIT'] = prev['AMT_DOWN_PAYMENT'] / prev['AMT_CREDIT']
total_payment = prev['AMT_ANNUITY'] * prev['CNT_PAYMENT']
prev['SIMPLE_INTERESTS'] =(total_payment/prev['AMT_CREDIT'] - 1)/prev['CNT_PAYMENT']<merge>
X = train.drop(['label'], axis = 1)
 y = to_categorical(train['label'].values )
approved = prev[prev['NAME_CONTRACT_STATUS_Approved'] == 1]
active_df = approved[approved['DAYS_LAST_DUE'] == 365243]
active_pay = pay[pay['SK_ID_PREV'].isin(active_df['SK_ID_PREV'])]
active_pay_agg = active_pay.groupby('SK_ID_PREV')[['AMT_INSTALMENT', 'AMT_PAYMENT']].sum()
active_pay_agg.reset_index(inplace= True)
 active_pay_agg['INSTALMENT_PAYMENT_DIFF'] = active_pay_agg['AMT_INSTALMENT'] - active_pay_agg['AMT_PAYMENT']
active_df = active_df.merge(active_pay_agg, on= 'SK_ID_PREV', how= 'left')
 active_df['REMAINING_DEBT'] = active_df['AMT_CREDIT'] - active_df['AMT_PAYMENT']
active_df['REPAYMENT_RATIO'] = active_df['AMT_PAYMENT'] / active_df['AMT_CREDIT']
active_agg_df = group(active_df, 'PREV_ACTIVE_', PREVIOUS_ACTIVE_AGG)
 active_agg_df['TOTAL_REPAYMENT_RATIO'] = active_agg_df['PREV_ACTIVE_AMT_PAYMENT_SUM']/\
active_agg_df['PREV_ACTIVE_AMT_CREDIT_SUM']
del active_pay, active_pay_agg, active_df; gc.collect()<categorify>
X = X / 255.0
X_test = test / 255.0
prev['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace= True)
 prev['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True )<feature_engineering>
datagen = ImageDataGenerator(rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
prev['DAYS_LAST_DUE_DIFF'] = prev['DAYS_LAST_DUE_1ST_VERSION'] - prev['DAYS_LAST_DUE']
approved['DAYS_LAST_DUE_DIFF'] = approved['DAYS_LAST_DUE_1ST_VERSION'] - approved['DAYS_LAST_DUE']<define_variables>
AUG_test(X[0], y[0] )
categorical_agg = {key: ['mean'] for key in categorical_cols}<merge>
def build_model() :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu',
input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same',
activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same',
activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer="adam", loss="categorical_crossentropy",
metrics=["accuracy"])
 return model
agg_prev = group(prev, 'PREV_', {**PREVIOUS_AGG, **categorical_agg})
 agg_prev = agg_prev.merge(active_agg_df, how='left', on='SK_ID_CURR')
 del active_agg_df; gc.collect()<merge>
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu )
agg_prev = group_and_merge(approved, agg_prev, 'APPROVED_', PREVIOUS_APPROVED_AGG)
 refused = prev[prev['NAME_CONTRACT_STATUS_Refused'] == 1]
agg_prev = group_and_merge(refused, agg_prev, 'REFUSED_', PREVIOUS_REFUSED_AGG)
 del approved, refused; gc.collect() 
<merge>
EPOCHS = 45
BATCH_SIZE = 16 * tpu_strategy.num_replicas_in_sync
for loan_type in ['Consumer loans', 'Cash loans']:
type_df = prev[prev['NAME_CONTRACT_TYPE_{}'.format(loan_type)] == 1]
prefix = 'PREV_' + loan_type.split(" ")[0] + '_'
agg_prev = group_and_merge(type_df, agg_prev, prefix, PREVIOUS_LOAN_TYPE_AGG)
 del type_df; gc.collect()<feature_engineering>
scores, History = [], []
pred = np.zeros(shape =(len(test), 10))
cv = KFold(n_splits=15, shuffle = True, random_state = 42)
 for fold,(train_index, val_index)in enumerate(cv.split(X)) :
start = default_timer()
K.clear_session()
tf.tpu.experimental.initialize_tpu_system(tpu)
 X_train , y_train = X[train_index], y[train_index]
X_val, y_val = X[val_index], y[val_index]
Train_x, Train_y = None, None
batch = 0
for x_batch, y_batch in datagen.flow(X_train, y_train,
batch_size=BATCH_SIZE):
if batch == 0:
Train_x, Train_y = x_batch, y_batch
elif batch >= X.shape[0] // BATCH_SIZE:
break
else:
Train_x = np.concatenate(( Train_x, x_batch))
Train_y = np.concatenate(( Train_y, y_batch))
batch += 1
with tpu_strategy.scope() :
model = build_model()
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 sv = ModelCheckpoint(f'Model Fold {fold}.h5', monitor='val_accuracy',
save_best_only=True, mode='max')
 history = model.fit(Train_x, Train_y, batch_size = BATCH_SIZE,
epochs = EPOCHS, verbose = 0, callbacks=[annealer, sv],
steps_per_epoch = X_train.shape[0]//BATCH_SIZE,
validation_data =(X_val, y_val))
History.append(history.history)
 model = load_model(f'Model Fold {fold}.h5')
 score = model.evaluate(X_val, y_val, verbose = 0)[1]
scores.append(score)
 pred += model.predict(X_test)
 time = round(default_timer() - start, 4)
 print(f'[INFO] Fold {fold + 1} val_accuracy : {round(score, 4)} - Time : {time} s')
 print()
print(f'[INFO] Mean CV scores : {round(sum(scores)/len(scores), 4)}' )
pay['LATE_PAYMENT'] = pay['DAYS_ENTRY_PAYMENT'] - pay['DAYS_INSTALMENT']
pay['LATE_PAYMENT'] = pay['LATE_PAYMENT'].apply(lambda x: 1 if x > 0 else 0)
 dpd_id = pay[pay['LATE_PAYMENT'] > 0]['SK_ID_PREV'].unique()<merge>
%%HTML
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}
img {max-width:100%; height:auto} 
.tab {
overflow: hidden;
border: 1px solid
background-color:
} 
.tab button {
background-color:
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
} 
.tab button:hover {
background-color:
} 
.tab button.active {
background-color:
} 
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid
border-top: none;
}
</style>
</head>
<body>
<h1>History</h1>
<p>Click on the buttons inside the tabbed menu</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'loss')">Loss</button>
<button class="tablinks" onclick="openCity(event, 'val_loss')">Val Loss</button>
<button class="tablinks" onclick="openCity(event, 'accuracy')">Accuracy</button>
<button class="tablinks" onclick="openCity(event, 'val_accuracy')">Val Accuracy</button>
</div>
<div id="loss" class="tabcontent">
<h3>Loss</h3>
<p>Loss History</p>
<img src="./loss.png" alt="loss.png">
</div>
<div id="val_loss" class="tabcontent">
<h3>Val Loss</h3>
<p>Val Loss History</p>
<img src="./val_loss.png" alt="val_loss.png">
</div>
<div id="accuracy" class="tabcontent">
<h3>Accuracy</h3>
<p>Accuracy History</p>
<img src="./accuracy.png" alt="accuracy.png">
</div>
<div id="val_accuracy" class="tabcontent">
<h3>Val Accuracy</h3>
<p>Val Accuracy History</p>
<img src="./val_accuracy.png" alt="val_accuracy.png">
</div>
<script>
function openCity(evt, cityName){
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for(i = 0; i < tabcontent.length; i++){
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for(i = 0; i < tablinks.length; i++){
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName ).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
</body>
agg_dpd = group_and_merge(prev[prev['SK_ID_PREV'].isin(dpd_id)], agg_prev,
'PREV_LATE_', PREVIOUS_LATE_PAYMENTS_AGG)
 del agg_dpd, dpd_id; gc.collect()<merge>
sub = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 sub['Label'] = pred
sub.head()
<split><EOS> 
sub.to_csv('Submission.csv', index=False )