Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<compute_test_metric>
np.random.seed(0)
 %matplotlib inline
print('Accuracy Score : ' + str(accuracy_score(y_test,y_pred)))
 print('Precision Score : ' + str(precision_score(y_test,y_pred)))
 print('Recall Score : ' + str(recall_score(y_test,y_pred)))
 print('F1 Score : ' + str(f1_score(y_test,y_pred)))
 print('Confusion Matrix :
' + str(confusion_matrix(y_test,y_pred)) )<compute_test_metric>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 X_train = train.drop(labels = ["label"], axis = 1)
 y_train = train['label']
X_test = test
print(X_train.shape, X_test.shape )
clf = LGBMClassifier().fit(X_train,y_train)
 y_pred = clf.predict(X_test)
 print('Accuracy Score : ' + str(accuracy_score(y_test,y_pred)))
 print('Precision Score : ' + str(precision_score(y_test,y_pred)))
 print('Recall Score : ' + str(recall_score(y_test,y_pred)))
 print('F1 Score : ' + str(f1_score(y_test,y_pred)))
 print('Confusion Matrix :
' + str(confusion_matrix(y_test,y_pred)) )<compute_train_metric>
img_rows, img_cols = 28, 28
num_pixels = X_train.shape[1]
input_shape =(img_rows, img_cols )
clf = LogisticRegression()
grid_values = {'penalty': ['l2'],'C':[0.001,.009,0.01,.09,1,5,10,25]}
grid_clf_acc = GridSearchCV(clf, param_grid = grid_values,scoring = 'recall')
 grid_clf_acc.fit(X_train, y_train)
 y_pred_acc = grid_clf_acc.predict(X_test)
 print('Accuracy Score : ' + str(accuracy_score(y_test,y_pred_acc)))
 print('Precision Score : ' + str(precision_score(y_test,y_pred_acc)))
 print('Recall Score : ' + str(recall_score(y_test,y_pred_acc)))
 print('F1 Score : ' + str(f1_score(y_test,y_pred_acc)))
 confusion_matrix(y_test,y_pred_acc )<predict_on_test>
X_train /= 255
X_test /= 255
y_train = to_categorical(y_train)
 num_classes = y_train.shape[1]
print(X_train.shape, X_test.shape )
pred = model.predict_proba(df_test )<save_to_csv>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 2 )
submit = test[['SK_ID_CURR']]
submit['TARGET'] = pred
submit.to_csv('lgbm_Minimized_code.csv', index = False )<set_options>
def get_mlp() :
return Sequential([
Dense(512, input_dim = num_pixels, activation='relu'),
Dense(num_classes, activation='softmax')
 ] )
warnings.simplefilter(action='ignore', category=FutureWarning )<define_variables>
model = get_mlp()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary()
DATA_DIRECTORY = ".. /input/home-credit-default-risk"<load_from_csv>
learning_history = model.fit(X_train, y_train,
batch_size = 1024, epochs = 40, verbose = 2,
validation_data=(X_val, y_val)) ;
df_train = pd.read_csv(os.path.join(DATA_DIRECTORY, 'application_train.csv'))
df_test = pd.read_csv(os.path.join(DATA_DIRECTORY, 'application_test.csv'))
df = df_train.append(df_test)
 del df_train, df_test; gc.collect()<categorify>
score = model.evaluate(X_val, y_val, verbose = 0)
 print('Test loss: {}%'.format(score[0] * 100))
print('Test accuracy: {}%'.format(score[1] * 100))
print("MLP Error: %.2f%%" %(100 - score[1] * 100))
df = df[df['AMT_INCOME_TOTAL'] < 20000000]
df = df[df['CODE_GENDER'] != 'XNA']
df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace=True)
 df['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True )<groupby>
def get_mlpv2() :
return Sequential([
Dense(512, input_dim=num_pixels, activation='relu'),
Dropout(0.3),
Dense(256, activation='relu'),
Dropout(0.2),
Dense(128, activation='relu'),
Dense(num_classes, kernel_initializer='normal', activation='softmax')
 ] )
def get_age_group(days_birth):
age_years = -days_birth / 365
if age_years < 27: return 1
elif age_years < 40: return 2
elif age_years < 50: return 3
elif age_years < 65: return 4
elif age_years < 99: return 5
else: return 0<feature_engineering>
model = get_mlpv2()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary()
docs = [f for f in df.columns if 'FLAG_DOC' in f]
df['DOCUMENT_COUNT'] = df[docs].sum(axis=1)
 df['NEW_DOC_KURT'] = df[docs].kurtosis(axis=1)
 df['AGE_RANGE'] = df['DAYS_BIRTH'].apply(lambda x: get_age_group(x))<feature_engineering>
learning_history = model.fit(X_train, y_train,
batch_size = 1024, epochs = 40, verbose = 2,
validation_data=(X_val, y_val)) ;
df['EXT_SOURCES_PROD'] = df['EXT_SOURCE_1'] * df['EXT_SOURCE_2'] * df['EXT_SOURCE_3']
df['EXT_SOURCES_WEIGHTED'] = df.EXT_SOURCE_1 * 2 + df.EXT_SOURCE_2 * 1 + df.EXT_SOURCE_3 * 3
np.warnings.filterwarnings('ignore', r'All-NaN(slice|axis)encountered')
 for function_name in ['min', 'max', 'mean', 'nanmedian', 'var']:
feature_name = 'EXT_SOURCES_{}'.format(function_name.upper())
 df[feature_name] = eval('np.{}'.format(function_name))(
 df[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']], axis=1 )<feature_engineering>
score = model.evaluate(X_val, y_val, verbose = 0)
 print('Test loss: {}%'.format(score[0] * 100))
print('Test accuracy: {}%'.format(score[1] * 100))
print("MLP Error: %.2f%%" %(100 - score[1] * 100))
df['CREDIT_TO_ANNUITY_RATIO'] = df['AMT_CREDIT'] / df['AMT_ANNUITY']
df['CREDIT_TO_GOODS_RATIO'] = df['AMT_CREDIT'] / df['AMT_GOODS_PRICE']
df['ANNUITY_TO_INCOME_RATIO'] = df['AMT_ANNUITY'] / df['AMT_INCOME_TOTAL']
df['CREDIT_TO_INCOME_RATIO'] = df['AMT_CREDIT'] / df['AMT_INCOME_TOTAL']
df['INCOME_TO_EMPLOYED_RATIO'] = df['AMT_INCOME_TOTAL'] / df['DAYS_EMPLOYED']
df['INCOME_TO_BIRTH_RATIO'] = df['AMT_INCOME_TOTAL'] / df['DAYS_BIRTH']
df['EMPLOYED_TO_BIRTH_RATIO'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['ID_TO_BIRTH_RATIO'] = df['DAYS_ID_PUBLISH'] / df['DAYS_BIRTH']
df['CAR_TO_BIRTH_RATIO'] = df['OWN_CAR_AGE'] / df['DAYS_BIRTH']
df['CAR_TO_EMPLOYED_RATIO'] = df['OWN_CAR_AGE'] / df['DAYS_EMPLOYED']
df['PHONE_TO_BIRTH_RATIO'] = df['DAYS_LAST_PHONE_CHANGE'] / df['DAYS_BIRTH']<merge>
def get_triplecnn() :
return Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same', input_shape = input_shape),
Conv2D(32, kernel_size=(3, 3), activation='relu'),
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(64, kernel_size=(3, 3), activation='relu'),
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(128, kernel_size=(3, 3), activation='relu'),
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Flatten() ,
Dense(256, activation='relu'),
Dropout(0.5),
Dense(num_classes, activation = "softmax")
 ] )
def do_mean(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].mean().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df<merge>
learning_history = model.fit(X_train, y_train,
batch_size = 128,
epochs = 50,
verbose = 1,
validation_data =(X_val, y_val))
def do_median(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].median().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df<merge>
score = model.evaluate(X_val, y_val, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1])
 print("CNN Error: %.2f%%" %(100-score[1]*100))
def do_std(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].std().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df<merge>
y_pred = model.predict(X_val )
def do_sum(df, group_cols, counted, agg_name):
gp = df[group_cols + [counted]].groupby(group_cols)[counted].sum().reset_index().rename(
 columns={counted: agg_name})
 df = df.merge(gp, on=group_cols, how='left')
 del gp
gc.collect()
return df<categorify>
train_aug = ImageDataGenerator(
 featurewise_center = False,
samplewise_center = False,
featurewise_std_normalization = False,
samplewise_std_normalization = False,
zca_whitening = False,
horizontal_flip = False,
vertical_flip = False,
fill_mode = 'nearest',
rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1)
 train_aug.fit(X_train)
 train_gen = train_aug.flow(X_train, y_train, batch_size=64 )
group = ['ORGANIZATION_TYPE', 'NAME_EDUCATION_TYPE', 'OCCUPATION_TYPE', 'AGE_RANGE', 'CODE_GENDER']
df = do_median(df, group, 'EXT_SOURCES_MEAN', 'GROUP_EXT_SOURCES_MEDIAN')
 df = do_std(df, group, 'EXT_SOURCES_MEAN', 'GROUP_EXT_SOURCES_STD')
 df = do_mean(df, group, 'AMT_INCOME_TOTAL', 'GROUP_INCOME_MEAN')
 df = do_std(df, group, 'AMT_INCOME_TOTAL', 'GROUP_INCOME_STD')
 df = do_mean(df, group, 'CREDIT_TO_ANNUITY_RATIO', 'GROUP_CREDIT_TO_ANNUITY_MEAN')
 df = do_std(df, group, 'CREDIT_TO_ANNUITY_RATIO', 'GROUP_CREDIT_TO_ANNUITY_STD')
 df = do_mean(df, group, 'AMT_CREDIT', 'GROUP_CREDIT_MEAN')
 df = do_mean(df, group, 'AMT_ANNUITY', 'GROUP_ANNUITY_MEAN')
 df = do_std(df, group, 'AMT_ANNUITY', 'GROUP_ANNUITY_STD' )<categorify>
def get_newtriplecnn() :
return Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same', input_shape = input_shape),
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Flatten() ,
Dense(512, activation='relu'),
BatchNormalization() ,
Dropout(0.5),
Dense(256, activation='relu'),
BatchNormalization() ,
Dropout(0.4),
Dense(64, activation='relu'),
BatchNormalization() ,
Dropout(0.3),
Dense(num_classes, activation = "softmax")
 ] )
