Kaggle competition: Santander Customer Transaction Prediction
cc_df = lag_feature(cc_df, [1], 'shop_item_active_months' )<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' )
for i in [1]:
cc_df['shop_item_active_months_lag_'+str(i)] += i
cc_df['shop_item_active_months_lag_'+str(i)] = cc_df['shop_item_active_months_lag_'
+str(i)].fillna(0 ).astype('int' )<drop_column>
magic_index = pd.read_csv('.. /input/magicdddd/78734.csv',header = None )
cc_df = cc_df.drop(['shop_item_active_months'], axis=1 )<merge>
magic_index_list = list(magic_index[0] )
alles = pd.merge(alles, cc_df, on=['item_id', 'shop_id', 'date_block_num'], how='left' )<drop_column>
magic_valid = list(set(train_df.index)-set(magic_index_list))
cum_revs = cum_revs[['revenue', 'revenue_usd']]<merge>
train_features = train_df.drop(['target','ID_code'], axis = 1)
 test_features = test_df.drop(['ID_code'],axis = 1)
 train_target = train_df['target']
cum_revs = pd.merge(all_vals[['shop_id', 'date_block_num']],
cum_revs, on=['shop_id', 'date_block_num'], how='left' ).drop_duplicates()<groupby>
large_duplicate_features=['var_5','var_10','var_11','var_17','var_18','var_19','var_20','var_21','var_26','var_30','var_40','var_41','var_44','var_45','var_47','var_48','var_49','var_51','var_54','var_55','var_61','var_67','var_70','var_73','var_74','var_75','var_76','var_80','var_82','var_83','var_84','var_86','var_87','var_90','var_96','var_97','var_100','var_102','var_107','var_117','var_118','var_120','var_123','var_134','var_135','var_136','var_137','var_139','var_141','var_142','var_147','var_149','var_155','var_157','var_158','var_160','var_167','var_171','var_172','var_173','var_174','var_176','var_178','var_182','var_184','var_187','var_199']
cum_revs['c_shop_revenue_usd'] = cum_revs.groupby('shop_id')['revenue_usd'].cumsum().values
cum_revs['c_shop_revenue'] = cum_revs.groupby('shop_id')['revenue'].cumsum().values<merge>
less_duplicate_features =['var_4']
tmp = cum_revs[['date_block_num','shop_id', 'c_shop_revenue']]
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id', 'c_shop_revenue'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
cum_revs = pd.merge(cum_revs, shifted, on=['date_block_num','shop_id'], how='left' )<merge>
train_df_copy = train_df.copy()
test_df_copy = test_df.copy()
for col in train_df.columns[2:202]:
duplicate = list(set(list(train_features[col].value_counts().index)) & set(list(test_features[col].value_counts().index)))
 train_df_copy[col+'train_test_have'] = train_df_copy[col].isin(duplicate ).astype(int)
 test_df_copy[col+'train_test_have'] = test_df_copy[col].isin(duplicate ).astype(int )
tmp = cum_revs[['date_block_num','shop_id', 'c_shop_revenue_usd']]
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id', 'c_shop_revenue_usd'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
cum_revs = pd.merge(cum_revs, shifted, on=['date_block_num','shop_id'], how='left' )<feature_engineering>
for f in train_df.columns[2:202]:
train_df_copy[f+'duplicate_value'] = round(train_df_copy[f],4)*train_df_copy[f+'train_test_have']
test_df_copy[f+'duplicate_value'] = round(test_df_copy[f],4)*test_df_copy[f+'train_test_have']
cum_revs = reduce_mem_usage(cum_revs )<merge>
train_features = train_df_copy.drop(['target','ID_code'], axis = 1)
 test_features = test_df_copy.drop(['ID_code'],axis = 1)
 train_target = train_df_copy['target']
