Kaggle competition: Santander Customer Transaction Prediction
sales_train_df['revenue'] = sales_train_df['item_price'] * sales_train_df['item_cnt_day']<feature_engineering>
warnings.filterwarnings('ignore' )
full_data = reduce_mem_usage(full_data )<data_type_conversions>
num_submission = 3
random_state = 3213
group = sales_train_df.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 full_data = pd.merge(full_data, group, on=['date_block_num','shop_id','item_id'], how='left')
 full_data['item_cnt_month'] =(full_data['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))<data_type_conversions>
train_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/train.csv')
 test_df = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv' )
test_df['date_block_num'] = 34
test_df['date_block_num'] = test_df['date_block_num'].astype(np.int8)
 test_df['shop_id'] = test_df['shop_id'].astype(np.int8)
 test_df['item_id'] = test_df['item_id'].astype(np.int16 )<concatenate>
target = train_df['target']
full_data = pd.concat([full_data, test_df], ignore_index=True, sort=False, keys=['date_block_num','shop_id','item_id'])
 full_data.fillna(0, inplace=True )<data_type_conversions>
train_stats = pd.read_csv('.. /input/stats-df/stats_train.csv')
 test_stats = pd.read_csv('.. /input/stats-df/stats_test.csv' )
full_data = pd.merge(full_data, shops_df, on=['shop_id'], how='left')
 full_data = pd.merge(full_data, items_df, on=['item_id'], how='left')
 full_data = pd.merge(full_data, item_categories_df, on=['item_category_id'], how='left')
 full_data['city_code'] = full_data['city_code'].astype(np.int8)
 full_data['item_category_id'] = full_data['item_category_id'].astype(np.int8)
 full_data['type_code'] = full_data['type_code'].astype(np.int8)
 full_data['subtype_code'] = full_data['subtype_code'].astype(np.int8 )<merge>
extra_train = pd.concat([train_df.transpose() , train_stats.transpose() ] ).transpose()
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df<merge>
del extra_train["Unnamed: 0"]
%%time
date_item_group = full_data.groupby(["date_block_num", "item_id"] ).agg({"item_cnt_month": ["mean"]})
 date_item_group.columns = ["date_item_avg_item_cnt"]
date_item_group.reset_index(inplace=True)
 full_data = pd.merge(full_data, date_item_group, on=["date_block_num", "item_id"], how="left")
 full_data = lag_feature(full_data, [1, 2, 3, 6, 12], "date_item_avg_item_cnt")
 full_data.drop(["date_item_avg_item_cnt"], axis=1, inplace=True)
 date_item_group = full_data.groupby(["date_block_num", "shop_id"] ).agg({"item_cnt_month": ["mean"]})
 date_item_group.columns = ["date_shop_avg_item_cnt"]
date_item_group.reset_index(inplace=True)
 full_data = pd.merge(full_data, date_item_group, on=["date_block_num", "shop_id"], how="left")
 full_data = lag_feature(full_data, [1, 2, 3, 6, 12], "date_shop_avg_item_cnt")
 full_data.drop(["date_shop_avg_item_cnt"], axis=1, inplace=True)
 date_item_group = full_data.groupby(["date_block_num", "item_id", "shop_id"] ).agg({"item_cnt_month": ["mean"]})
 date_item_group.columns = ["date_item_shop_avg_item_cnt"]
date_item_group.reset_index(inplace=True)
 full_data = pd.merge(full_data, date_item_group, on=["date_block_num", "item_id", "shop_id"], how="left")
 full_data = lag_feature(full_data, [1, 2, 3], "date_item_shop_avg_item_cnt")
 full_data.drop(["date_item_shop_avg_item_cnt"], axis=1, inplace=True)
 date_shop_cat_group = full_data.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month': ['mean']})
 date_shop_cat_group.columns = ['date_shop_cat_avg_item_cnt']
date_shop_cat_group.reset_index(inplace=True)
 full_data = pd.merge(full_data, date_shop_cat_group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 full_data = lag_feature(full_data, [1], 'date_shop_cat_avg_item_cnt')
 full_data.drop(['date_shop_cat_avg_item_cnt'], axis=1, inplace=True)
 item_group = sales_train_df.groupby(["item_id"] ).agg({"item_price": ["mean"]})
 item_group.columns = ["item_avg_item_price"]
item_group.reset_index(inplace=True)
 full_data = pd.merge(full_data, item_group, on=["item_id"], how="left")
 date_item_group = sales_train_df.groupby(["date_block_num", "item_id"] ).agg({"item_price": ["mean"]})
 date_item_group.columns = ["date_item_avg_item_price"]
date_item_group.reset_index(inplace=True)
 full_data = pd.merge(full_data, date_item_group, on=["date_block_num", "item_id"], how="left")
 lags = [1,2,3,4,5,6]
full_data = lag_feature(full_data, lags, 'date_item_avg_item_price')
 for i in lags:
full_data['delta_price_lag_'+str(i)] = \ 
(full_data['date_item_avg_item_price_lag_'+str(i)] - full_data['item_avg_item_price'])/ full_data['item_avg_item_price']
def select_trend(row):
for i in lags:
if row['delta_price_lag_'+str(i)]:
return row['delta_price_lag_'+str(i)]
return 0
full_data['delta_price_lag'] = full_data.apply(select_trend, axis=1)
 full_data['delta_price_lag'] = full_data['delta_price_lag'].astype(np.float16)
 full_data['delta_price_lag'].fillna(0, inplace=True)
<merge>
extra_test = pd.concat([test_df.transpose() , test_stats.transpose() ] ).transpose()
group = sales_train_df.groupby(['date_block_num','shop_id'] ).agg({'revenue': ['sum']})
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 full_data = pd.merge(full_data, group, on=['date_block_num','shop_id'], how='left')
 full_data['date_shop_revenue'] = full_data['date_shop_revenue'].astype(np.float32)
 group = group.groupby(['shop_id'] ).agg({'date_shop_revenue': ['mean']})
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 full_data = pd.merge(full_data, group, on=['shop_id'], how='left')
 full_data['shop_avg_revenue'] = full_data['shop_avg_revenue'].astype(np.float32)
 full_data['delta_revenue'] =(full_data['date_shop_revenue'] - full_data['shop_avg_revenue'])/ full_data['shop_avg_revenue']
full_data['delta_revenue'] = full_data['delta_revenue'].astype(np.float16)
 full_data = lag_feature(full_data, [1], 'delta_revenue')
 full_data.drop(['date_shop_revenue','shop_avg_revenue','delta_revenue'], axis=1, inplace=True )<categorify>
del extra_test["Unnamed: 0"]
full_data['month'] = full_data['date_block_num'] % 12
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 full_data['days'] = full_data['month'].map(days ).astype(np.int8 )<filter>
del train_df, test_df, train_stats, test_stats
full_data = full_data[full_data.date_block_num > 11]<correct_missing_values>
features = [c for c in extra_train.columns if c not in ['ID_code', 'target']]
%%time
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
full_data = fill_na(full_data )<drop_column>
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
full_data.drop(["ID"], axis=1, inplace=True )<data_type_conversions>
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : 90,
"num_leaves" : 15,
"learning_rate" : 0.01000123,
"bagging_freq": 5,
"bagging_fraction" : 0.4,
"feature_fraction" : 0.05,
"min_data_in_leaf": 150,
"min_sum_heassian_in_leaf": 15,
"tree_learner": "voting",
"boost_from_average": "false",
"lambda_l1" : 10,
"lambda_l2" : 10,
"bagging_seed" : random_state,
"verbosity" : 1,
"seed": random_state
}
cat_col = ["date_block_num", "shop_id", "item_id", "item_category_id", "month", "days"]
full_data[cat_col] = full_data[cat_col].astype(np.int32 )<prepare_x_and_y>
num_folds = 3
folds = StratifiedKFold(n_splits=num_folds, shuffle=False, random_state=random_state)
 oof = extra_train[['ID_code', 'target']]
oof['predict'] = 0
predictions = extra_test[['ID_code']]
val_aucs = []
feature_importance_df = pd.DataFrame()
X_train = full_data[full_data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 y_train = full_data[full_data.date_block_num < 33]['item_cnt_month']
X_valid = full_data[full_data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 y_valid = full_data[full_data.date_block_num == 33]['item_cnt_month']
X_test = full_data[full_data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<drop_column>
X_test = extra_test[features].values
X_train.drop(["delta_price_lag_1", "delta_price_lag_2", "delta_price_lag_3", "delta_price_lag_4", "delta_price_lag_5", "delta_price_lag_6"], axis=1, inplace=True)
 X_valid.drop(["delta_price_lag_1", "delta_price_lag_2", "delta_price_lag_3", "delta_price_lag_4", "delta_price_lag_5", "delta_price_lag_6"], axis=1, inplace=True)
 X_test.drop(["delta_price_lag_1", "delta_price_lag_2", "delta_price_lag_3", "delta_price_lag_4", "delta_price_lag_5", "delta_price_lag_6"], axis=1, inplace=True )<define_variables>
for fold,(trn_idx, val_idx)in enumerate(folds.split(extra_train.values, target.values)) :
print("Fold :{}".format(fold + 1))
X_train, y_train = extra_train.iloc[trn_idx][features], target.iloc[trn_idx]
X_valid, y_valid = extra_train.iloc[val_idx][features], target.iloc[val_idx]
N = 4
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_')
 X_t.columns = [*X_t.columns[:200], *swap]
trn_data = lgb.Dataset(X_t.values, label=y_t)
 val_data = lgb.Dataset(X_valid.values, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(lgb_params,
trn_data,
110000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=3500,
verbose_eval = 1000,
evals_result=evals_result 
)
 p_valid += lgb_clf.predict(X_valid.values)
 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
cat_feats = [i for i, c in enumerate(X_train.columns)if c in cat_col]<set_options>
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))
del full_data
gc.collect()<save_to_csv>
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":extra_test["ID_code"].values})
 sub_df["target"] = predictions['target']
sub_df.to_csv("lgb_stats_aug_submission{}.csv".format(num_submission), index=False)
 oof.to_csv('lgb_oof.csv', index=False )