Kaggle competition: Home Credit Default Risk
X_train1 = feature_union1.fit_transform(X_train, y_train)
 X_test1 = feature_union1.transform(X_test )<categorify>
PATH = ".. /input"
list_of_files = os.listdir(PATH)
 application_train = pd.read_csv(PATH+"/application_train.csv")
 application_test = pd.read_csv(PATH+"/application_test.csv")
 bureau = pd.read_csv(PATH+"/bureau.csv")
 bureau_balance = pd.read_csv(PATH+"/bureau_balance.csv")
 credit_card_balance = pd.read_csv(PATH+"/credit_card_balance.csv")
 installments_payments = pd.read_csv(PATH+"/installments_payments.csv")
 previous_application = pd.read_csv(PATH+"/previous_application.csv")
 POS_CASH_balance = pd.read_csv(PATH+"/POS_CASH_balance.csv" )
oe = ce.OrdinalEncoder(cols=object_cols )<normalization>
total_IDS = np.concatenate(( application_test["SK_ID_CURR"].values, application_train["SK_ID_CURR"].values))
print(len(np.unique(np.array(total_IDS)))== len(total_IDS))
X_train = oe.fit_transform(X_train, y_train)
 X_test = oe.transform(X_test )<concatenate>
POS_CASH_balance_IDS = POS_CASH_balance["SK_ID_CURR"].values
bureau_IDS = bureau["SK_ID_CURR"].values
credit_card_balance_IDS = credit_card_balance["SK_ID_CURR"].values
installments_payments_IDS = installments_payments["SK_ID_CURR"].values
previous_application_IDS = previous_application["SK_ID_CURR"].values
tot = len(total_IDS)
 print(tot)
 print(len(np.intersect1d(POS_CASH_balance_IDS, total_IDS)) /tot*100,
len(np.intersect1d(bureau_IDS, total_IDS)) /tot*100,
len(np.intersect1d(credit_card_balance_IDS, total_IDS)) /tot*100,
len(np.intersect1d(installments_payments_IDS, total_IDS)) /tot*100,
len(np.intersect1d(previous_application_IDS, total_IDS)) /tot*100 )
X_train = np.hstack([X_train, X_train1])
 X_test = np.hstack([X_test, X_test1] )<choose_model_class>
