Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<split>
EARLY_STOPPING=4000
NFOLDS=15
NSHUFFLES_1=2
NSHUFFLES_0=1
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']
num_folds = NFOLDS
folds = StratifiedKFold(n_splits=num_folds, shuffle=False, random_state=2319)
 oof_preds = np.zeros(( len(train_df), 1))
test_preds = np.zeros(( len(test_df), 1))
roc_cv =[]
param = {
'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
train_df, val_df = train_test_split(df, test_size=0.2, random_state=RANDOM_STATE)
 train_df, test_df = train_test_split(train_df, test_size=0.2, random_state=RANDOM_STATE)
 if VERBOSE:
print(train_df.shape, val_df.shape, test_df.shape )<choose_model_class>
def overshuff(df, y, n=2, m=1):

 df_1 = df[y==1]
y_1 = y[y==1]
randoms=np.random.randint(0, 999999, size=n)
 for i, rand in enumerate(randoms):
df_sh = df_1.apply(lambda x: x.sample(n=len(x), random_state=rand ).values)
 df = pd.concat([df, df_sh])
 y = pd.concat([y, y_1])
 print("Step {} of {}.The random state used is {}".format(i+1, n, rand))
print("The random state used for the final shuffling is {}".format(rand))
df_1 = df[y==0]
y_1 = y[y==0]
randoms=np.random.randint(0, 999999, size=m)
 for i, rand in enumerate(randoms):
df_sh = df_1.apply(lambda x: x.sample(n=len(x), random_state=rand ).values)
 df = pd.concat([df, df_sh])
 y = pd.concat([y, y_1])
 print("Step {} of {}.The random state used is {}".format(i+1, n, rand))
rand = np.random.randint(0, 999999, size=1)[0]
df, y = shuffle(df, y, random_state=rand)
 print("The random state used for the final shuffling is {}".format(rand))
return df, y
<choose_model_class><EOS> 
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold idx:{}".format(fold_ + 1))
trn_x = train_df.iloc[trn_idx][features]
trn_y = target.iloc[trn_idx]
trn_x, trn_y = overshuff(trn_x, trn_y, NSHUFFLES_1, NSHUFFLES_0)
 print("Converting the data to lgbm format")
 trn_data = lgb.Dataset(trn_x, label=trn_y)
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 print("Training the classifier")
 clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data],
verbose_eval=5000, early_stopping_rounds = EARLY_STOPPING)
 print("Making predictions for the validation data")
 val_pred = clf.predict(train_df.iloc[val_idx][features],
num_iteration=clf.best_iteration)
 print("Computing the AUC score")
 roc_cv.append(roc_auc_score(target.iloc[val_idx], val_pred))
print("AUC = {}".format(roc_auc_score(target.iloc[val_idx], val_pred)))
 oof_preds[val_idx, :] = val_pred.reshape(( -1, 1))
print("Making predictions for the test data")
 test_fold_pred = clf.predict(test_df[features],
num_iteration=clf.best_iteration ).\
reshape(( -1, 1))
test_preds += test_fold_pred
test_preds /= num_folds
roc_score_1 = round(roc_auc_score(target.ravel() , oof_preds.ravel()), 5)
 roc_cv = np.array(roc_cv)
 roc_score = round(sum(roc_cv)/len(roc_cv), 5)
 st_dev = round(np.array(roc_cv ).std() , 5)
 print("Average of the folds' AUCs = {}".format(roc_score))
print("Combined folds' AUC = {}".format(roc_score_1))
print("The standard deviation = {}".format(st_dev))
print("Saving OOF predictions")
 oof_preds = pd.DataFrame(np.column_stack(( train_df.ID_code.values,
oof_preds.ravel())) ,
columns=['ID_code', 'target'])
 oof_preds.to_csv(( "LGBM_{}.csv" ).format(str(roc_score)) , index=False)
 print("Saving submission file")
 sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = test_preds
sub.to_csv('submission_{}.csv'.format(str(roc_score)) , index=False )