def label_encoder(df, categorical_columns=None):
if not categorical_columns:
categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
for col in categorical_columns:
df[col], uniques = pd.factorize(df[col])
 return df, categorical_columns<drop_column>
callbacks1 = [
EarlyStopping(monitor = 'loss', patience = 6),
ReduceLROnPlateau(monitor = 'loss', patience = 3),
ModelCheckpoint('.. /working/model.best.hdf5', save_best_only=True)
 ]
def drop_application_columns(df):
drop_list = [
'CNT_CHILDREN', 'CNT_FAM_MEMBERS', 'HOUR_APPR_PROCESS_START',
'FLAG_EMP_PHONE', 'FLAG_MOBIL', 'FLAG_CONT_MOBILE', 'FLAG_EMAIL', 'FLAG_PHONE',
'FLAG_OWN_REALTY', 'REG_REGION_NOT_LIVE_REGION', 'REG_REGION_NOT_WORK_REGION',
'REG_CITY_NOT_WORK_CITY', 'OBS_30_CNT_SOCIAL_CIRCLE', 'OBS_60_CNT_SOCIAL_CIRCLE',
'AMT_REQ_CREDIT_BUREAU_DAY', 'AMT_REQ_CREDIT_BUREAU_MON', 'AMT_REQ_CREDIT_BUREAU_YEAR',
'COMMONAREA_MODE', 'NONLIVINGAREA_MODE', 'ELEVATORS_MODE', 'NONLIVINGAREA_AVG',
'FLOORSMIN_MEDI', 'LANDAREA_MODE', 'NONLIVINGAREA_MEDI', 'LIVINGAPARTMENTS_MODE',
'FLOORSMIN_AVG', 'LANDAREA_AVG', 'FLOORSMIN_MODE', 'LANDAREA_MEDI',
'COMMONAREA_MEDI', 'YEARS_BUILD_AVG', 'COMMONAREA_AVG', 'BASEMENTAREA_AVG',
'BASEMENTAREA_MODE', 'NONLIVINGAPARTMENTS_MEDI', 'BASEMENTAREA_MEDI',
'LIVINGAPARTMENTS_AVG', 'ELEVATORS_AVG', 'YEARS_BUILD_MEDI', 'ENTRANCES_MODE',
'NONLIVINGAPARTMENTS_MODE', 'LIVINGAREA_MODE', 'LIVINGAPARTMENTS_MEDI',
'YEARS_BUILD_MODE', 'YEARS_BEGINEXPLUATATION_AVG', 'ELEVATORS_MEDI', 'LIVINGAREA_MEDI',
'YEARS_BEGINEXPLUATATION_MODE', 'NONLIVINGAPARTMENTS_AVG', 'HOUSETYPE_MODE',
'FONDKAPREMONT_MODE', 'EMERGENCYSTATE_MODE'
]
for doc_num in [2,4,5,6,7,9,10,11,12,13,14,15,16,17,19,20,21]:
drop_list.append('FLAG_DOCUMENT_{}'.format(doc_num))
df.drop(drop_list, axis=1, inplace=True)
 return df<categorify>
