Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<prepare_x_and_y>
gc.enable()
X1 = X_countV[len(test):]
X2 = X_tfidfV[len(test):]
y = data[len(test):]<compute_test_metric>
buro_bal = pd.read_csv('.. /input/bureau_balance.csv')
 print('Buro bal shape : ', buro_bal.shape )
logR = LogisticRegression(solver='lbfgs',n_jobs=-1)
 evaluate(logR, [X1,X2], y )<compute_test_metric>
print('transform to dummies')
 buro_bal = pd.concat([buro_bal, pd.get_dummies(buro_bal.STATUS, prefix='buro_bal_status')], axis=1 ).drop('STATUS', axis=1)
logR = LogisticRegression(class_weight='balanced', solver='lbfgs', n_jobs=-1)
 evaluate(logR, [X1, X2], y )<compute_train_metric>
print('Counting buros')
 buro_counts = buro_bal[['SK_ID_BUREAU', 'MONTHS_BALANCE']].groupby('SK_ID_BUREAU' ).count()
buro_bal['buro_count'] = buro_bal['SK_ID_BUREAU'].map(buro_counts['MONTHS_BALANCE'] )
knn = KNeighborsClassifier()
evaluate(knn, [0, X2], y )<feature_engineering>
print('averaging buro bal')
 avg_buro_bal = buro_bal.groupby('SK_ID_BUREAU' ).mean()
tfidf = TfidfVectorizer(max_features=1000, min_df=2, max_df=0.9)
 X_tfidfV = tfidf.fit_transform(data['posts_processed'] )<filter>
avg_buro_bal.columns = ['avg_buro_' + f_ for f_ in avg_buro_bal.columns]
del buro_bal
gc.collect()
X3 = X_tfidfV[len(test):]<choose_model_class>
print('Read Bureau')
 buro = pd.read_csv('.. /input/bureau.csv' )
ks = [3, 5, 10]
param_grid = {'n_neighbors': ks}
grid_knn = GridSearchCV(KNeighborsClassifier() , param_grid, scoring='f1', cv=5, return_train_score=False )<train_on_grid>
print('Go to dummies')
 buro_credit_active_dum = pd.get_dummies(buro.CREDIT_ACTIVE, prefix='ca_')
 buro_credit_currency_dum = pd.get_dummies(buro.CREDIT_CURRENCY, prefix='cu_')
 buro_credit_type_dum = pd.get_dummies(buro.CREDIT_TYPE, prefix='ty_')
grid_knn.fit(X3, y[labels[0]] )<create_dataframe>
buro_full = pd.concat([buro, buro_credit_active_dum, buro_credit_currency_dum, buro_credit_type_dum], axis=1 )
pd.DataFrame(grid_knn.cv_results_)[['params', 'mean_test_score', 'rank_test_score']]<compute_train_metric>
del buro_credit_active_dum, buro_credit_currency_dum, buro_credit_type_dum
gc.collect()
randF = RandomForestClassifier(n_estimators=10)
 evaluate(randF, [0, X3], y )<define_variables>
print('Merge with buro avg')
 buro_full = buro_full.merge(right=avg_buro_bal.reset_index() , how='left', on='SK_ID_BUREAU', suffixes=('', '_bur_bal'))
cleaning_funcs = [remove_urls,
remove_numbers,
remove_extra_spaces]<drop_column>
print('Counting buro per SK_ID_CURR')
 nb_bureau_per_curr = buro_full[['SK_ID_CURR', 'SK_ID_BUREAU']].groupby('SK_ID_CURR' ).count()
buro_full['SK_ID_BUREAU'] = buro_full['SK_ID_CURR'].map(nb_bureau_per_curr['SK_ID_BUREAU'] )
data = clean_data(data, 'posts', cleaning_funcs )<feature_engineering>
print('Averaging bureau')
 avg_buro = buro_full.groupby('SK_ID_CURR' ).mean()
print(avg_buro.head() )
tfidf = TfidfVectorizer(stop_words='english',
max_df=0.8,
min_df=2)
 X_tfidfV = tfidf.fit_transform(data['posts_processed'] )<prepare_x_and_y>
del buro, buro_full
gc.collect()
X4 = X_tfidfV[len(test):]
X_sub = X_tfidfV[:len(test)]
y = data[len(test):]<choose_model_class>
print('Read prev')
 prev = pd.read_csv('.. /input/previous_application.csv')
logR = LogisticRegression(class_weight='balanced', max_iter=1000)
 logR_parameters = {'C': [0.01, 0.1, 1.0, 10],
'solver':('liblinear','saga','lbfgs')
 }
logR_grid = GridSearchCV(estimator=logR,
param_grid=logR_parameters,
cv=5,
return_train_score=False, )<prepare_output>
prev_cat_features = [
f_ for f_ in prev.columns if prev[f_].dtype == 'object'
]
sub = example<predict_on_test>
print('Go to dummies')
 prev_dum = pd.DataFrame()
for f_ in prev_cat_features:
prev_dum = pd.concat([prev_dum, pd.get_dummies(prev[f_], prefix=f_ ).astype(np.uint8)], axis=1)
 prev = pd.concat([prev, prev_dum], axis=1 )
for label in labels:
y_train = y[label]
X_train = X4
logR_grid.fit(X_train, y_train)
 sub[label] = logR_grid.predict(X_sub)
 results = pd.DataFrame(logR_grid.cv_results_)
 print(results[results['rank_test_score']==1][['params', 'mean_test_score']] )<save_to_csv>
