Kaggle competition: Santander Customer Transaction Prediction
del train_0
gc.collect()<define_variables>
df = pd.read_csv(".. /input/train.csv")
 df.head()
reset_variable_space<load_pretrained>
df.isnull().any().any()
train_1=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/train_1.pkl'))
print(train_1.info(null_counts=True))<define_variables>
df['target'].value_counts(normalize=True )
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'
]
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+=['shop_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']
train_1=train_1[[split_col]+label_encoding+mapping_columns+[target_col]+raw_features]
print(train_1.info(null_counts=True))
gc.collect()<load_pretrained>
labels = df.pop('target')
 data = df.drop('ID_code', axis=1 )
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>
test_data = pd.read_csv(".. /input/test.csv")
 test_data.head()
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_1,item_prices,['item_id'],col_labels,'item_month_id_of_last_sale',suffix='_last_sale'))
price_features_df.append(moving_statistics(train_1,item_prices,['month_id','item_id'],col_labels,34,'mean',suffix='_absolute_mean'))
time_window=34
price_features_df.append(moving_statistics(train_1,item_prices,['month_id','item_id'],['item_price_min'],34,'min',suffix='_absolute_min'))
price_features_df.append(moving_statistics(train_1,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_1,category_prices,['month_id','item_category_id'],['category_price_median'],3,'mean',suffix='_recent_mean'))
print('time : ' +str(time.time() - ts))
train_1=pd.concat([train_1]+price_features_df,axis=1,sort=False)
 del price_features_df
gc.collect()
print('time : ' +str(time.time() - ts))
train_1=downcast_dtypes(train_1)
 print('time : ' +str(time.time() - ts))
print(train_1.info(null_counts=True))
train_1<drop_column>
test_data = test_data.drop('ID_code', axis=1)
 test_data.head()
del item_prices
del category_prices
gc.collect()<load_pretrained>
n_splits = 10
kf = KFold(n_splits=n_splits )
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',
'shop_category_months_since_release_encoding',
'shop_supercategory_months_since_release_encoding'
]
for df in dfs:
exec(df+"=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/target_encodings/"+df+".pkl')) ")
 exec(df+"_seniority_1=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/target_encodings/"+df+"_seniority_1.pkl')) " )<compute_test_metric>
warnings.filterwarnings('ignore')
 param = {'objective': 'binary', 'metric': 'auc', 'learning_rate': 0.01, 'num_rounds': 6000, 'verbose': 1}
test_pred = np.zeros(len(test_data))
for fold,(train_indx, val_indx)in enumerate(kf.split(labels)) :
print("Fold {}".format(fold+1))
train_set = lgb.Dataset(data.iloc[train_indx], label=labels.iloc[train_indx])
 val_set = lgb.Dataset(data.iloc[val_indx], label=labels.iloc[val_indx])
 model = lgb.train(param, train_set, valid_sets=val_set, verbose_eval=500)
 test_pred += model.predict(test_data)/n_splits
<statistical_test><EOS> 
save = pd.read_csv('.. /input/sample_submission.csv')
 save['target'] = test_pred
save.to_csv('LGB_kfold.csv', index=False)
 save.head()