Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<feature_engineering>
MainDir = ".. /input/.. /input/home-credit-default-risk"
test = pd.read_csv(f'{MainDir}/application_test.csv' )
model.get_feature_importance(prettified=True )<init_hyperparams>
preprocessor = joblib.load('.. /input/defaultdata08/default_preprocessor_08.joblib')
 model = joblib.load('.. /input/defaultdata08/default_model_08.joblib')
 print(type(model))
params = {}
params['loss_function'] = 'Logloss'
params['iterations'] = 93
params['custom_loss'] = 'AUC'
params['random_seed'] = 63
params['learning_rate'] = 0.5
cv_data = cv(
 params = params,
pool = Pool(X, label=y, cat_features=cat_features),
fold_count=5,
shuffle=True,
partition_random_seed=0,
stratified=False,
verbose=False
)<train_model>
bureau = pd.read_csv(f'{MainDir}/bureau.csv')
 print(bureau.shape, "- shape of bureau table")
 bureau_balance = pd.read_csv(f'{MainDir}/bureau_balance.csv')
 bb_status = pd.crosstab(bureau_balance.SK_ID_BUREAU, bureau_balance.STATUS)
 bb_status.columns = ['BB_'+column for column in bb_status.columns]
bureau = bureau.merge(bb_status, left_on = 'SK_ID_BUREAU', right_on = 'SK_ID_BUREAU')
 bureau = bureau.drop(['SK_ID_BUREAU'], axis = 1)
 print(bureau.shape, "- shape of bureau table after merge")
 bureau.columns = ['BU_'+column if column !='SK_ID_CURR' else column for column in bureau.columns]
bureau_num = bureau.groupby(by=['SK_ID_CURR'] ).mean().reset_index()
print(bureau_num.shape, "- shape of numeric features(incl index)")
 bureau_cat = pd.get_dummies(bureau.select_dtypes('object'))
bureau_cat['SK_ID_CURR'] = bureau['SK_ID_CURR']
bureau_cat = bureau_cat.groupby(by = ['SK_ID_CURR'] ).mean().reset_index()
print(bureau_cat.shape, "- shape of categorical features(incl index)")
 bureau_count = bureau.groupby(by = ['SK_ID_CURR'])['BU_CREDIT_ACTIVE'].count().reset_index()
bureau_count.rename(columns={'BU_CREDIT_ACTIVE':'COUNT_of_BUREAU'})
 bureau_count.head(5)
 test = test.merge(bureau_num, on='SK_ID_CURR', how='left')
 test = test.merge(bureau_cat, on='SK_ID_CURR', how='left')
 test = test.merge(bureau_count, on='SK_ID_CURR', how='left')
 print(test.shape, "- shape of training data after merges")
 list = ['bureau', 'bureau_num', 'bureau_cat', 'bureau_balance']
del list
gc.collect()
previous = pd.read_csv(f'{MainDir}/previous_application.csv')
 print(previous.shape, "- shape of previous_application")
 pos = pd.read_csv(f'{MainDir}/POS_CASH_balance.csv')
 pos.columns = ['PO_'+column if column !='SK_ID_PREV' else column for column in pos.columns]
pos_num = pos.groupby(by=['SK_ID_PREV'] ).mean().reset_index()
print(pos_num.shape, "- shape of numeric features(incl index)")
 pos_cat = pd.get_dummies(pos.select_dtypes('object'))
pos_cat['SK_ID_PREV'] = pos['SK_ID_PREV']
pos_cat = pos_cat.groupby(by = ['SK_ID_PREV'] ).mean().reset_index()
print(pos_cat.shape, "- shape of categorical features(incl index)")
 previous = previous.merge(pos_num, on='SK_ID_PREV', how='left')
 previous = previous.merge(pos_cat, on='SK_ID_PREV', how='left')
 print(previous.shape, "- shape of previous data after merges")
 list = ['pos', 'pos_num', 'pos_cat']
del list
gc.collect()
inst = pd.read_csv(f'{MainDir}/installments_payments.csv')
 inst.columns = ['IP_'+column if column !='SK_ID_PREV' else column for column in inst.columns]
inst_num = inst.groupby(by=['SK_ID_PREV'] ).mean().reset_index()
print(inst_num.shape, "- shape of numeric features(incl index)")
 previous = previous.merge(inst_num, left_on='SK_ID_PREV', right_on = 'SK_ID_PREV', how='left')
 print(previous.shape, "- shape of previous data after merges")
 list = ['inst', 'inst_num']
del list
gc.collect()
ccb = pd.read_csv(f'{MainDir}/credit_card_balance.csv')
 ccb.columns = ['CC_'+column if column !='SK_ID_PREV' else column for column in ccb.columns]
ccb_num = ccb.groupby(by=['SK_ID_PREV'] ).mean().reset_index()
print(ccb_num.shape, "- shape of numeric features(incl index)")
 ccb_cat = pd.get_dummies(ccb.select_dtypes('object'))