history = model.fit_generator(( train_gen), epochs = 100,
steps_per_epoch = X_train.shape[0] // 64,
validation_data =(X_val, y_val),
callbacks = callbacks1,
)
df, le_encoded_cols = label_encoder(df, None)
 df = drop_application_columns(df )<categorify>
model = load_model('.. /working/model.best.hdf5' )
df = pd.get_dummies(df )<load_from_csv>
score = model.evaluate(X_val, y_val, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1])
 print("CNN Error: %.2f%%" %(100-score[1]*100))
bureau = pd.read_csv(os.path.join(DATA_DIRECTORY, 'bureau.csv'))<feature_engineering>
output = model.predict(X_test)
 output = np.argmax(output, axis = 1)
 output = pd.Series(output, name="Label")
 submission = pd.concat([pd.Series(range(1,28001), name = "ImageId"), output], axis = 1)
 submission.to_csv("submission.csv", index=False )
bureau['CREDIT_DURATION'] = -bureau['DAYS_CREDIT'] + bureau['DAYS_CREDIT_ENDDATE']
bureau['ENDDATE_DIF'] = bureau['DAYS_CREDIT_ENDDATE'] - bureau['DAYS_ENDDATE_FACT']
bureau['DEBT_PERCENTAGE'] = bureau['AMT_CREDIT_SUM'] / bureau['AMT_CREDIT_SUM_DEBT']
bureau['DEBT_CREDIT_DIFF'] = bureau['AMT_CREDIT_SUM'] - bureau['AMT_CREDIT_SUM_DEBT']
bureau['CREDIT_TO_ANNUITY_RATIO'] = bureau['AMT_CREDIT_SUM'] / bureau['AMT_ANNUITY']<categorify>
def load_data(path):
with np.load(path)as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
return(x_train, y_train),(x_test, y_test)
(x_train1, y_train1),(x_test1, y_test1)= load_data('.. /input/mnist-numpy/mnist.npz' )
def one_hot_encoder(df, categorical_columns=None, nan_as_category=True):
original_columns = list(df.columns)
 if not categorical_columns:
categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
df = pd.get_dummies(df, columns=categorical_columns, dummy_na=nan_as_category)
 categorical_columns = [c for c in df.columns if c not in original_columns]
return df, categorical_columns<groupby>
x_train1 = x_train1 / 255
x_test1 = x_test1 / 255
x_train1 = x_train1.reshape(-1, 28, 28, 1)
 x_test1 = x_test1.reshape(-1, 28, 28, 1)
 y_train1 = y_train1.reshape(y_train1.shape[0], 1)
 y_test1 = y_test1.reshape(y_test1.shape[0], 1 )
def group(df_to_agg, prefix, aggregations, aggregate_by= 'SK_ID_CURR'):
agg_df = df_to_agg.groupby(aggregate_by ).agg(aggregations)
 agg_df.columns = pd.Index(['{}{}_{}'.format(prefix, e[0], e[1].upper())
 for e in agg_df.columns.tolist() ])
 return agg_df.reset_index()<merge>
Add_X = np.vstack(( x_train1, x_test1))
Add_y = np.vstack(( y_train1, y_test1))
Add_y = to_categorical(Add_y )
def group_and_merge(df_to_agg, df_to_merge, prefix, aggregations, aggregate_by= 'SK_ID_CURR'):
agg_df = group(df_to_agg, prefix, aggregations, aggregate_by= aggregate_by)
 return df_to_merge.merge(agg_df, how='left', on= aggregate_by )<load_from_csv>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 X_train = train.drop(labels = ["label"], axis = 1)
 y_train = train['label']
y_train = to_categorical(y_train)
 X_train /= 255
X_train = X_train.values.reshape(-1, 28, 28, 1 )
def get_bureau_balance(path, num_rows= None):
bb = pd.read_csv(os.path.join(path, 'bureau_balance.csv'))
bb, categorical_cols = one_hot_encoder(bb, nan_as_category= False)
 bb_processed = bb.groupby('SK_ID_BUREAU')[categorical_cols].mean().reset_index()
agg = {'MONTHS_BALANCE': ['min', 'max', 'mean', 'size']}
bb_processed = group_and_merge(bb, bb_processed, '', agg, 'SK_ID_BUREAU')
 del bb; gc.collect()
