Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<statistical_test>
!pip install bayesian-optimization
feature_correlation_analysis(train_1,shop_supercategory_encoding,shop_supercategory_encoding_seniority_1,'shop_supercategory',['month_id','shop_id','item_supercategory_id'],1)
 compare_features(shop_supercategory_encoding,shop_supercategory_encoding_seniority_1,'shop_supercategory',['month_id','shop_id','item_supercategory_id'],1,20,20,20 )<statistical_test>
pd.set_option('display.max_columns', 200 )
feature_correlation_analysis(train_1,category_months_since_release_encoding,category_months_since_release_encoding_seniority_1,'category_months_since_release',['month_id','item_category_id','item_months_since_release'],1)
 compare_features(category_months_since_release_encoding,category_months_since_release_encoding_seniority_1,'category_months_since_release',['month_id','item_category_id','item_months_since_release'],1,20,20,20 )<compute_test_metric>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
feature_correlation_analysis(train_1,shop_category_months_since_release_encoding,shop_category_months_since_release_encoding_seniority_1,'shop_category_months_since_release',['month_id','shop_id','item_category_id','item_months_since_release'],1 )<create_dataframe>
target = 'target'
predictors = train_df.columns.values.tolist() [2:]
ts = time.time()
target_features_df=[]
col_labels=['item_avg_sales','item_avg_sales_over_sold']
target_features_df.append(moving_statistics(train_1,item_encoding,['month_id','item_id'],col_labels,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,item_encoding,['month_id','item_id'],col_labels,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,item_encoding,['month_id','item_id'],col_labels,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,item_encoding,['month_id','item_id'],col_labels,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,item_encoding,['month_id','item_id'],col_labels,[1]))
target_features_df.append(lag_features(train_1,item_encoding,['month_id','item_id'],col_labels,[12]))
target_features_df.append(time_mapping(train_1,item_encoding,['item_id'],col_labels,'item_month_id_of_last_sale',suffix='_last_sale'))
target_features_df.append(moving_statistics(train_1,item_encoding,['month_id','item_id'],['item_max_quantity'],34,'max',suffix='_absolute_max'))
col_labels_seniority_1=['item_avg_sales_seniority_1']
target_features_df.append(moving_statistics(train_1,item_encoding_seniority_1,['month_id','item_id'],col_labels_seniority_1,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,item_encoding_seniority_1,['month_id','item_id'],col_labels_seniority_1,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,item_encoding_seniority_1,['month_id','item_id'],col_labels_seniority_1,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,item_encoding_seniority_1,['month_id','item_id'],col_labels_seniority_1,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,item_encoding_seniority_1,['month_id','item_id'],col_labels_seniority_1,[1]))
target_features_df.append(lag_features(train_1,item_encoding_seniority_1,['month_id','item_id'],col_labels_seniority_1,[12]))
target_features_df.append(time_mapping(train_1,item_encoding_seniority_1,['item_id'],col_labels_seniority_1,'item_month_id_of_last_sale',suffix='_last_sale'))
print('time : ' +str(time.time() - ts))
col_labels=['shop_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(moving_statistics(train_1,shop_encoding,['month_id','shop_id'],col_labels,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,shop_encoding,['month_id','shop_id'],col_labels,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,shop_encoding,['month_id','shop_id'],col_labels,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,shop_encoding,['month_id','shop_id'],col_labels,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,shop_encoding,['month_id','shop_id'],col_labels,[1]))
target_features_df.append(lag_features(train_1,shop_encoding,['month_id','shop_id'],col_labels,[12]))
target_features_df.append(moving_statistics(train_1,shop_encoding_seniority_1,['month_id','shop_id'],col_labels_seniority_1,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,shop_encoding_seniority_1,['month_id','shop_id'],col_labels_seniority_1,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,shop_encoding_seniority_1,['month_id','shop_id'],col_labels_seniority_1,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,shop_encoding_seniority_1,['month_id','shop_id'],col_labels_seniority_1,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,shop_encoding_seniority_1,['month_id','shop_id'],col_labels_seniority_1,[1]))
target_features_df.append(lag_features(train_1,shop_encoding_seniority_1,['month_id','shop_id'],col_labels_seniority_1,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['category_months_since_release_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(moving_statistics(train_1,category_months_since_release_encoding_seniority_1,['month_id','item_category_id','item_months_since_release'],col_labels_seniority_1,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,category_months_since_release_encoding_seniority_1,['month_id','item_category_id','item_months_since_release'],col_labels_seniority_1,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,category_months_since_release_encoding_seniority_1,['month_id','item_category_id','item_months_since_release'],col_labels_seniority_1,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,category_months_since_release_encoding_seniority_1,['month_id','item_category_id','item_months_since_release'],col_labels_seniority_1,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,category_months_since_release_encoding_seniority_1,['month_id','item_category_id','item_months_since_release'],col_labels_seniority_1,[1]))
target_features_df.append(lag_features(train_1,category_months_since_release_encoding_seniority_1,['month_id','item_category_id','item_months_since_release'],col_labels_seniority_1,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['shop_category_months_since_release_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(moving_statistics(train_1,shop_category_months_since_release_encoding_seniority_1,['month_id','shop_id','item_category_id','item_months_since_release'],col_labels_seniority_1,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,shop_category_months_since_release_encoding_seniority_1,['month_id','shop_id','item_category_id','item_months_since_release'],col_labels_seniority_1,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,shop_category_months_since_release_encoding_seniority_1,['month_id','shop_id','item_category_id','item_months_since_release'],col_labels_seniority_1,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,shop_category_months_since_release_encoding_seniority_1,['month_id','shop_id','item_category_id','item_months_since_release'],col_labels_seniority_1,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,shop_category_months_since_release_encoding_seniority_1,['month_id','shop_id','item_category_id','item_months_since_release'],col_labels_seniority_1,[1]))
target_features_df.append(lag_features(train_1,shop_category_months_since_release_encoding_seniority_1,['month_id','shop_id','item_category_id','item_months_since_release'],col_labels_seniority_1,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['supercategory_months_since_release_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(moving_statistics(train_1,supercategory_months_since_release_encoding_seniority_1,['month_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,supercategory_months_since_release_encoding_seniority_1,['month_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,supercategory_months_since_release_encoding_seniority_1,['month_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,supercategory_months_since_release_encoding_seniority_1,['month_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,supercategory_months_since_release_encoding_seniority_1,['month_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,[1]))
target_features_df.append(lag_features(train_1,supercategory_months_since_release_encoding_seniority_1,['month_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['shop_supercategory_months_since_release_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(moving_statistics(train_1,shop_supercategory_months_since_release_encoding_seniority_1,['month_id','shop_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,shop_supercategory_months_since_release_encoding_seniority_1,['month_id','shop_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,shop_supercategory_months_since_release_encoding_seniority_1,['month_id','shop_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,shop_supercategory_months_since_release_encoding_seniority_1,['month_id','shop_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,shop_supercategory_months_since_release_encoding_seniority_1,['month_id','shop_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,[1]))
target_features_df.append(lag_features(train_1,shop_supercategory_months_since_release_encoding_seniority_1,['month_id','shop_id','item_supercategory_id','item_months_since_release'],col_labels_seniority_1,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['shop_category_avg_sales_compared_to_category']
target_features_df.append(moving_statistics(train_1,shop_category_encoding,['month_id','shop_id','item_category_id'],col_labels,34,'mean',suffix='_absolute_mean'))
target_features_df.append(moving_statistics(train_1,shop_category_encoding,['month_id','shop_id','item_category_id'],col_labels,12,'mean',suffix='_annual_mean'))
target_features_df.append(moving_statistics(train_1,shop_category_encoding,['month_id','shop_id','item_category_id'],col_labels,6,'mean',suffix='_semiannual_mean'))
target_features_df.append(moving_statistics(train_1,shop_category_encoding,['month_id','shop_id','item_category_id'],col_labels,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,shop_category_encoding,['month_id','shop_id','item_category_id'],col_labels,[1]))
target_features_df.append(lag_features(train_1,shop_category_encoding,['month_id','shop_id','item_category_id'],col_labels,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['shop_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(rational_fraction(train_1,shop_encoding,['month_id','shop_id'],col_labels,weights_num=[0,0,0,0,0,0,0,0,0,0,0,12],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_lag12_to_annual_mean'))
target_features_df.append(rational_fraction(train_1,shop_encoding,['month_id','shop_id'],col_labels,weights_num=[12,0,0,0,0,0,0,0,0,0,0,0],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_lag1_to_annual_mean'))
target_features_df.append(rational_fraction(train_1,shop_encoding,['month_id','shop_id'],col_labels,weights_num=[4,4,4,0,0,0,0,0,0,0,0,0],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_recent_mean_to_annual_mean'))
col_labels=['category_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(rational_fraction(train_1,category_encoding,['month_id','item_category_id'],['category_avg_sales'],weights_num=[0,0,0,0,0,0,0,0,0,0,0,12],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_lag12_to_annual_mean'))
target_features_df.append(rational_fraction(train_1,category_encoding,['month_id','item_category_id'],['category_avg_sales'],weights_num=[12,0,0,0,0,0,0,0,0,0,0,0],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_lag1_to_annual_mean'))
target_features_df.append(rational_fraction(train_1,category_encoding,['month_id','item_category_id'],['category_avg_sales'],weights_num=[4,4,4,0,0,0,0,0,0,0,0,0],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_recent_mean_to_annual_mean'))
col_labels=['supercategory_avg_sales']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(rational_fraction(train_1,supercategory_encoding,['month_id','item_supercategory_id'],col_labels,weights_num=[0,0,0,0,0,0,0,0,0,0,0,12],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_lag12_to_annual_mean'))
target_features_df.append(rational_fraction(train_1,supercategory_encoding,['month_id','item_supercategory_id'],col_labels,weights_num=[12,0,0,0,0,0,0,0,0,0,0,0],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_lag1_to_annual_mean'))
target_features_df.append(rational_fraction(train_1,supercategory_encoding,['month_id','item_supercategory_id'],col_labels,weights_num=[4,4,4,0,0,0,0,0,0,0,0,0],weights_denom=[1,1,1,1,1,1,1,1,1,1,1,1],suffix='_compare_recent_mean_to_annual_mean'))
col_labels=['category_avg_sales_compared_to_supercategory']
col_labels_seniority_1=[col+'_seniority_1' for col in col_labels]
target_features_df.append(moving_statistics(train_1,category_encoding,['month_id','item_category_id'],col_labels,3,'mean',suffix='_recent_mean'))
target_features_df.append(lag_features(train_1,category_encoding,['month_id','item_category_id'],col_labels,[1]))
target_features_df.append(lag_features(train_1,category_encoding,['month_id','item_category_id'],col_labels,[12]))
print('time : ' +str(time.time() - ts))
col_labels=['category_month_of_release_avg_sales_compared_to_category']
target_features_df.append(moving_statistics(train_1,category_month_of_release_encoding,['month_id','item_category_id','item_month_of_release'],col_labels,34,'mean',suffix='_absolute_mean'))
col_labels=['supercategory_month_of_release_avg_sales_compared_to_supercategory']
target_features_df.append(moving_statistics(train_1,supercategory_month_of_release_encoding,['month_id','item_supercategory_id','item_month_of_release'],col_labels,34,'mean',suffix='_absolute_mean'))
print('time : ' +str(time.time() - ts))
train_1=pd.concat([train_1]+target_features_df,axis=1,sort=False)
 del target_features_df
gc.collect()
print('time : ' +str(time.time() - ts))
print(train_1.info(null_counts=True,verbose=True))
train_1<drop_column>
train_df.target.value_counts()
for df in dfs:
exec("del "+df)
 exec("del "+df+"_seniority_1")
 gc.collect()<drop_column>
bayesian_tr_index, bayesian_val_index = list(StratifiedKFold(n_splits=2, shuffle=True, random_state=1 ).split(train_df, train_df.target.values)) [0]
train_1.drop(features_to_discard,axis=1,inplace=True)
 train_1.info(null_counts=True,verbose=True )<load_pretrained>
def LGB_bayesian(
 num_leaves,
min_data_in_leaf,
learning_rate,
min_sum_hessian_in_leaf,
feature_fraction,
lambda_l1,
lambda_l2,
min_gain_to_split,
max_depth):
num_leaves = int(num_leaves)
 min_data_in_leaf = int(min_data_in_leaf)
 max_depth = int(max_depth)
 assert type(num_leaves)== int
assert type(min_data_in_leaf)== int
assert type(max_depth)== int
param = {
'num_leaves': num_leaves,
'max_bin': 63,
'min_data_in_leaf': min_data_in_leaf,
'learning_rate': learning_rate,
'min_sum_hessian_in_leaf': min_sum_hessian_in_leaf,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': feature_fraction,
'lambda_l1': lambda_l1,
'lambda_l2': lambda_l2,
'min_gain_to_split': min_gain_to_split,
'max_depth': max_depth,
'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,
}
xg_train = lgb.Dataset(train_df.iloc[bayesian_tr_index][predictors].values,
label=train_df.iloc[bayesian_tr_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train_df.iloc[bayesian_val_index][predictors].values,
label=train_df.iloc[bayesian_val_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 num_round = 5000
clf = lgb.train(param, xg_train, num_round, valid_sets = [xg_valid], verbose_eval=250, early_stopping_rounds = 50)
 predictions = clf.predict(train_df.iloc[bayesian_val_index][predictors].values, num_iteration=clf.best_iteration)
 score = metrics.roc_auc_score(train_df.iloc[bayesian_val_index][target].values, predictions)
 return score
create_directory(os.path.join(DATA_FOLDER, 'training'))
train_1.to_pickle(os.path.join(DATA_FOLDER,'training/train_1_pred.pkl'))<set_options>
bounds_LGB = {
'num_leaves':(5, 20),
'min_data_in_leaf':(5, 20),
'learning_rate':(0.01, 0.3),
'min_sum_hessian_in_leaf':(0.00001, 0.01),
'feature_fraction':(0.05, 0.5),
'lambda_l1':(0, 5.0),
'lambda_l2':(0, 5.0),
'min_gain_to_split':(0, 1.0),
'max_depth':(3,15),
}
del train_1
gc.collect()<define_variables>
from bayes_opt import BayesianOptimization
reset_variable_space<load_pretrained>
LGB_BO = BayesianOptimization(LGB_bayesian, bounds_LGB, random_state=13 )
train_2=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/train_2.pkl'))
print(train_2.info(null_counts=True))<define_variables>
init_points = 5
n_iter = 5
split_col='month_id'
target_col='item_quantity'
raw_features=[]
label_encoding=['shop_id',
'item_id',
'item_category_id',
'item_supercategory_id',
'item_category_console_id',
'item_category_is_digital'
]
mapping_columns=['item_month_id_of_last_sale',
'item_month_id_of_release',
'item_month_of_release',
'item_month_id_of_last_sale_in_shop',
'item_month_id_of_first_sale_in_shop'
]
features_to_discard=['item_id',
'shop_id',
'item_supercategory_id',
'item_category_console_id',
'item_category_is_digital'
]
features_to_discard+=mapping_columns
raw_features+=['shop_months_since_opening']
raw_features+=['item_freq_in_seniority']
raw_features+=['item_category_freq']
raw_features+=['item_supercategory_freq']
raw_features+=['item_category_console_freq']
raw_features+=['item_category_digital_freq']
raw_features+=['item_category_freq_in_seniority']
raw_features+=['item_supercategory_freq_in_seniority']
raw_features+=['item_category_console_freq_in_seniority']
raw_features+=['item_category_digital_freq_in_seniority']
raw_features+=['category_semiannual_avg_recent_rsd']
raw_features+=['category_semiannual_avg_annual_rsd']
raw_features+=['item_months_since_release']
raw_features+=['item_months_since_first_sale_in_shop']
raw_features+=['item_months_since_last_sale_in_shop']
train_2=train_2[[split_col]+label_encoding+mapping_columns+[target_col]+raw_features]
print(train_2.info(null_counts=True))
gc.collect()<load_pretrained>
print('-' * 130)
 with warnings.catch_warnings() :
warnings.filterwarnings('ignore')
 LGB_BO.maximize(init_points=init_points, n_iter=n_iter, acq='ucb', xi=0.0, alpha=1e-6 )
item_prices=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/price_features/item_prices.pkl'))
category_prices=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/price_features/category_prices.pkl'))<create_dataframe>
LGB_BO.probe(
 params={'feature_fraction': 0.1403,
'lambda_l1': 4.218,
'lambda_l2': 1.734,
'learning_rate': 0.07,
'max_depth': 14,
'min_data_in_leaf': 17,
'min_gain_to_split': 0.1501,
'min_sum_hessian_in_leaf': 0.000446,
'num_leaves': 6},
lazy=True,
)
ts = time.time()
price_features_df=[]
col_labels=['item_price_median']
col_labels+=['item_price_median_compared_to_category_price_median']
price_features_df.append(time_mapping(train_2,item_prices,['item_id'],col_labels,'item_month_id_of_last_sale',suffix='_last_sale'))
price_features_df.append(moving_statistics(train_2,item_prices,['month_id','item_id'],col_labels,34,'mean',suffix='_absolute_mean'))
time_window=34
price_features_df.append(moving_statistics(train_2,item_prices,['month_id','item_id'],['item_price_min'],34,'min',suffix='_absolute_min'))
price_features_df.append(moving_statistics(train_2,item_prices,['month_id','item_id'],['item_price_max'],34,'max',suffix='_absolute_max'))
print('time : ' +str(time.time() - ts))
price_features_df.append(moving_statistics(train_2,category_prices,['month_id','item_category_id'],['category_price_median'],3,'mean',suffix='_recent_mean'))
print('time : ' +str(time.time() - ts))
train_2=pd.concat([train_2]+price_features_df,axis=1,sort=False)
 del price_features_df
gc.collect()
print('time : ' +str(time.time() - ts))
train_2=downcast_dtypes(train_2)
 print('time : ' +str(time.time() - ts))
print(train_2.info(null_counts=True))
train_2<drop_column>
LGB_BO.maximize(init_points=0, n_iter=0 )
del item_prices
del category_prices
gc.collect()<define_variables>
for i, res in enumerate(LGB_BO.res):
print("Iteration {}:
\t{}".format(i, res))
dfs=['shop_encoding',
'item_encoding',
'category_encoding',
'supercategory_encoding',
'shop_category_encoding',
'shop_supercategory_encoding',
'category_months_since_release_encoding',
'supercategory_months_since_release_encoding',
'category_month_of_release_encoding',
'supercategory_month_of_release_encoding',
'category_months_since_first_sale_in_shop_encoding',
'supercategory_months_since_first_sale_in_shop_encoding',
'shop_category_months_since_release_encoding',
'shop_supercategory_months_since_release_encoding',
'shop_category_months_since_first_sale_in_shop_encoding',
'shop_supercategory_months_since_first_sale_in_shop_encoding'
]
for df in dfs:
exec(df+"=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/target_encodings/"+df+".pkl')) ")
 exec(df+"_seniority_2=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/target_encodings/"+df+"_seniority_2.pkl')) ")
 shop_item_encoding=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/target_encodings/shop_item_encoding.pkl'))<compute_test_metric>
param_lgb = {
'num_leaves': int(LGB_BO.max['params']['num_leaves']),
'max_bin': 63,
'min_data_in_leaf': int(LGB_BO.max['params']['min_data_in_leaf']),
'learning_rate': LGB_BO.max['params']['learning_rate'],
'min_sum_hessian_in_leaf': LGB_BO.max['params']['min_sum_hessian_in_leaf'],
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': LGB_BO.max['params']['feature_fraction'],
'lambda_l1': LGB_BO.max['params']['lambda_l1'],
'lambda_l2': LGB_BO.max['params']['lambda_l2'],
'min_gain_to_split': LGB_BO.max['params']['min_gain_to_split'],
'max_depth': int(LGB_BO.max['params']['max_depth']),
'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,
}
feature_correlation_analysis(train_2,item_encoding,item_encoding_seniority_2,'item',['month_id','item_id'],2)
 compare_features(item_encoding,item_encoding_seniority_2,'item',['month_id','item_id'],2,20,20,20 )<statistical_test>
nfold = 5
feature_correlation_analysis(train_2,supercategory_encoding,supercategory_encoding_seniority_2,'supercategory',['month_id','item_supercategory_id'],2)
 compare_features(supercategory_encoding,supercategory_encoding_seniority_2,'supercategory',['month_id','item_supercategory_id'],2,20,20,20 )<statistical_test>
gc.collect()
feature_correlation_analysis(train_2,shop_supercategory_encoding,shop_supercategory_encoding_seniority_2,'shop_supercategory',['month_id','shop_id','item_supercategory_id'],2)
 compare_features(shop_supercategory_encoding,shop_supercategory_encoding_seniority_2,'shop_supercategory',['month_id','shop_id','item_supercategory_id'],2,20,20,20 )<statistical_test>
skf = StratifiedKFold(n_splits=nfold, shuffle=True, random_state=2019 )
feature_correlation_analysis(train_2,category_months_since_release_encoding,category_months_since_release_encoding_seniority_2,'category_months_since_release',['month_id','item_category_id','item_months_since_release'],2)
 compare_features(category_months_since_release_encoding,category_months_since_release_encoding_seniority_2,'category_months_since_release',['month_id','item_category_id','item_months_since_release'],2,20,20,20 )<feature_engineering>
oof = np.zeros(len(train_df))
predictions = np.zeros(( len(test_df),nfold))
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_lgb, xg_train, 5000, valid_sets = [xg_valid], verbose_eval=250, early_stopping_rounds = 50)
 oof[valid_index] = clf.predict(train_df.iloc[valid_index][predictors].values, num_iteration=clf.best_iteration)
 predictions[:,i-1] += clf.predict(test_df[predictors], num_iteration=clf.best_iteration)
 i = i + 1
print("
CV AUC: {:<0.2f}".format(metrics.roc_auc_score(train_df.target.values, oof)) )
feature_correlation_analysis(train_2,category_months_since_first_sale_in_shop_encoding,category_months_since_first_sale_in_shop_encoding_seniority_2,'category_months_since_first_sale_in_shop',['month_id','item_category_id','item_months_since_first_sale_in_shop'],2)
 compare_features(category_months_since_first_sale_in_shop_encoding,category_months_since_first_sale_in_shop_encoding_seniority_2,'category_months_since_first_sale_in_shop',['month_id','item_category_id','item_months_since_first_sale_in_shop'],2,20,20,20 )<filter>
print("Rank averaging on", nfold, "fold predictions")
 rank_predictions = np.zeros(( predictions.shape[0],1))
for i in range(nfold):
rank_predictions[:, 0] = np.add(rank_predictions[:, 0], rankdata(predictions[:, i].reshape(-1,1)) /rank_predictions.shape[0])
 rank_predictions /= nfold
tmp=train_2[['month_id','shop_id','item_id','item_category_id','item_months_since_release','item_months_since_first_sale_in_shop']]
tmp2=category_months_since_release_encoding_seniority_2.loc[(category_months_since_release_encoding_seniority_2['month_id']>12)&(category_months_since_release_encoding_seniority_2['month_id']<34),['month_id','item_category_id','item_months_since_release','category_months_since_release_avg_sales_seniority_2','category_months_since_release_avg_sales_over_sold_seniority_2']]
tmp3=category_months_since_release_encoding.loc[(category_months_since_release_encoding['month_id']>12)&(category_months_since_release_encoding['month_id']<34),['month_id','item_category_id','item_months_since_release','category_months_since_release_avg_sales','category_months_since_release_avg_sales_over_sold']]
tmp=tmp.join(tmp2.set_index(['month_id','item_category_id','item_months_since_release']),on=['month_id','item_category_id','item_months_since_release'])
 tmp=tmp.join(tmp3.set_index(['month_id','item_category_id','item_months_since_release']),on=['month_id','item_category_id','item_months_since_release'])
 tmp4=category_months_since_first_sale_in_shop_encoding.loc[(category_months_since_first_sale_in_shop_encoding['month_id']>12)&(category_months_since_first_sale_in_shop_encoding['month_id']<34),['month_id','item_category_id','item_months_since_first_sale_in_shop','category_months_since_first_sale_in_shop_avg_sales','category_months_since_first_sale_in_shop_avg_sales_over_sold']]
tmp=tmp.join(tmp4.set_index(['month_id','item_category_id','item_months_since_first_sale_in_shop']),on=['month_id','item_category_id','item_months_since_first_sale_in_shop'])
 print(tmp[['item_months_since_release','item_months_since_first_sale_in_shop']].corr().loc['item_months_since_release',:])
 sns.heatmap(tmp.drop(['month_id','shop_id','item_id','item_category_id','item_months_since_release','item_months_since_first_sale_in_shop'],axis=1 ).corr() ,annot=True)
 del tmp, tmp2, tmp3, tmp4<compute_test_metric>
sub_df = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub_df["target"] = rank_predictions
sub_df[:10]
<define_variables><EOS> 
sub_df.to_csv("Customer_Transaction_rank_predictions.csv", index=False )