Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<find_best_params>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
grid_search_xg.best_params_<find_best_params>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
best_grid_xg = grid_search_xg.best_estimator_
best_grid_xg<train_model>
def transform(df, var='var_12'):
df['random_{}'.format(var)] = np.random.normal(df[var].mean() , df[var].std() , 200000 ).round(4)
 var_counts = pd.DataFrame(df.groupby(var)['ID_code'].count() ).reset_index()
var_counts_random = pd.DataFrame(df.groupby('random_{}'.format(var)) ['ID_code'].count() ).reset_index()
merged_counts = pd.merge(var_counts, var_counts_random, left_on=var, right_on='random_{}'.format(var))
merged_counts['diff'] = merged_counts['ID_code_x'] - merged_counts['ID_code_y']
df['{}_diff_normal_dist'.format(var)] = df.merge(merged_counts[[var,'diff']], how='left')['diff']
df = df.drop('random_{}'.format(var), axis=1)
 return df
xg_regressor = best_grid_xg
xg_regressor.fit(X_train,y_train )<predict_on_test>
for var in tqdm(['var_{}'.format(x)for x in range(0, 200)]):
train_df = transform(train_df, var=var)
 test_df = transform(test_df, var=var )
<compute_test_metric><EOS> 
random_state = 42
params = {
"objective" : "binary", "metric" : "auc", "boosting": 'gbdt', "max_depth" : -1, "num_leaves" : 13,
"learning_rate" : 0.01, "bagging_freq": 5, "bagging_fraction" : 0.4, "feature_fraction" : 0.05,
"min_data_in_leaf": 80, "min_sum_heassian_in_leaf": 10, "tree_learner": "serial", "boost_from_average": "false",
"bagging_seed" : random_state, "verbosity" : 1, "seed": random_state
}
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 oof = train_df[['ID_code', 'target']].copy()
oof['predict'] = 0
predictions = test_df[['ID_code']].copy()
val_aucs = []
features = [col for col in train_df.columns if col not in ['target', 'ID_code']]
X_test = test_df[features].values
for fold,(trn_idx, val_idx)in enumerate(skf.split(train_df, train_df['target'])) :
X_train, y_train = train_df.iloc[trn_idx][features], train_df.iloc[trn_idx]['target']
X_valid, y_valid = train_df.iloc[val_idx][features], train_df.iloc[val_idx]['target']
N = 3
p_valid,yp = 0,0
trn_data = lgb.Dataset(X_train, label=y_train)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(params,
trn_data,
100000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=1000,
verbose_eval = 500,
evals_result=evals_result)
 p_valid += lgb_clf.predict(X_valid)
 yp += lgb_clf.predict(X_test)
 oof['predict'][val_idx] = p_valid
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp
mean_auc = np.mean(val_aucs)
 std_auc = np.std(val_aucs)
 all_auc = roc_auc_score(oof['target'], oof['predict'])
 print("Mean auc: %.9f, std: %.9f.All auc: %.9f." %(mean_auc, std_auc, all_auc))
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 submission = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 submission["target"] = predictions['target']
submission.to_csv("lgb_submission2.csv", index=False )