return bb_processed<categorify>
add_train_aug = ImageDataGenerator(
 featurewise_center = False,
samplewise_center = False,
featurewise_std_normalization = False,
samplewise_std_normalization = False,
zca_whitening = False,
horizontal_flip = False,
vertical_flip = False,
fill_mode = 'nearest',
rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1)
 add_train_aug.fit(Add_X)
 add_train_gen = add_train_aug.flow(Add_X, Add_y, batch_size=64 )
bureau, categorical_cols = one_hot_encoder(bureau, nan_as_category= False)
 bureau = bureau.merge(get_bureau_balance(DATA_DIRECTORY), how='left', on='SK_ID_BUREAU')
 bureau['STATUS_12345'] = 0
for i in range(1,6):
bureau['STATUS_12345'] += bureau['STATUS_{}'.format(i)]<merge>
add_callbacks = [
EarlyStopping(monitor = 'loss', patience = 6),
ReduceLROnPlateau(monitor = 'loss', patience = 3),
ModelCheckpoint('.. /working/additional_model.best.hdf5', save_best_only=True)
 ]
features = ['AMT_CREDIT_MAX_OVERDUE', 'AMT_CREDIT_SUM_OVERDUE', 'AMT_CREDIT_SUM',
'AMT_CREDIT_SUM_DEBT', 'DEBT_PERCENTAGE', 'DEBT_CREDIT_DIFF', 'STATUS_0', 'STATUS_12345']
agg_length = bureau.groupby('MONTHS_BALANCE_SIZE')[features].mean().reset_index()
agg_length.rename({feat: 'LL_' + feat for feat in features}, axis=1, inplace=True)
 bureau = bureau.merge(agg_length, how='left', on='MONTHS_BALANCE_SIZE')
 del agg_length; gc.collect()<define_variables>
def get_addcnn() :
return Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same', input_shape = input_shape),
Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'),
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same'),
BatchNormalization() ,
MaxPool2D(pool_size=(2, 2)) ,
Dropout(0.25),
Flatten() ,
Dense(512, activation='relu'),
BatchNormalization() ,
Dropout(0.5),
Dense(256, activation='relu'),
BatchNormalization() ,
Dropout(0.4),
Dense(64, activation='relu'),
BatchNormalization() ,
Dropout(0.3),
Dense(num_classes, activation = "softmax")
 ] )