alles = pd.merge(alles, cum_revs[['shop_id', 'date_block_num',
'c_shop_revenue_lag_1', 'c_shop_revenue_usd_lag_1']],
on=['shop_id', 'date_block_num'], how='left' )<drop_column>
train_all = pd.concat([train_features,test_features], axis = 0 )
cum_revs = cum_revs[['revenue', 'revenue_usd']]<merge>
for f in train_all.columns[0:200]:
train_all[f+'duplicate'] =(( train_all.duplicated(f,False ).astype(int)) -1)*(-1)
cum_revs = pd.merge(all_vals, cum_revs, on=['shop_id', 'date_block_num', 'item_id'], how='left' )<data_type_conversions>
cum_revs['revenue'] = cum_revs['revenue'].fillna(0)
 cum_revs['revenue_usd'] = cum_revs['revenue_usd'].fillna(0 )<groupby>
cum_revs['c_revenue_usd'] = cum_revs.groupby('shop_id')['revenue_usd'].cumsum().values
cum_revs['c_revenue'] = cum_revs.groupby('shop_id')['revenue'].cumsum().values<feature_engineering>
train_features = train_all.iloc[:len(train_target)]
test_features = train_all.iloc[len(train_target):len(train_all)]
cum_revs = reduce_mem_usage(cum_revs )<merge>
gc.collect()
alles = pd.merge(alles, cum_revs[['shop_id', 'date_block_num', 'item_id',
'c_revenue_lag_1', 'c_revenue_usd_lag_1']],
on=['shop_id', 'date_block_num', 'item_id'], how='left' )<groupby>
train_valid = list(set(train_features.index)-set(magic_valid))
group = month_sum.groupby(['date_block_num'])
 gr_df = group.mean()
