Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<predict_on_test>
print(os.listdir(".. /input"))
warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings('ignore')
 plt.style.use('seaborn')
 sns.set(font_scale=1 )
lgbm.fit(X[(X.date_block_num>begin_month)&(X.date_block_num<34)][ftr],
X_target[X[(X.date_block_num>begin_month)&(X.date_block_num<34)].index].clip(0,20))
pred = lgbm.predict(X[X.date_block_num==34][ftr] ).clip(0,20)
 res = pd.DataFrame()
res['ID'] = test['ID']
res['item_cnt_month'] = pred 
<merge>
random_state = 42
np.random.seed(random_state)
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 features = [c for c in df_train.columns if c not in ['ID_code', 'target']]
specific_features = ['var_68', 'var_91', 'var_108', 'var_103', 'var_12']
nt = test.merge(items[['item_id','item_category_id']], on = 'item_id',how = 'left')
 res.loc[nt[(nt.item_category_id==9)&(nt.shop_id!=12)].index,'item_cnt_month'] = 0<save_to_csv>
a, b = np.zeros(len(df_train['var_0'])) , np.zeros(len(df_test['var_0']))
for f in specific_features:
col = collections.Counter(df_train[f])
 for i in range(len(df_train[f])) :
a[i] = col[df_train[f][i]]
for j in range(len(df_test[f])) :
b[j] = col[df_test[f][j]]
a_avg, b_avg = np.average(a), np.average(b)
 a_std, b_std = np.std(a), np.std(b)
 a =(a - a_avg)/ a_std
b =(b - b_avg)/ b_std
temp1 = pd.DataFrame({'freq_' + f: a})
 temp2 = pd.DataFrame({'freq_' + f: b})
 df_train = pd.merge(df_train, temp1, left_index=True, right_index=True)
 df_test = pd.merge(df_test, temp2, left_index=True, right_index=True )
res.to_csv('submission.csv', index = False )<import_modules>
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
train_test_split,
cross_val_score 
)
<load_from_csv>
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : -1,
"num_leaves" : 22,
"learning_rate" : 0.0083,
"bagging_freq": 5,
"bagging_fraction" : 0.93,
"feature_fraction" : 0.042,
"min_data_in_leaf": 76,
"min_sum_heassian_in_leaf": 15,
"tree_learner": "serial",
"boost_from_average": "false",
"bagging_seed" : random_state,
"verbosity" : 1,
"seed": random_state
}
train = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sales_train.csv")
 test = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")
 sample_submission = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sample_submission.csv")
 items = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/items.csv")
 item_categories = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/item_categories.csv")
 shops = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/shops.csv" )<feature_engineering>
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 oof = df_train[['ID_code', 'target']]
oof['predict'] = 0
predictions = df_test[['ID_code']]
val_aucs = []
feature_importance_df = pd.DataFrame()
train['date']=train['date'].apply(lambda x:datetime.datetime.strptime(x, '%d.%m.%Y'))<merge>
features = [col for col in df_train.columns if col not in ['target', 'ID_code']]
X_test = df_test[features].values
train = train.join(items, on='item_id', rsuffix='_' ).drop(['item_id_', 'item_name'], axis=1 )<filter>
for fold,(trn_idx, val_idx)in enumerate(skf.split(df_train, df_train['target'])) :
X_train, y_train = df_train.iloc[trn_idx][features], df_train.iloc[trn_idx]['target']
X_valid, y_valid = df_train.iloc[val_idx][features], df_train.iloc[val_idx]['target']
N = 5
p_valid,yp = 0,0
for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values)
 X_t = pd.DataFrame(X_t)
 X_t = X_t.add_prefix('var_')
 trn_data = lgb.Dataset(X_t, label=y_t)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(lgb_params,
trn_data,
100000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=3000,
verbose_eval = 1000,
evals_result=evals_result 
)
 p_valid += lgb_clf.predict(X_valid)
 yp += lgb_clf.predict(X_test)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = lgb_clf.feature_importance()
fold_importance_df["fold"] = fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 oof['predict'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp/N
train[train['item_price']<=0]<filter>
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))
<feature_engineering><EOS> 
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 predictions.to_csv('lgb_all_predictions.csv', index=None)
 sub_df = pd.DataFrame({"ID_code":df_test["ID_code"].values})
 sub_df["target"] = predictions['target']
sub_df.to_csv("lgb_submission.csv", index=False)
 oof.to_csv('lgb_oof.csv', index=False )