Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<drop_column>
import os
import gc
import pickle
from pathlib import Path
import numpy as np
import pandas as pd
import random
from sklearn.preprocessing import minmax_scale
from sklearn.model_selection import GridSearchCV, StratifiedKFold
for df in dfs:
exec("del "+df)
 exec("del "+df+"_seniority_2")
 del shop_item_encoding
gc.collect()<drop_column>
MODEL_NAME = "santander_2"
RESCALE = False
MIN_MAX =(-1,1 )
train_2.drop(features_to_discard,axis=1,inplace=True)
 train_2.info(null_counts=True,verbose=True )<load_pretrained>
input_path = Path(".. /input")
 train_csv = str(input_path / "train.csv")
 test_csv = str(input_path / "test.csv" )
create_directory(os.path.join(DATA_FOLDER, 'training'))
train_2.to_pickle(os.path.join(DATA_FOLDER,'training/train_2_pred.pkl'))<set_options>
train_df = pd.read_csv(train_csv)
 test_df = pd.read_csv(test_csv)
 train_df.head()
del train_2
gc.collect()<define_variables>
feature_columns = []
for i in range(0,200,1):
key = 'var_' + str(i)
 feature_columns.append(key)
 if RESCALE:
test_df[key] = minmax_scale(test_df[key].values.astype(np.float32), feature_range=MIN_MAX, axis=0)
 train_df[key] = minmax_scale(train_df[key].values.astype(np.float32), feature_range=MIN_MAX, axis=0)
 if RESCALE:
train_df.head()
reset_variable_space<prepare_x_and_y>
def build_model() :
model = CatBoostClassifier(loss_function="CrossEntropy",
eval_metric="AUC",
learning_rate=0.01,
iterations=40000,
random_seed=42,
od_type="Iter",
depth=8,
border_count=32,
early_stopping_rounds=700,
task_type = "GPU",
logging_level='Verbose')
 return model
def datasplit_train_val(train_df,n_months_val=1,month_id_first=0):
if month_id_first==0:
month_id_first=train_df['month_id'].min()
month_id_split=train_df['month_id'].max() -(n_months_val-1)
 X_train=train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<month_id_split),:].astype(np.float32)
 Y_train=X_train['item_quantity'].astype(np.float32)
 X_train.drop(['month_id','item_quantity'],axis=1,inplace=True)
 print(X_train.shape, Y_train.shape)
 X_val=train_df.loc[train_df['month_id']>=month_id_split,:].astype(np.float32)
 Y_val=X_val['item_quantity'].astype(np.float32)
 X_val.drop(['month_id','item_quantity'],axis=1,inplace=True)
 print(X_val.shape , Y_val.shape)
 return(X_train,Y_train,X_val,Y_val)
 def datasplit_train_test(train_df,month_id_first=0):
if month_id_first==0:
month_id_first=train_df['month_id'].min()
X_train=train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),:].astype(np.float32)
 Y_train=X_train['item_quantity'].astype(np.float32)
 X_train.drop(['month_id','item_quantity'],axis=1,inplace=True)
 print(X_train.shape, Y_train.shape)
 X_test=train_df.loc[train_df['month_id']==34,:].astype(np.float32)
 X_test.drop(['month_id','item_quantity'],axis=1,inplace=True)
 print(X_test.shape)
 return(X_train,Y_train,X_test)
 def datasplit_evalset(train_df,month_id_first=0):
if month_id_first==0:
month_id_first=train_df['month_id'].min()
eval_set=[]
for mid in train_df['month_id'].unique() :
if(mid>=month_id_first)&(mid<34):
X_val=train_df.loc[(train_df['month_id']==mid),:].astype(np.float32)
 Y_val=X_val['item_quantity'].astype(np.float32)
 X_val.drop(['month_id','item_quantity'],axis=1,inplace=True)
 eval_set.append(( X_val,Y_val))
return eval_set<split>
FOLDS = 5
X = train_df[feature_columns].values
y = np.array(train_df['target'].values)
 splits = list(StratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=42 ).split(X, y))
models = []
for idx,(train_idx, val_idx)in enumerate(splits):
print("Beginning fold {}".format(idx+1))
train_X, train_y, val_X, val_y = X[train_idx], y[train_idx], X[val_idx], y[val_idx]
model = build_model()
model.fit(train_X, train_y, eval_set=(val_X,val_y))
models.append(model)
def target_analysis_val(train_df,n_months_val=1,month_id_first=0): 
(X_train,Y_train,X_val,Y_val)= datasplit_train_val(train_df,n_months_val,month_id_first)
 evalset = datasplit_evalset(train_df,month_id_first)
 base_score=Y_train.mean()
