Kaggle competition: Don't Overfit! II
data = data.merge(POS_CASH.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(POS_CASH.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR' )<merge>
train_df = pd.read_csv('.. /input/train.csv', index_col='id')
 test_df = pd.read_csv('.. /input/test.csv', index_col='id' )
data = data.merge(credit_card.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(credit_card.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR' )<merge>
print("number of training samples: ", train_df.shape[0])
 print("number of testing samples: ", test_df.shape[0] )
data = data.merge(right=avg_payments.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments.reset_index() , how='left', on='SK_ID_CURR' )<merge>
print("count of target values: ")
 train_df.target.value_counts()
data = data.merge(right=avg_payments2.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments2.reset_index() , how='left', on='SK_ID_CURR' )<merge>
def bootstrap(data, target, n=5):

 samples = []
for i in range(n):
ix = range(len(data))
random_ixs = np.random.choice(ix, len(data), replace=True)
 new_data, new_target = data[random_ixs, :], target[random_ixs]
samples.append(( new_data, new_target))
return samples
data = data.merge(right=avg_payments3.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments3.reset_index() , how='left', on='SK_ID_CURR' )<filter>
features = [col for col in train_df.columns if col.isdigit() ]
target = 'target'
test = test[test.columns[data.isnull().mean() < 0.80]]
data = data[data.columns[data.isnull().mean() < 0.80]]<split>
max_alpha_ix = np.argmax(np.vstack(( mean_train_auc_roc, mean_val_auc_roc)).mean(axis=0))
max_mean_cv_score = np.max(np.vstack(( mean_train_auc_roc, mean_val_auc_roc)).mean(axis=0))
alpha = alpha_values[max_alpha_ix]
print("alpha: ", alpha, "max_mean, cv_score: ", max_mean_cv_score )
gc.enable()
folds = KFold(n_splits=5, 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']]
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.02,
num_leaves=255,
colsample_bytree=0.81,
subsample=0.82,
max_depth = 8,
reg_alpha=.1,
reg_lambda=.1,
min_split_gain=.01,
min_sum_hessian_in_leaf = 150,
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=200 
)
 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() 
<compute_test_metric>
max_alpha_ix = np.argmax(np.vstack(( mean_train_auc_roc, mean_val_auc_roc)).mean(axis=0))
max_mean_cv_score = np.max(np.vstack(( mean_train_auc_roc, mean_val_auc_roc)).mean(axis=0))
alpha = alpha_values[max_alpha_ix]
print("alpha: ", alpha, "max_mean, cv_score: ", max_mean_cv_score )
print('Full AUC score %.6f' % roc_auc_score(y, oof_preds))<save_to_csv>
lr = ElasticNet(alpha=alpha, l1_ratio=0.75, normalize=False, max_iter=1000, precompute=True)
 lr.fit(X_train, y_train)
 test_predictions = lr.predict(test_df.values )
test['TARGET'] = sub_preds
test[['SK_ID_CURR', 'TARGET']].to_csv('home_risk_1submission.csv', index=False )<import_modules>
submission = pd.DataFrame({
'id': np.arange(250,20000),
'target': test_predictions
})
 submission.to_csv("submission.csv", index=False )