prev = previous_application["SK_ID_PREV"].values
POS_CASH_balance_IDS_prev = POS_CASH_balance["SK_ID_PREV"].values
credit_card_balance_IDS_prev = credit_card_balance["SK_ID_PREV"].values
installments_payments_IDS_prev = installments_payments["SK_ID_PREV"].values
prev_num = len(prev)
 print(prev_num)
 print(len(np.intersect1d(POS_CASH_balance_IDS_prev, prev)) /prev_num*100,
len(np.intersect1d(credit_card_balance_IDS_prev, prev)) /prev_num*100,
len(np.intersect1d(installments_payments_IDS_prev, prev)) /prev_num*100)
models = []
seeds = [114]
for seed in seeds:
params = {
"objective": "regression",
"learning_rate":.02,
"tree_learner": "data",
"device_type": "cpu",
"num_leaves": 128,
"seed": seed,
"colsample_bytree":.8,
"max_depth": 7,
"subsample":.9,
"metric": ["rmse"]
}
skf = KFold(n_splits=5, random_state=seed, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(skf.split(X_train, y_train)) :
X_train_, y_train_ = X_train[train_ix,], y_train.values[train_ix]
X_val_, y_val_ = X_train[test_ix,], y_train.values[test_ix]
train_data = lgbm.Dataset(
 data=X_train_,
label=y_train_,
feature_name=feature_names 
)
 val_data = lgbm.Dataset(
 data=X_val_,
label=y_val_,
reference=train_data,
feature_name=feature_names 
)
 model = lgbm.train(params=params, train_set=train_data, num_boost_round=9999, valid_sets=[val_data],
early_stopping_rounds=200, verbose_eval=100)
 models.append(model)
 y_pred_ = model.predict(X_val_)
 score = mean_squared_error(y_val_, y_pred_)
 print('CV Score of Fold_%d is %f' %(i, score))<load_pretrained>
bureau_br = np.unique(bureau["SK_ID_BUREAU"].values)
 print(len(np.intersect1d(np.unique(bureau_balance["SK_ID_BUREAU"].values), bureau_br)) /len(bureau_br)*100 )
joblib.dump(models, model_dir / f"models-lgbm-{VERSION}.joblib" )<predict_on_test>
breau_total = np.unique(np.intersect1d(bureau_IDS, total_IDS))
bureau_filtered = bureau.loc[bureau["SK_ID_CURR"].isin(breau_total)]
b = np.intersect1d(np.unique(bureau_filtered["SK_ID_BUREAU"].values), np.unique(bureau_balance["SK_ID_BUREAU"].values))
bureau_filtered = bureau_filtered.loc[bureau_filtered["SK_ID_BUREAU"].isin(b)]
len(bureau_filtered["SK_ID_CURR"].values)
 bureau_filtered
for i, model in enumerate(models):
if i == 0:
y_preds = model.predict(X_test)
 else:
y_preds = np.vstack(( y_preds, model.predict(X_test)) )<prepare_output>
print(len(np.unique(bureau_filtered["SK_ID_CURR"].values)) /tot*100 )
y_preds = np.expm1(y_preds )<prepare_output>
train = application_train.drop(["TARGET"], axis = 1)
 train_target = application_train["TARGET"]
test= application_test.copy()
tr = len(application_train)
 print(all(i ==True for i in train.columns==test.columns))
y_pred = y_preds.mean(axis=0 )<save_to_csv>
df = pd.concat([train, test])
 del train, test, application_train, application_test
gc.collect()
def categorical_features(data):
features = [i for i in list(data.columns)if data[i].dtype == 'object']
return features
categorical = categorical_features(df)
 numerical = [i for i in df.columns if i not in categorical]
numerical.remove("SK_ID_CURR")
 IDs = df["SK_ID_CURR"]
submission = pd.read_csv('.. /input/exam-for-students20200129/sample_submission.csv', index_col=0)
 submission.ConvertedSalary = y_pred
submission.to_csv(model_dir / f'submission-{VERSION}.csv' )<feature_engineering>
for feature in categorical:
df[feature].fillna("unidentified")
 print(f'Transforming {feature}...')
 encoder = LabelEncoder()
encoder.fit(df[feature].astype(str))
df[feature] = encoder.transform(df[feature].astype(str))
df.head()
feature_importances["mean"] = feature_importances.mean(axis=1 )<set_options>
for feats in df.columns:
df[feats] = df[feats].fillna(-1)
 df.head()
plt.style.use('ggplot')
 %matplotlib inline 
<set_options>
POS_CASH_balance_G1 = POS_CASH_balance.loc[POS_CASH_balance["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(POS_CASH_balance_G1["SK_ID_CURR"].values)))
 POS_CASH_balance_G1.head()
pd.set_option('display.max_columns', 500 )<load_from_csv>
np.unique(POS_CASH_balance_G1["NAME_CONTRACT_STATUS"].values)
 POS_CASH_balance_G1_num =(POS_CASH_balance_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = POS_CASH_balance_G1[["SK_ID_CURR", "NAME_CONTRACT_STATUS"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_POS_CASH"] = nb["NAME_CONTRACT_STATUS"]
df = df.merge(POS_CASH_balance_G1_num.drop("SK_ID_PREV", axis = 1), on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NAME_CONTRACT_STATUS", axis = 1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, POS_CASH_balance_G1_num, POS_CASH_balance_G1
gc.collect()
df.head()
df_train = pd.read_csv('.. /input/exam-for-students20200129/train.csv', index_col=0)
 df_test = pd.read_csv('.. /input/exam-for-students20200129/test.csv', index_col=0 )<count_missing_values>
bureau_G1 = bureau.drop(["SK_ID_BUREAU"], axis = 1 ).loc[bureau["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(bureau_G1["SK_ID_CURR"].values)))
 bureau_G1.head()
df_train.isnull().sum()<count_missing_values>
bureau_G1_num =(bureau_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = bureau_G1[["SK_ID_CURR", "CREDIT_ACTIVE"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_bureau"] = nb["CREDIT_ACTIVE"]
df = df.merge(bureau_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("CREDIT_ACTIVE", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, bureau_G1_num, bureau_G1
gc.collect()
df.head()
df_test.isnull().sum()<count_values>
credit_card_balance_G1 = credit_card_balance.drop(["SK_ID_PREV"], axis = 1 ).loc[credit_card_balance["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(credit_card_balance_G1["SK_ID_CURR"].values)))
 credit_card_balance_G1.head()
for col_name in df_train.columns:
print(df_train[col_name].value_counts())
 print(df_train[col_name].value_counts() / len(df_train[col_name]))<count_values>
credit_card_balance_G1_num =(credit_card_balance_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = credit_card_balance_G1[["SK_ID_CURR", "NAME_CONTRACT_STATUS"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_credit_card"] = nb["NAME_CONTRACT_STATUS"]
df = df.merge(credit_card_balance_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NAME_CONTRACT_STATUS", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, credit_card_balance_G1_num, credit_card_balance_G1
gc.collect()
df.head()
for col_name in df_test.columns:
print(df_test[col_name].value_counts())
 print(df_test[col_name].value_counts() / len(df_test[col_name]))<load_from_csv>
installments_payments_G1 = installments_payments.drop(["SK_ID_PREV"], axis = 1 ).loc[installments_payments["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(installments_payments_G1["SK_ID_CURR"].values)))
 installments_payments_G1.head()
df_country_info = pd.read_csv('.. /input/exam-for-students20200129/country_info.csv' )<count_values>
installments_payments_G1_num =(installments_payments_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = installments_payments_G1[["SK_ID_CURR", "NUM_INSTALMENT_VERSION"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_install_pay"] = nb["NUM_INSTALMENT_VERSION"]
df = df.merge(installments_payments_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NUM_INSTALMENT_VERSION", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, installments_payments_G1_num, installments_payments_G1
gc.collect()
df.head()
df_country_info['Country'].value_counts()<create_dataframe>
previous_application_G1 = previous_application.drop(["SK_ID_PREV"], axis = 1 ).loc[previous_application["SK_ID_CURR"].isin(total_IDS)]
print(len(np.unique(previous_application_G1["SK_ID_CURR"].values)))
 previous_application_G1.head()
df_country_info_edit = df_country_info[['Country', 'Region']].copy()
df_country_info_edit<merge>
previous_application_G1_num =(previous_application_G1.groupby("SK_ID_CURR", as_index=False ).mean())
 nb = previous_application_G1[["SK_ID_CURR", "NAME_CONTRACT_TYPE"]].groupby("SK_ID_CURR", as_index = False ).count()
nb["num_in_previous_app"] = nb["NAME_CONTRACT_TYPE"]
df = df.merge(previous_application_G1_num, on='SK_ID_CURR', how='left' ).fillna(-1)
 df = df.merge(nb.drop("NAME_CONTRACT_TYPE", axis=1), on='SK_ID_CURR', how='left' ).fillna(-1)
 del nb, previous_application_G1_num, previous_application_G1
gc.collect()
df.head()
df_train_add_countryinfo = pd.merge(df_train, df_country_info_edit, how = 'left', on = ['Country'] ).copy()
df_test_add_countryinfo = pd.merge(df_test, df_country_info_edit, how = 'left', on = ['Country'] ).copy()<prepare_x_and_y>
train_X = df[:tr].drop("SK_ID_CURR", axis = 1)
 test_X = df[tr:].drop("SK_ID_CURR", axis = 1)
 y = train_target
x_train, x_val, y_train, y_val = train_test_split(train_X, y, test_size=0.2, random_state=18)
y_train = df_train_add_countryinfo['ConvertedSalary'].copy()
X_train = df_train_add_countryinfo.drop(['ConvertedSalary', 'Country'], axis=1 ).copy()
X_test = df_test_add_countryinfo.drop(['Country'], axis=1 ).copy()<count_unique_values>
lgt = lgb.Dataset(data=x_train, label=y_train)
 lgv = lgb.Dataset(data=x_val, label=y_val)
 params = {'task': 'train', 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'auc',
'learning_rate': 0.01, 'num_leaves': 48, 'num_iteration': 5000, 'verbose': 0 ,
'colsample_bytree':.8, 'subsample':.9, 'max_depth':7, 'reg_alpha':.1, 'reg_lambda':.1,
'min_split_gain':.01, 'min_child_weight':1}
model = lgb.train(params, lgt, valid_sets=lgv, early_stopping_rounds=150, verbose_eval=200 )
<categorify><EOS> 
preds = model.predict(test_X)
 submission = pd.read_csv(".. /input/sample_submission.csv")
 submission['TARGET'] = preds
submission.to_csv("baseline.csv", index=False)
 submission.head()