gr_df = gr_df.rename(columns={'item_cnt_month':'av_month'} ).reset_index()<merge>
gc.collect()
tmp = gr_df[['date_block_num', 'av_month']]
shifted = tmp.copy()
shifted.columns = ['date_block_num', 'av_month'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
gr_df = pd.merge(gr_df, shifted, on=['date_block_num'], how='left' )<merge>
train_features_array = np.array(train_features)
 train_target_array = np.array(train_target)
 x_train = train_features_array[np.array(train_valid)]
y_train = train_target_array[np.array(train_valid)]
x_valid = train_features_array[np.array(magic_valid)]
y_valid = train_target_array[np.array(magic_valid)]
alles = pd.merge(alles, gr_df[['av_month_lag_1', 'date_block_num']], on=['date_block_num'], how='left' )<groupby>
group = month_sum.groupby(['date_block_num', 'item_id'])
 gr_df = group.mean()
gr_df = gr_df.rename(columns={'item_cnt_month':'av_month_item'} ).reset_index()<merge>
del train_features_array
del train_target_array
gc.collect()
tmp = gr_df[['date_block_num', 'item_id', 'av_month_item']]
shifted = tmp.copy()
shifted.columns = ['date_block_num', 'item_id', 'av_month_item'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
gr_df = pd.merge(gr_df, shifted, on=['date_block_num', 'item_id'], how='left' )<merge>
del train_df_copy
del test_df_copy
del train_all
gc.collect()
alles = pd.merge(alles, gr_df[['av_month_item_lag_1', 'date_block_num', 'item_id']],
on=['date_block_num', 'item_id'], how='left' )<groupby>
oof_cb = np.zeros(len(train_features))
predictions_cb = np.zeros(len(test_df))
cat_params = {
'learning_rate':0.01,
'max_depth':3,
'eval_metric': 'AUC',
'bootstrap_type': 'Bernoulli',
'objective': 'Logloss',
'od_type': 'Iter',
'l2_leaf_reg': 13,
'random_seed': 2500,
'allow_writing_files': False}
num_round = 50000
clf = CatBoostClassifier(num_round, task_type='GPU', early_stopping_rounds=3000,**cat_params)
 clf.fit(x_train, y_train, eval_set=(x_valid, y_valid), cat_features=[], use_best_model=True, verbose=500)
 predictions_cb = clf.predict_proba(test_features)[:,1]
valid_cb = clf.predict_proba(x_valid)[:,1]
oof_cb = clf.predict_proba(x_train)[:,1]
print("CV score: {:<8.5f}".format(roc_auc_score(y_valid, valid_cb)))
 gc.collect()
group = month_sum.groupby(['date_block_num', 'shop_id'])
 gr_df = group.mean()
gr_df = gr_df.rename(columns={'item_cnt_month':'av_month_shop'} ).reset_index()<merge>
feature_importance_df = pd.DataFrame()
features = [c for c in train_features.columns if c not in ['ID_code', 'target']]
param = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 11,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
trn_data = lgb.Dataset(x_train, label=y_train)
 val_data = lgb.Dataset(x_valid, label=y_valid)
 oof = np.zeros(len(x_train))
predictions = np.zeros(len(test_features))
num_round = 100000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 3000)
 oof = clf.predict(x_train, num_iteration=clf.best_iteration)
 valid = clf.predict(x_valid, num_iteration=clf.best_iteration)
 predictions = clf.predict(test_features, num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print("CV score: {:<8.5f}".format(roc_auc_score(y_valid, valid)))
 gc.collect()
tmp = gr_df[['date_block_num', 'shop_id', 'av_month_shop']]
shifted = tmp.copy()
shifted.columns = ['date_block_num', 'shop_id', 'av_month_shop'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
gr_df = pd.merge(gr_df, shifted, on=['date_block_num', 'shop_id'], how='left' )<merge>
test = pd.read_csv('.. /input/ensemble-usesssssss/submission_ensemble.csv')
 test2 = pd.read_csv('.. /input/ensemble-use/submission_ensemble.csv' )
alles = pd.merge(alles, gr_df[['av_month_shop_lag_1', 'date_block_num', 'shop_id']],
on=['date_block_num', 'shop_id'], how='left' )<merge>
id_code_test = test_df['ID_code']
month_cat = pd.merge(month_sum, item_df )<groupby>
my_submission_lbgm = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions} )
group = month_cat.groupby(['date_block_num', 'item_category_id'])
 gr_df = group.mean()
gr_df = gr_df.rename(columns={'item_cnt_month':'av_month_cat'} ).reset_index()<merge>
print(pearsonr(test.target.rank() ,my_submission_lbgm.target.rank()))
print(pearsonr(test2.target.rank() ,my_submission_lbgm.target.rank()))
tmp = gr_df[['date_block_num', 'item_category_id', 'av_month_cat']]
shifted = tmp.copy()
shifted.columns = ['date_block_num', 'item_category_id', 'av_month_cat'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
gr_df = pd.merge(gr_df, shifted, on=['date_block_num', 'item_category_id'], how='left' )<merge>
esemble_lgbm_cat = 0.5*valid_cb+0.5*valid
print('LightBGM auc = {:<8.5f}'.format(roc_auc_score(y_valid, valid)))
 print('catboost auc = {:<8.5f}'.format(roc_auc_score(y_valid, valid_cb)))
 print('LightBGM+catboost auc = {:<8.5f}'.format(roc_auc_score(y_valid, esemble_lgbm_cat)))
alles = pd.merge(alles, gr_df[['av_month_cat_lag_1', 'date_block_num', 'item_category_id']],
on=['date_block_num', 'item_category_id'], how='left' )<groupby>
id_code_test = test_df['ID_code']
group = month_cat.groupby(['date_block_num', 'item_subcategory'])
 gr_df = group.mean()
gr_df = gr_df.rename(columns={'item_cnt_month':'av_month_subcat'} ).reset_index()<merge>
esemble_pred_lgbm_cat = 0.5*predictions+0.5*predictions_cb
tmp = gr_df[['date_block_num', 'item_subcategory', 'av_month_subcat']]
shifted = tmp.copy()
shifted.columns = ['date_block_num', 'item_subcategory', 'av_month_subcat'+'_lag_'+str(1)]
shifted['date_block_num'] += 1
gr_df = pd.merge(gr_df, shifted, on=['date_block_num', 'item_subcategory'], how='left' )<merge>
my_submission_lbgm = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions})
 my_submission_cat = pd.DataFrame({"ID_code" : id_code_test, "target" : predictions_cb})
 my_submission_esemble_lgbm_cat = pd.DataFrame({"ID_code" : id_code_test, "target" : esemble_pred_lgbm_cat})
<groupby><EOS> 
my_submission_lbgm.to_csv('submission_lbgm.csv', index = False, header = True)
 my_submission_cat.to_csv('submission_cb.csv', index = False, header = True)
 my_submission_esemble_lgbm_cat.to_csv('my_submission_esemble_lgbm_cat.csv', index = False, header = True)