Kaggle competition: Don't Overfit! II
prev_category = pd.get_dummies(previous_application)
 bureau_category = pd.get_dummies(bureau)
 pos_category = pd.get_dummies(POS_cash)
 credit_category= pd.get_dummies(credit_card_balance)
<data_type_conversions>
%matplotlib inline
application_train=application_train.fillna(0)
 application_test=application_test.fillna(0 )<prepare_x_and_y>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
application_test['is_test'] = 1
application_test['is_train'] = 0
application_train['is_test'] = 0
application_train['is_train'] = 1
Y = application_train['TARGET']
train_X = application_train.drop(['TARGET'], axis = 1)
 test_id = application_train['SK_ID_CURR']
test_X = application_test
data = pd.concat([train_X, test_X], axis=0 )<merge>
train_df=reduce_mem_usage(train_df)
 test_df=reduce_mem_usage(test_df )
prev_apps = 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['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' )<merge>
target = train_df["target"]
train_df = train_df.drop(["target","id"],axis=1)
 test_id = test_df["id"]
test_df = test_df.drop(["id"],axis=1)
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' )<merge>
shuffle_split = StratifiedShuffleSplit(test_size=0.25,train_size=0.75,n_splits=25 )
install_pay= install_payment[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
install_payment['SK_ID_PREV'] = install_payment['SK_ID_CURR'].map(install_pay['SK_ID_PREV'])
 avg_inst = install_payment.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' )<merge>
param_grid = {'C' : [1,0.01,0.1,10,100],
'penalty' : ["l1", "l2"],
'class_weight' : [{1:0.5, 0:0.5}, {1:0.4, 0:0.6}, {1:0.6, 0:0.4}, {1:0.7, 0:0.3}]
}
grid_search = GridSearchCV(
 estimator = LogisticRegression(random_state=12,solver="liblinear"),
param_grid = param_grid,
scoring='roc_auc',
cv = shuffle_split
)
pos_cash = POS_cash[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
POS_cash['SK_ID_PREV'] = POS_cash['SK_ID_CURR'].map(pos_cash['SK_ID_PREV'])
 POS_avg = POS_cash.groupby('SK_ID_CURR' ).mean()
data = data.merge(right=POS_avg.reset_index() , how='left', on='SK_ID_CURR' )<merge>
grid_search.fit(train_df,target )
credit_balns= 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(credit_balns['SK_ID_PREV'])
 avg_credit_bal = credit_card_balance.groupby('SK_ID_CURR' ).mean()
avg_credit_bal.columns = ['credit_bal_' + f_ for f_ in avg_credit_bal.columns]
data = data.merge(right=avg_credit_bal.reset_index() , how='left', on='SK_ID_CURR' )<prepare_x_and_y>
print("Best parameters : {}".format(grid_search.best_params_))
print("Best cross validation score: {:.2f}".format(grid_search.best_score_))
print("Best estimator: {}".format(grid_search.best_estimator_))
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]<split>
results = pd.DataFrame(grid_search.cv_results_)
 results.head(10 )
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 )<train_model>
scores_mean = np.array(results.mean_test_score ).reshape(-1)
 scores_std = np.array(results.std_test_score ).reshape(-1)
 print("mean CV scores for each fold {} ".format(scores_mean))
print("std CV scores for each fold {} ".format(scores_std))
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=170, verbose_eval=200 )<save_to_csv>
gc.collect()
preds = model.predict(testX)
 sub = application_test[['SK_ID_CURR']].copy()
sub['TARGET'] = preds
sub.to_csv('sub.csv', index= False)
 sub.head(10 )<load_from_csv>
pred_lr = grid_search.predict_proba(test_df)[:,1]
M1 = pd.read_csv('.. /input/diversity/LGBM.798.csv')
 M2 = pd.read_csv('.. /input/ingredients/WEIGHT_AVERAGE_RANK2.csv')
 M3 = pd.read_csv('.. /input/neural/sub_nn.csv')
 M4 = pd.read_csv('.. /input/genetic/pure_submission.csv')
 M5 = pd.read_csv('.. /input/diversity/xgb.796.csv' )<merge>
sub_df = pd.DataFrame()
sub_df["id"] = test_id
sub_df["target"] = pred_lr
sub_df.to_csv("baseline_lr.csv", index=False )