del prev_dum
gc.collect()
sub.to_csv('submission.csv' )<set_options>
print('Counting number of Prevs')
 nb_prev_per_curr = prev[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
prev['SK_ID_PREV'] = prev['SK_ID_CURR'].map(nb_prev_per_curr['SK_ID_PREV'])
 print('Averaging prev')
 avg_prev = prev.groupby('SK_ID_CURR' ).mean()
print(avg_prev.head())
 del prev
gc.collect()
print('Reading POS_CASH')
 pos = pd.read_csv('.. /input/POS_CASH_balance.csv')
 print('Go to dummies')
 pos = pd.concat([pos, pd.get_dummies(pos['NAME_CONTRACT_STATUS'])], axis=1)
 print('Compute nb of prevs per curr')
 nb_prevs = pos[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
pos['SK_ID_PREV'] = pos['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 print('Go to averages')
 avg_pos = pos.groupby('SK_ID_CURR' ).mean()
del pos, nb_prevs
gc.collect()
print('Reading CC balance')
 cc_bal = pd.read_csv('.. /input/credit_card_balance.csv')
 print('Go to dummies')
 cc_bal = pd.concat([cc_bal, pd.get_dummies(cc_bal['NAME_CONTRACT_STATUS'], prefix='cc_bal_status_')], axis=1)
 nb_prevs = cc_bal[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
cc_bal['SK_ID_PREV'] = cc_bal['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 print('Compute average')
 avg_cc_bal = cc_bal.groupby('SK_ID_CURR' ).mean()
avg_cc_bal.columns = ['cc_bal_' + f_ for f_ in avg_cc_bal.columns]
del cc_bal, nb_prevs
gc.collect()
print('Reading Installments')
 inst = pd.read_csv('.. /input/installments_payments.csv')
 nb_prevs = inst[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
inst['SK_ID_PREV'] = inst['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 avg_inst = inst.groupby('SK_ID_CURR' ).mean()
avg_inst.columns = ['inst_' + f_ for f_ in avg_inst.columns]
print('Read data and test')
 data = pd.read_csv('.. /input/application_train.csv')
 test = pd.read_csv('.. /input/application_test.csv')
 print('Shapes : ', data.shape, test.shape)
 y = data['TARGET']
del data['TARGET']
categorical_feats = [
f for f in data.columns if data[f].dtype == 'object'
]
categorical_feats
for f_ in categorical_feats:
data[f_], indexer = pd.factorize(data[f_])
 test[f_] = indexer.get_indexer(test[f_] )
%matplotlib inline
plt.rcParams['figure.figsize'] = [9, 12]
warnings.simplefilter('ignore' )<load_from_csv>
data = data.merge(right=avg_buro.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_buro.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_prev.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_prev.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_pos.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_pos.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_cc_bal.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_cc_bal.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR')
 del avg_buro, avg_prev
gc.collect()
train = pd.read_csv("/kaggle/input/whoisafriend/train.csv")
 test = pd.read_csv("/kaggle/input/whoisafriend/test.csv")
 sub = pd.read_csv("/kaggle/input/whoisafriend/sample_submission.csv")
 train.shape, test.shape, sub.shape<groupby>
gc.enable()
folds = KFold(n_splits=6, shuffle=True, random_state=546789)
 oof_preds = np.zeros(data.shape[0])
 sub_preds = np.zeros(test.shape[0])
 feature_importance_df = pd.DataFrame()
feats = [f for f in data.columns if f not in ['SK_ID_CURR']]
agg_train = train.groupby(['Person A', 'Person B'])['Years of Knowing'].count().reset_index()
agg_train.rename({
"Years of Knowing": "Interaction Count"
}, axis=1, inplace=True)
 agg_test = test.groupby(['Person A', 'Person B'])['Years of Knowing'].count().reset_index()
agg_test.rename({
"Years of Knowing": "Interaction Count"
}, axis=1, inplace=True )<merge>
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(data)) :
trn_x, trn_y = data[feats].iloc[trn_idx], y.iloc[trn_idx]
val_x, val_y = data[feats].iloc[val_idx], y.iloc[val_idx]
clf = LGBMClassifier(
 n_estimators=10000,
learning_rate=0.03,
num_leaves = 22,
colsample_bytree=0.8,
subsample=0.8,
max_depth=6,
reg_alpha=0.1,
reg_lambda=0.1,
min_split_gain=0.01,
min_child_weight=100,
silent=-1,
verbose=-1, 
)
 clf.fit(trn_x, trn_y,
eval_set= [(trn_x, trn_y),(val_x, val_y)],
eval_metric='auc', verbose=100, early_stopping_rounds=300 
)
 oof_preds[val_idx] = clf.predict_proba(val_x, num_iteration=clf.best_iteration_)[:, 1]
sub_preds += clf.predict_proba(test[feats], num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = clf.feature_importances_
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d AUC : %.6f' %(n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx])))
 del clf, trn_x, trn_y, val_x, val_y
gc.collect()
<feature_engineering><EOS> 
print('Full AUC score %.6f' % roc_auc_score(y, oof_preds))
test['TARGET'] = sub_preds
test[['SK_ID_CURR', 'TARGET']].to_csv('first_submission.csv', index=False )