Kaggle competition: Digit Recognizer
bureau = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv')
 print("Size of bureau data", bureau.shape )<merge>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from tensorflow.keras import utils
from tensorflow.keras.preprocessing import image
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
import tensorflow as tf
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
PAST_LOANS_PER_CUS = bureau[['SK_ID_CURR', 'DAYS_CREDIT']].groupby(by = ['SK_ID_CURR'])['DAYS_CREDIT'].count().reset_index().rename(index=str, columns={'DAYS_CREDIT': 'BUREAU_LOAN_COUNT'})
 app_train_bureau = prev_app_merge.merge(PAST_LOANS_PER_CUS, on = ['SK_ID_CURR'], how = 'left')
 print(app_train_bureau.shape)
 app_train_bureau.head()<merge>
data_train = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', skiprows = 1, delimiter= ',')
 data_train[0:5]
BUREAU_LOAN_TYPES = bureau[['SK_ID_CURR', 'CREDIT_TYPE']].groupby(by = ['SK_ID_CURR'])['CREDIT_TYPE'].nunique().reset_index().rename(index=str, columns={'CREDIT_TYPE': 'BUREAU_LOAN_TYPES'})
 app_train_bureau = app_train_bureau.merge(BUREAU_LOAN_TYPES, on = ['SK_ID_CURR'], how = 'left' ).fillna(0)
 print(app_train_bureau.shape)
 app_train_bureau.head()<feature_engineering>
x_train = data_train[:, 1:]
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
 input_shape =(28, 28, 1 )
app_train_bureau['AVERAGE_LOAN_TYPE'] = app_train_bureau['BUREAU_LOAN_COUNT']/app_train_bureau['BUREAU_LOAN_TYPES']
app_train_bureau = app_train_bureau.fillna(0)
 print(app_train_bureau.shape)
 app_train_bureau.head()<drop_column>
x_train = x_train / 255.0
del app_train_bureau['BUREAU_LOAN_COUNT'], app_train_bureau['BUREAU_LOAN_TYPES']
app_train_bureau.head()<feature_engineering>
y_train = data_train[:, 0]
y_train[:5]
def f(x):
if x == 'Closed':
y = 0
else:
y = 1
return y
bureau_fe1 = bureau
bureau_fe1['CREDIT_ACTIVE_CLOSED'] = bureau_fe1.apply(lambda x: f(x.CREDIT_ACTIVE), axis = 1)
 bureau_fe1.head()<merge>
y_train = utils.to_categorical(y_train )
grp = bureau_fe1.groupby(by = ['SK_ID_CURR'])['CREDIT_ACTIVE_CLOSED'].mean().reset_index().rename(index=str, columns={'CREDIT_ACTIVE_CLOSED':'ACTIVE_LOANS_PERCENTAGE'})
 app_train_bureau = app_train_bureau.merge(grp, on = ['SK_ID_CURR'], how = 'left')
 del bureau_fe1['CREDIT_ACTIVE_CLOSED']
print(bureau_fe1.shape)
 bureau_fe1.head()<data_type_conversions>
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed )
app_train_bureau = app_train_bureau.fillna(0)
 app_train_bureau.head()<feature_engineering>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1)
app_train_bureau['BUREAU_ENDDATE_FACT_DIFF'] = bureau['DAYS_CREDIT_ENDDATE'] - bureau['DAYS_ENDDATE_FACT']
app_train_bureau['BUREAU_CREDIT_FACT_DIFF'] = bureau['DAYS_CREDIT'] - bureau['DAYS_ENDDATE_FACT']
app_train_bureau['BUREAU_CREDIT_ENDDATE_DIFF'] = bureau['DAYS_CREDIT'] - bureau['DAYS_CREDIT_ENDDATE']
app_train_bureau['BUREAU_CREDIT_DEBT_RATIO'] = bureau['AMT_CREDIT_SUM_DEBT'] / bureau['AMT_CREDIT_SUM']
app_train_bureau['BUREAU_CREDIT_DEBT_DIFF'] = bureau['AMT_CREDIT_SUM_DEBT'] - bureau['AMT_CREDIT_SUM']
app_train_bureau = app_train_bureau.fillna(0)
 app_train_bureau.head()<prepare_output>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"] )
app_train = app_train_bureau<split>
checkpoint = ModelCheckpoint('mnist-cnn.h5',
monitor='val_acc',
save_best_only=True,
verbose=1 )
ftr_app = app_train.drop(columns=['SK_ID_CURR','TARGET'])
 target_app = app_train['TARGET']
train_x, valid_x, train_y, valid_y = train_test_split(ftr_app, target_app, test_size=0.3, random_state=2020)
 train_x.shape, valid_x.shape<train_on_grid>
learn_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
def lgb_cv(num_leaves, learning_rate, n_estimators, subsample, colsample_bytree, reg_alpha, reg_lambda, x_data=None, y_data=None, n_splits=5, output='score'):
score = 0
kf = KFold(n_splits=n_splits)
 models = []