ccb_cat['SK_ID_PREV'] = ccb['SK_ID_PREV']
ccb_cat = ccb_cat.groupby(by = ['SK_ID_PREV'] ).mean().reset_index()
print(ccb_cat.shape, "- shape of categorical features(incl index)")
 previous = previous.merge(ccb_num, on='SK_ID_PREV', how='left')
 previous = previous.merge(ccb_cat, on='SK_ID_PREV', how='left')
 print(previous.shape, "- shape of previous data after merges")
 list = ['ccb', 'ccb_num', 'ccb_cat']
del list
gc.collect()
previous.columns = ['PR_'+column if column !='SK_ID_CURR' else column for column in previous.columns]
previous['PR_DAYS_LAST_DUE'].replace({365243: np.nan}, inplace = True)
 previous['PR_DAYS_TERMINATION'].replace({365243: np.nan}, inplace = True)
 previous['PR_DAYS_FIRST_DRAWING'].replace({365243: np.nan}, inplace = True)
 previous_num = previous.groupby(by=['SK_ID_CURR'] ).mean().reset_index()
print(previous_num.shape, "- shape of numeric features(incl index)")
 previous_cat = pd.get_dummies(previous.select_dtypes('object'))
previous_cat['SK_ID_CURR'] = previous['SK_ID_CURR']
previous_cat = bureau_cat.groupby(by = ['SK_ID_CURR'] ).mean().reset_index()
print(previous_cat.shape, "- shape of categorical features(incl index)")
 test = test.merge(previous_num, on='SK_ID_CURR', how='left')
 test = test.merge(previous_cat, on='SK_ID_CURR', how='left')
 print(test.shape, "- shape of training data after merges")
 list = ['previous', 'previous_num', 'previous_cat']
del list
gc.collect()
test['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True)
 test['CI_ratio'] = test['AMT_CREDIT'] / test['AMT_INCOME_TOTAL']
test['AI_ratio'] = test['AMT_ANNUITY'] / test['AMT_INCOME_TOTAL']
test['AC_ratio'] = test['AMT_CREDIT'] / test['AMT_ANNUITY']
test['CG_ratio'] = test['AMT_CREDIT'] / test['AMT_GOODS_PRICE']
test['log_INCOME'] = np.log(test['AMT_INCOME_TOTAL'])
 test['log_ANNUITY'] = np.log(test['AMT_ANNUITY'])
 test['log_CREDIT'] = np.log(test['AMT_CREDIT'])
 test['log_GOODS'] = np.log(test['AMT_GOODS_PRICE'])
 test['MissingBureau'] = test.iloc[:, 41:44].isnull().sum(axis=1 ).astype("category")
 test['FLAG_CG_ratio'] = test['AMT_CREDIT'] > test['AMT_GOODS_PRICE']
test['DAYS_ID_4200'] = test['DAYS_ID_PUBLISH'] < -4200
test['AVG_EXT'] = test.iloc[:, 41:44].sum(axis=1)/(3- test.iloc[:,41:44].isnull().sum(axis=1))
test['AVG_EXT'].replace(np.nan, 0.2, inplace = True)
 test.EXT_SOURCE_1.fillna(test.AVG_EXT, inplace=True)
 test.EXT_SOURCE_2.fillna(test.AVG_EXT, inplace=True)
 test.EXT_SOURCE_3.fillna(test.AVG_EXT, inplace=True)
 test.drop(['AVG_EXT'], axis = 1)
 test.drop(['ORGANIZATION_TYPE'], axis = 1)
 test['OD_ratio'] = test['BU_AMT_CREDIT_SUM_OVERDUE'] / test['BU_AMT_CREDIT_SUM_DEBT']
test['OD_ratio'].replace([np.nan, np.inf, -np.inf], 0, inplace = True)
 test['Credit_ratio'] = test['BU_AMT_CREDIT_SUM'] / test['BU_AMT_CREDIT_SUM_LIMIT']
test['Credit_ratio'].replace([np.nan, np.inf, -np.inf], 0, inplace = True)
 test['Debt_ratio'] = test['BU_AMT_CREDIT_SUM_DEBT'] / test['BU_AMT_CREDIT_SUM']
test['Debt_ratio'].replace([np.nan, np.inf, -np.inf], 0, inplace = True)
 test['PR_term'] = test['PR_IP_AMT_PAYMENT'] / test['PR_IP_AMT_INSTALMENT']
test['PR_term'].replace([np.nan, np.inf, -np.inf], 0, inplace = True)
 X_test = preprocessor.transform(test)
 print(X_test.shape )
best_value = np.min(cv_data['test-Logloss-mean'])
 best_iter = np.argmin(cv_data['test-Logloss-mean'])
 print('Best validation Logloss score, not stratified: {:.4f}±{:.4f} on step {}'.format(
 best_value,
cv_data['test-Logloss-std'][best_iter],
best_iter)
 )<load_from_csv>
test_pred = model.predict_proba(X_test)
 print(test_pred.shape)
 print(test_pred[:5] )
<prepare_output><EOS> 
submission = pd.read_csv('.. /input/home-credit-default-risk/sample_submission.csv')
 submission.head(10)
 submission.TARGET = test_pred[:,1]
submission.head(10)
 submission.to_csv('default_submission_08.csv', index=False, header = True )