Kaggle competition: Santander Customer Transaction Prediction
t_df['month'] = t_df['date'].dt.month
t_df['year'] = t_df['date'].dt.year<set_options>
pd.set_option('display.max_columns', 200 )
del t_df, data
gc.collect()<groupby>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
temp_sum = train_df.groupby(['date_block_num'])[['item_cnt_day', 'revenue', 'revenue_usd']].sum()
temp_sum = temp_sum.reset_index().rename(columns={'item_cnt_day': 'item_cnt_month'} )<groupby>
train_df.target.value_counts()
temp_mean = train_df.groupby(['date_block_num'])[['item_cnt_day', 'revenue', 'revenue_usd']].mean()
temp_mean = temp_mean.reset_index().rename(columns={'item_cnt_day': 'item_cnt_mean',
'revenue': 'av_revenue'} )<set_options>
param = {
'num_leaves': 10,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
del temp_mean, temp_sum
gc.collect()<groupby>
nfold = 5
shop_sum = train_df.groupby(['shop_id'], as_index=False)[['item_cnt_day', 'revenue']].sum()
shop_mean = train_df.groupby(['shop_id'], as_index=False)[['item_cnt_day', 'revenue']].mean()<sort_values>
target = 'target'
predictors = train_df.columns.values.tolist() [2:]
shop_sum = shop_sum.rename(columns={'item_cnt_day': 'item_cnt_sum'} ).sort_values(by='item_cnt_sum')
 shop_mean = shop_mean.rename(columns={'item_cnt_day': 'item_cnt_mean'} )<merge>
gc.collect()
pd.merge(shop_sum, shop_df ).tail()<set_options>
skf = StratifiedKFold(n_splits=nfold, shuffle=True, random_state=2019)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
i = 1
for train_index, valid_index in skf.split(train_df, train_df.target.values):
print("
fold {}".format(i))
xg_train = lgb.Dataset(train_df.iloc[train_index][predictors].values,
label=train_df.iloc[train_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train_df.iloc[valid_index][predictors].values,
label=train_df.iloc[valid_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 clf = lgb.train(param, xg_train, 5000, valid_sets = [xg_valid], verbose_eval=50, early_stopping_rounds = 50)
 oof[valid_index] = clf.predict(train_df.iloc[valid_index][predictors].values, num_iteration=clf.best_iteration)
 predictions += clf.predict(test_df[predictors], num_iteration=clf.best_iteration)/ nfold
i = i + 1
print("
CV AUC: {:<0.2f}".format(metrics.roc_auc_score(train_df.target.values, oof)) )
del shop_sum, shop_mean
gc.collect()<groupby>
sub_df = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub_df["target"] = predictions
sub_df[:10]
<sort_values><EOS> 
sub_df.to_csv("Customer_Transaction.csv", index=False )