for train_index, valid_index in kf.split(x_data):
x_train, y_train = x_data.reindex([train_index]), y_data.reindex([train_index])
 x_valid, y_valid = x_data.reindex([valid_index]), y_data.reindex([valid_index])
 model = LGBMClassifier(
 num_leaves = int(num_leaves),
learning_rate = learning_rate,
n_estimators = int(n_estimators),
subsample = np.clip(subsample, 0, 1),
colsample_bytree = np.clip(colsample_bytree, 0, 1),
reg_alpha = reg_alpha,
reg_lambda = reg_lambda,
max_depth=16, 
)
 model.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)], eval_metric= 'auc', verbose= False,
early_stopping_rounds= 100)
 models.append(model)
 pred = model.predict_proba(valid_x)[:, 1]
true = valid_y
score += roc_auc_score(true, pred)/n_splits
if output == 'score':
return score
if output == 'model':
return models<choose_model_class>
batch_size=96
history = model.fit(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs=30,
validation_data=(X_val, Y_val),
steps_per_epoch=X_train.shape[0] // batch_size,
verbose=1,
callbacks=[checkpoint, learn_rate_reduction] )
func_fixed = partial(lgb_cv, x_data=train_x, y_data=train_y, n_splits=5, output='score')
 lgbBO = BayesianOptimization(
 func_fixed,
{
'num_leaves':(16, 1024),
'learning_rate':(0.0001, 0.1),
'n_estimators':(16, 1024),
'subsample':(0, 1),
'colsample_bytree':(0, 1),
'reg_alpha':(0, 10),
'reg_lambda':(0, 50),
},
random_state=2020 
)
 lgbBO.maximize(init_points=5, n_iter=10 )<train_model>
data_test = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', skiprows = 1, delimiter=',')
 x_test = data_test.reshape(data_test.shape[0], 28, 28, 1)
 x_test /= 255.0
clf = LGBMClassifier(
 n_estimators=int(lgbBO.max['params']['n_estimators']),
learning_rate=lgbBO.max['params']['learning_rate'],
num_leaves=int(lgbBO.max['params']['num_leaves']),
subsample=lgbBO.max['params']['subsample'],
max_depth=16,
reg_alpha=lgbBO.max['params']['reg_alpha'],
reg_lambda=lgbBO.max['params']['reg_lambda'])
 clf.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)], eval_metric= 'auc', verbose= 100,
early_stopping_rounds= 200 )<merge>
predict = model.predict(x_test)
 predict = np.argmax(predict, axis=1 )
test_merge = app_test.merge(prev_amt_agg, on='SK_ID_CURR', how='left', indicator=False)
 test_merge = test_merge.merge(prev_approved, on='SK_ID_CURR', how='left', indicator=False)
 test_merge = test_merge.merge(prev_refused, on='SK_ID_CURR', how='left', indicator=False)
 test_merge['PRE_CONTRACT_APPROVED_RATE'] = test_merge['PRE_CONTRACT_APPROVED'] /(test_merge['PRE_CONTRACT_APPROVED'] + test_merge['PRE_CONTRACT_REFUSED'])
 test_merge['PRE_CONTRACT_REFUSED_RATE'] = test_merge['PRE_CONTRACT_REFUSED'] /(test_merge['PRE_CONTRACT_APPROVED'] + test_merge['PRE_CONTRACT_REFUSED'])
 test_merge = test_merge.replace(float('NaN'),0)
 test_merge = test_merge.merge(PAST_LOANS_PER_CUS, on = ['SK_ID_CURR'], how = 'left')
 test_merge = test_merge.merge(BUREAU_LOAN_TYPES, on = ['SK_ID_CURR'], how = 'left' ).fillna(0)
 test_merge['AVERAGE_LOAN_TYPE'] = test_merge['BUREAU_LOAN_COUNT']/test_merge['BUREAU_LOAN_TYPES']
test_merge = test_merge.fillna(0)
 del test_merge['BUREAU_LOAN_COUNT'], test_merge['BUREAU_LOAN_TYPES']
test_merge = test_merge.merge(grp, on = ['SK_ID_CURR'], how = 'left' ).fillna(0)
 test_merge['BUREAU_ENDDATE_FACT_DIFF'] = bureau['DAYS_CREDIT_ENDDATE'] - bureau['DAYS_ENDDATE_FACT']
test_merge['BUREAU_CREDIT_FACT_DIFF'] = bureau['DAYS_CREDIT'] - bureau['DAYS_ENDDATE_FACT']
test_merge['BUREAU_CREDIT_ENDDATE_DIFF'] = bureau['DAYS_CREDIT'] - bureau['DAYS_CREDIT_ENDDATE']
test_merge['BUREAU_CREDIT_DEBT_RATIO'] = bureau['AMT_CREDIT_SUM_DEBT'] / bureau['AMT_CREDIT_SUM']
test_merge['BUREAU_CREDIT_DEBT_DIFF'] = bureau['AMT_CREDIT_SUM_DEBT'] - bureau['AMT_CREDIT_SUM']
test_merge = test_merge.fillna(0)
 preds = clf.predict_proba(test_merge.drop(columns=['SK_ID_CURR'])) [:, 1]
app_test['TARGET'] = preds
app_test[['SK_ID_CURR', 'TARGET']].to_csv('result_00.csv', index=False )<set_options>
out = np.column_stack(( range(1, predict.shape[0]+1), predict))
<import_modules><EOS> 
np.savetxt('submission.csv', out, header="ImageId,Label",
comments="", fmt="%d,%d" )