Y_guess_train=0*Y_train
rmse_guess_train=np.sqrt(mean_squared_error(Y_train, Y_guess_train))
Y_base_train=0*Y_train+base_score
rmse_base_train=np.sqrt(mean_squared_error(Y_train, Y_base_train))
print('
print('mean target value: '+str(base_score))
print('RMSE(guess,base): '+str(rmse_guess_train)+' , '+str(rmse_base_train))
base_score_val=Y_val.mean()
Y_guess_val=0*Y_val
rmse_guess_val=np.sqrt(mean_squared_error(Y_val, Y_guess_val))
Y_base_val=0*Y_val+base_score_val
rmse_base_val=np.sqrt(mean_squared_error(Y_val, Y_base_val))
print('
print('mean target value: '+str(base_score_val))
print('RMSE(guess,base): '+str(rmse_guess_val)+' , '+str(rmse_base_val))
base_score_evalset=[Y.mean() for(_,Y)in evalset]
rmse_guess_evalset=[]
rmse_base_evalset=[]
for(_,Y)in evalset:
Y_guess=0*Y
rmse_guess_evalset.append(np.sqrt(mean_squared_error(Y, Y_guess)))
 Y_base=0*Y+Y.mean()
rmse_base_evalset.append(np.sqrt(mean_squared_error(Y, Y_base)))
 print('
print('mean target value: '+str([round(b*1e4)/1e4 for b in base_score_evalset]))
print('RMSE(guess): '+str([round(b*1e2)/1e2 for b in rmse_guess_evalset]))
print('RMSE(base): '+str([round(b*1e2)/1e2 for b in rmse_base_evalset]))
fig,axes=plt.subplots(1,2,figsize=(15,5))
axes[0].plot(train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),'month_id'].unique() ,base_score_evalset,'-o')
 axes[0].set_xlabel('month_id')
 axes[0].set_ylabel('mean of target value')
 axes[0].grid(True)
 axes[0].set_ylim(bottom=0)
 axes[1].plot(train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),'month_id'].unique() ,rmse_guess_evalset,'-o')
 axes[1].plot(train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),'month_id'].unique() ,rmse_base_evalset,'-o')
 axes[1].set_xlabel('month_id')
 axes[1].set_ylabel('rmse')
 axes[1].legend(['guess = 0','mean value'])
 axes[1].grid(True)
 axes[1].set_ylim(bottom=0)
 return rmse_guess_train, rmse_base_train, rmse_guess_val, rmse_base_val, rmse_guess_evalset, rmse_base_evalset
def target_analysis_test(train_df,month_id_first=0): 
(X_train,Y_train,_)= datasplit_train_test(train_df,month_id_first)
 evalset = datasplit_evalset(train_df,month_id_first)
 base_score=Y_train.mean()
base_score_evalset=[Y.mean() for(_,Y)in evalset]
Y_guess_train=0*Y_train
rmse_guess_train=np.sqrt(mean_squared_error(Y_train, Y_guess_train))
Y_base_train=0*Y_train+base_score
rmse_base_train=np.sqrt(mean_squared_error(Y_train, Y_base_train))
print('
print('mean target value: '+str(base_score))
print('RMSE(guess,base): '+str(rmse_guess_train)+' , '+str(rmse_base_train))
rmse_guess_evalset=[]
rmse_base_evalset=[]
for(_,Y)in evalset:
Y_guess=0*Y
rmse_guess_evalset.append(np.sqrt(mean_squared_error(Y, Y_guess)))
 Y_base=0*Y+Y.mean()
rmse_base_evalset.append(np.sqrt(mean_squared_error(Y, Y_base)))
 print('
print('mean target value: '+str([round(b*1e4)/1e4 for b in base_score_evalset]))
print('RMSE(guess): '+str([round(b*1e2)/1e2 for b in rmse_guess_evalset]))
print('RMSE(base): '+str([round(b*1e2)/1e2 for b in rmse_base_evalset]))
fig,axes=plt.subplots(1,2,figsize=(15,5))
axes[0].plot(train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),'month_id'].unique() ,base_score_evalset,'-o')
 axes[0].set_xlabel('month_id')
 axes[0].set_ylabel('mean of target value')
 axes[0].grid(True)
 axes[0].set_ylim(bottom=0)
 axes[1].plot(train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),'month_id'].unique() ,rmse_guess_evalset,'-o')
 axes[1].plot(train_df.loc[(train_df['month_id']>=month_id_first)&(train_df['month_id']<34),'month_id'].unique() ,rmse_base_evalset,'-o')
 axes[1].set_xlabel('month_id')
 axes[1].set_ylabel('rmse')
 axes[1].legend(['guess = 0','mean value'])
 axes[1].grid(True)
 axes[1].set_ylim(bottom=0)
 return rmse_guess_train, rmse_base_train, rmse_guess_evalset, rmse_base_evalset<compute_train_metric>
preds_test = []
X = test_df[feature_columns].values
for i in range(FOLDS):
pred = models[i].predict_proba(X)
 preds_test.append(pred)
def performance_analysis_val(xgbreg,rmse_guess_train,rmse_base_train,rmse_guess_val,rmse_base_val,rmse_guess_evalset,rmse_base_evalset):
rmse_evalset_last=[]
rmse_evalset_min=[]
for evset in xgbreg.evals_result().values() :
rmse_evalset_last.append(evset['rmse'][-1])
 rmse_evalset_min.append(min(evset['rmse']))
