Kaggle competition: Home Credit Default Risk
plt.rcParams['figure.figsize'] =(10, 8 )<load_from_csv>
app_train = pd.read_csv(path + "application_train.csv")
 app_train.head()
train_og = pd.read_csv('/kaggle/input/equipfails/equip_failures_training_set.csv' )<load_from_csv>
bureau = pd.read_csv(path + "bureau.csv")
 bureau.head()
test_og = pd.read_csv('/kaggle/input/equipfails/equip_failures_test_set.csv' )<concatenate>
bureau_balance = pd.read_csv(path + "bureau_balance.csv")
 bureau_balance.head()
data = pd.concat([train_og, test_og], ignore_index=True, sort=False )<define_variables>
credit_card_balance = pd.read_csv(path + "credit_card_balance.csv")
 credit_card_balance.head()
data = data.replace('na', np.nan )<data_type_conversions>
pcb = pd.read_csv(path + "POS_CASH_balance.csv")
 pcb.head()
for c in data.columns:
data[c] = pd.to_numeric(data[c] )<correct_missing_values>
previous_application = pd.read_csv(path + "previous_application.csv")
 previous_application.head()
data_nonna = data.iloc[:,2:].fillna(data.mean() )<normalization>
installments_payments = pd.read_csv(path + "installments_payments.csv")
 installments_payments.head()
scaler = MinMaxScaler()<train_model>
app_test = pd.read_csv('.. /input/application_test.csv')
 app_test['is_test'] = 1
app_test['is_train'] = 0
app_train['is_test'] = 0
app_train['is_train'] = 1
Y = app_train['TARGET']
train_X = app_train.drop(['TARGET'], axis = 1)
 test_id = app_test['SK_ID_CURR']
test_X = app_test
data = pd.concat([train_X, test_X], axis=0 )
scaler.fit(data_nonna )<normalization>
def _get_categorical_features(df):
feats = [col for col in list(df.columns)if df[col].dtype == 'object']
return feats
def _factorize_categoricals(df, cats):
for col in cats:
df[col], _ = pd.factorize(df[col])
 return df
def _get_dummies(df, cats):
for col in cats:
df = pd.concat([df, pd.get_dummies(df[col], prefix=col)], axis=1)
 return df
data_cats = _get_categorical_features(data)
 prev_app_cats = _get_categorical_features(previous_application)
 bureau_cats = _get_categorical_features(bureau)
 pcb_cats = _get_categorical_features(pcb)
 ccbal_cats = _get_categorical_features(credit_card_balance)
 previous_application = _get_dummies(previous_application, prev_app_cats)
 bureau = _get_dummies(bureau, bureau_cats)
 pcb = _get_dummies(pcb, pcb_cats)
 credit_card_balance = _get_dummies(credit_card_balance, ccbal_cats)
 data = _factorize_categoricals(data, data_cats )
X = scaler.transform(data_nonna )<prepare_x_and_y>
prev_apps_count = previous_application[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
previous_application['SK_ID_PREV'] = previous_application['SK_ID_CURR'].map(prev_apps_count['SK_ID_PREV'])
 prev_apps_avg = previous_application.groupby('SK_ID_CURR' ).mean()
prev_apps_avg.columns = ['p_' + col for col in prev_apps_avg.columns]
data = data.merge(right=prev_apps_avg.reset_index() , how='left', on='SK_ID_CURR' )
y = data.target.values<define_variables>
bureau_avg = bureau.groupby('SK_ID_CURR' ).mean()
bureau_avg['buro_count'] = bureau[['SK_ID_BUREAU','SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
bureau_avg.columns = ['b_' + f_ for f_ in bureau_avg.columns]
data = data.merge(right=bureau_avg.reset_index() , how='left', on='SK_ID_CURR')
m = train_og.shape[0]<split>
cnt_inst = installments_payments[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
installments_payments['SK_ID_PREV'] = installments_payments['SK_ID_CURR'].map(cnt_inst['SK_ID_PREV'])
 avg_inst = installments_payments.groupby('SK_ID_CURR' ).mean()
avg_inst.columns = ['i_' + f_ for f_ in avg_inst.columns]
data = data.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR' )
X_train, X_valid, y_train, y_valid = train_test_split(X[:m], y[:m], test_size=0.3, random_state = 0 )<count_values>
pcb_count = pcb[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
pcb['SK_ID_PREV'] = pcb['SK_ID_CURR'].map(pcb_count['SK_ID_PREV'])
 pcb_avg = pcb.groupby('SK_ID_CURR' ).mean()
data = data.merge(right=pcb_avg.reset_index() , how='left', on='SK_ID_CURR' )
train_og.target.value_counts(normalize = True )<predict_on_test>
nb_prevs = credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
credit_card_balance['SK_ID_PREV'] = credit_card_balance['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 avg_cc_bal = credit_card_balance.groupby('SK_ID_CURR' ).mean()
avg_cc_bal.columns = ['cc_bal_' + f_ for f_ in avg_cc_bal.columns]
data = data.merge(right=avg_cc_bal.reset_index() , how='left', on='SK_ID_CURR' )
def get_f1_score(clf, X_train, X_valid, y_train, y_valid):

 y_train_preds = clf.predict(X_train)
 train_f1_score = f1_score(y_pred=y_train_preds, y_true=y_train)
 y_valid_preds = clf.predict(X_valid)
 valid_f1_score = f1_score(y_pred=y_valid_preds, y_true=y_valid)
 return train_f1_score, valid_f1_score<train_on_grid>
ignore_features = ['SK_ID_CURR', 'is_train', 'is_test']
relevant_features = [col for col in data.columns if col not in ignore_features]
trainX = data[data['is_train'] == 1][relevant_features]
testX = data[data['is_test'] == 1][relevant_features]
lrclassifier = LogisticRegression()
lrclassifier.fit(X_train, y_train )<compute_test_metric>
x_train, x_val, y_train, y_val = train_test_split(trainX, Y, test_size=0.2, random_state=18)
 lgb_train = lgb.Dataset(data=x_train, label=y_train)
 lgb_eval = lgb.Dataset(data=x_val, label=y_val )
get_f1_score(lrclassifier, X_train, X_valid, y_train, y_valid )<define_variables>
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, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=200 )
def select_sample(mult):
sample_size = mult*y_train[y_train==1].shape[0]
np.random.seed(2)
 sample_idx = np.concatenate([np.random.choice(np.where(y_train==0)[0], size = sample_size), np.where(y_train==1)[0]])
 X_train_sample = X_train[sample_idx]
y_train_sample = y_train[sample_idx]
return X_train_sample, y_train_sample<train_model>
preds = model.predict(testX)
 sub_lgb = pd.DataFrame()
sub_lgb['SK_ID_CURR'] = test_id
sub_lgb['TARGET'] = preds
sub_lgb.to_csv("lgb_baseline.csv", index=False)
 sub_lgb.head()