BUREAU_AGG = {
'SK_ID_BUREAU': ['nunique'],
'DAYS_CREDIT': ['min', 'max', 'mean'],
'DAYS_CREDIT_ENDDATE': ['min', 'max'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['max', 'mean', 'sum'],
'AMT_ANNUITY': ['mean'],
'DEBT_CREDIT_DIFF': ['mean', 'sum'],
'MONTHS_BALANCE_MEAN': ['mean', 'var'],
'MONTHS_BALANCE_SIZE': ['mean', 'sum'],
'STATUS_0': ['mean'],
'STATUS_1': ['mean'],
'STATUS_12345': ['mean'],
'STATUS_C': ['mean'],
'STATUS_X': ['mean'],
'CREDIT_ACTIVE_Active': ['mean'],
'CREDIT_ACTIVE_Closed': ['mean'],
'CREDIT_ACTIVE_Sold': ['mean'],
'CREDIT_TYPE_Consumer credit': ['mean'],
'CREDIT_TYPE_Credit card': ['mean'],
'CREDIT_TYPE_Car loan': ['mean'],
'CREDIT_TYPE_Mortgage': ['mean'],
'CREDIT_TYPE_Microloan': ['mean'],
'LL_AMT_CREDIT_SUM_OVERDUE': ['mean'],
'LL_DEBT_CREDIT_DIFF': ['mean'],
'LL_STATUS_12345': ['mean'],
}
BUREAU_ACTIVE_AGG = {
'DAYS_CREDIT': ['max', 'mean'],
'DAYS_CREDIT_ENDDATE': ['min', 'max'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM': ['max', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['max', 'mean'],
'DAYS_CREDIT_UPDATE': ['min', 'mean'],
'DEBT_PERCENTAGE': ['mean'],
'DEBT_CREDIT_DIFF': ['mean'],
'CREDIT_TO_ANNUITY_RATIO': ['mean'],
'MONTHS_BALANCE_MEAN': ['mean', 'var'],
'MONTHS_BALANCE_SIZE': ['mean', 'sum'],
}
BUREAU_CLOSED_AGG = {
'DAYS_CREDIT': ['max', 'var'],
'DAYS_CREDIT_ENDDATE': ['max'],
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'sum'],
'DAYS_CREDIT_UPDATE': ['max'],
'ENDDATE_DIF': ['mean'],
'STATUS_12345': ['mean'],
}
BUREAU_LOAN_TYPE_AGG = {
'DAYS_CREDIT': ['mean', 'max'],
'AMT_CREDIT_MAX_OVERDUE': ['mean', 'max'],
'AMT_CREDIT_SUM': ['mean', 'max'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'max'],
'DEBT_PERCENTAGE': ['mean'],
'DEBT_CREDIT_DIFF': ['mean'],
'DAYS_CREDIT_ENDDATE': ['max'],
}
BUREAU_TIME_AGG = {
'AMT_CREDIT_MAX_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM': ['max', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['mean', 'sum'],
'DEBT_PERCENTAGE': ['mean'],
'DEBT_CREDIT_DIFF': ['mean'],
'STATUS_0': ['mean'],
'STATUS_12345': ['mean'],
}<merge>
model = get_addcnn()
model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
 model.summary()
agg_bureau = group(bureau, 'BUREAU_', BUREAU_AGG)
 active = bureau[bureau['CREDIT_ACTIVE_Active'] == 1]
agg_bureau = group_and_merge(active,agg_bureau,'BUREAU_ACTIVE_',BUREAU_ACTIVE_AGG)
 closed = bureau[bureau['CREDIT_ACTIVE_Closed'] == 1]
agg_bureau = group_and_merge(closed,agg_bureau,'BUREAU_CLOSED_',BUREAU_CLOSED_AGG)
 del active, closed; gc.collect()
for credit_type in ['Consumer credit', 'Credit card', 'Mortgage', 'Car loan', 'Microloan']:
type_df = bureau[bureau['CREDIT_TYPE_' + credit_type] == 1]
prefix = 'BUREAU_' + credit_type.split(' ')[0].upper() + '_'
agg_bureau = group_and_merge(type_df, agg_bureau, prefix, BUREAU_LOAN_TYPE_AGG)
 del type_df; gc.collect()
for time_frame in [6, 12]:
prefix = "BUREAU_LAST{}M_".format(time_frame)
 time_frame_df = bureau[bureau['DAYS_CREDIT'] >= -30*time_frame]
agg_bureau = group_and_merge(time_frame_df, agg_bureau, prefix, BUREAU_TIME_AGG)
 del time_frame_df; gc.collect()<merge>
history = model.fit_generator(( add_train_gen), epochs = 100,
steps_per_epoch = x_train1.shape[0] // 64,
validation_data =(X_val, y_val),
callbacks = add_callbacks,
)
sort_bureau = bureau.sort_values(by=['DAYS_CREDIT'])
 gr = sort_bureau.groupby('SK_ID_CURR')['AMT_CREDIT_MAX_OVERDUE'].last().reset_index()
gr.rename({'AMT_CREDIT_MAX_OVERDUE': 'BUREAU_LAST_LOAN_MAX_OVERDUE'}, inplace=True)
 agg_bureau = agg_bureau.merge(gr, on='SK_ID_CURR', how='left')
 agg_bureau['BUREAU_DEBT_OVER_CREDIT'] = \
agg_bureau['BUREAU_AMT_CREDIT_SUM_DEBT_SUM']/agg_bureau['BUREAU_AMT_CREDIT_SUM_SUM']
agg_bureau['BUREAU_ACTIVE_DEBT_OVER_CREDIT'] = \
agg_bureau['BUREAU_ACTIVE_AMT_CREDIT_SUM_DEBT_SUM']/agg_bureau['BUREAU_ACTIVE_AMT_CREDIT_SUM_SUM']<merge>
model = load_model('additional_model.best.hdf5' )
df = pd.merge(df, agg_bureau, on='SK_ID_CURR', how='left')
 del agg_bureau, bureau
gc.collect()<load_from_csv>
score = model.evaluate(X_val, y_val, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1])
 print("CNN Error: %.2f%%" %(100-score[1]*100))
<define_variables><EOS> 
output = model.predict(X_test)
 output = np.argmax(output, axis = 1)
 output = pd.Series(output, name="Label")
 submission = pd.concat([pd.Series(range(1,28001), name = "ImageId"), output], axis = 1)
 submission.to_csv("bonus_submission.csv", index=False )