print('
print('RMSE(last, min, base, guess): '+str(rmse_evalset_last[0])+' , '+str(rmse_evalset_min[0])+' , '+str(rmse_base_train)+' , '+str(rmse_guess_train))
print()
print('
print('RMSE(last, min, base, guess): '+str(rmse_evalset_last[1])+' , '+str(rmse_evalset_min[1])+' , '+str(rmse_base_val)+' , '+str(rmse_guess_val))
print()
print('
print('RMSE(last): '+str([round(b*1e4)/1e4 for b in rmse_evalset_last[2:]]))
print('RMSE(min): '+str([round(b*1e4)/1e4 for b in rmse_evalset_min[2:]]))
print('RMSE(guess): '+str([round(b*1e4)/1e4 for b in rmse_guess_evalset]))
print('RMSE(base): '+str([round(b*1e4)/1e4 for b in rmse_base_evalset]))
fig,axes=plt.subplots(1,2,figsize=(15,5))
for i in range(2,len(xgbreg.evals_result())) :
evset=xgbreg.evals_result() ['validation_'+str(i)]['rmse']
axes[0].plot(evset,'-o')
 axes[0].legend(range(0,len(eval_set)))
 axes[0].plot(xgbreg.evals_result() ['validation_0']['rmse'],'k',linewidth=5)
 axes[0].plot(xgbreg.evals_result() ['validation_1']['rmse'],'k--',linewidth=5)
 axes[1].plot(rmse_evalset_last[2:],'-o')
 axes[1].plot(0*np.array(rmse_evalset_last[2:])+rmse_evalset_last[0],'k')
 axes[1].plot(0*np.array(rmse_evalset_last[2:])+rmse_evalset_last[1],'k--')
 axes[1].plot(rmse_evalset_min[2:],'-o')
 axes[1].plot(0*np.array(rmse_evalset_min[2:])+rmse_evalset_last[0],'r')
 axes[1].plot(0*np.array(rmse_evalset_min[2:])+rmse_evalset_last[1],'r--')
 axes[1].plot(rmse_guess_evalset,'k-s')
 axes[1].set_xlabel('month_id')
 axes[1].set_ylabel('rmse(last,min)')
 axes[1].grid(True)
 axes[1].set_ylim(bottom=0)
 def performance_analysis_test(xgbreg,rmse_guess_train,rmse_base_train,rmse_guess_evalset,rmse_base_evalset):
rmse_evalset_last=[]
rmse_evalset_min=[]
for evset in xgbreg.evals_result().values() :
rmse_evalset_last.append(evset['rmse'][-1])
 rmse_evalset_min.append(min(evset['rmse']))
print('
print('RMSE(last, min, base, guess): '+str(rmse_evalset_last[0])+' , '+str(rmse_evalset_min[0])+' , '+str(rmse_base_train)+' , '+str(rmse_guess_train))
print()
print('
print('RMSE(last): '+str([round(b*1e4)/1e4 for b in rmse_evalset_last[1:]]))
print('RMSE(min): '+str([round(b*1e4)/1e4 for b in rmse_evalset_min[1:]]))
print('RMSE(base): '+str([round(b*1e4)/1e4 for b in rmse_base_evalset]))
print('RMSE(guess): '+str([round(b*1e4)/1e4 for b in rmse_guess_evalset]))
fig,axes=plt.subplots(1,2,figsize=(15,5))
for i in range(1,len(xgbreg.evals_result())) :
evset=xgbreg.evals_result() ['validation_'+str(i)]['rmse']
axes[0].plot(evset,'-o')
 axes[0].legend(range(0,len(eval_set)))
 axes[0].plot(xgbreg.evals_result() ['validation_0']['rmse'],'k',linewidth=5)
 axes[1].plot(rmse_evalset_last[1:],'-o')
 axes[1].plot(0*np.array(rmse_evalset_last[1:])+rmse_evalset_last[0],'k')
 axes[1].plot(rmse_evalset_min[1:],'-o')
 axes[1].plot(0*np.array(rmse_evalset_min[1:])+rmse_evalset_last[0],'r')
 axes[1].plot(rmse_guess_evalset,'k-s')
 axes[1].set_xlabel('month_id')
 axes[1].set_ylabel('rmse(last,min)')
 axes[1].grid(True)
 axes[1].set_ylim(bottom=0 )<set_options>
preds = np.mean(np.array(preds_test),axis=0 )
loaded=%who_ls
loaded.append('loaded' )<load_pretrained>
submission_df = pd.DataFrame(data={'ID_code': list(test_df['ID_code'].values), 'target': list(preds)})
 submission_df['target'] = submission_df['target'].apply(lambda x: float(x[0])).astype('float')
 submission_df[submission_df['target'] > 0.5].head(10)
<split><EOS> 
submission_df.to_csv('submission.csv', index=False )