Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<define_variables>
warnings.filterwarnings('ignore')
TRAIN_DATASIZE = 6_000_000
CHUNK_SIZE = 1_000_000
train_dtype = {
'key' : 'object',
'fare_amount' : 'float32',
'pickup_datetime' : 'str',
'pickup_longitude': 'float32',
'pickup_latitude' : 'float32',
'dropoff_longitude': 'float32',
'dropoff_latitude' : 'float32',
'passenger_count' : 'int16'
}
test_dtype = {
'key' : 'object',
'pickup_datetime' : 'str',
'pickup_longitude': 'float32',
'pickup_latitude' : 'float32',
'dropoff_longitude': 'float32',
'dropoff_latitude' : 'float32',
'passenger_count' : 'int16'
}
submission_dype = {
'key' : 'object',
'fare_amount' : 'float32'
}<load_from_csv>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv" )
def read_csv(path, dtype, chunksize = None, totalsize = None):

 if chunksize != None:
data_lst = []
reader = pd.read_csv(TRAIN_PATH, dtype = dtype, chunksize = chunksize)
 index = 1
for df_data in reader:
data_lst.append(df_data)
 del df_data
print('Rows read so far {0} M'.format(index * chunksize /(1e6)))
 if index * chunksize >= totalsize:
break
index = index + 1
df_data = pd.concat(data_lst)
 del data_lst
elif totalsize == None:
df_data = pd.read_csv(path, dtype = dtype)
 else:
df_data = pd.read_csv(path, dtype = dtype, nrows= totalsize)
 print('data ' + path + ' is read')
 return df_data<load_from_csv>
train_cols = [c for c in train_df.columns if c not in ["ID_code", "target"]]
y_train = train_df["target"]
%%time
train = read_csv(path = TRAIN_PATH, dtype = train_dtype, chunksize = CHUNK_SIZE, totalsize = TRAIN_DATASIZE)
 test = read_csv(path = TEST_PATH, dtype = test_dtype)
 submission = read_csv(path = SAMPLE_SUBMISSION_PATH, dtype = submission_dype )<drop_column>
y_train.value_counts()
for df in [train, test]:
df.drop('key', axis = 1, inplace = True)
 train.shape, test.shape,<define_variables>
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1001 )
str(100 * len(train[train.passenger_count.between(1, 6)])/ len(train)) + ' %'<define_variables>
params = {'tree_method': 'hist',
'objective': 'binary:logistic',
'eval_metric': 'auc',
'learning_rate': 0.0936165921314771,
'max_depth': 2,
'colsample_bytree': 0.3561271102144279,
'subsample': 0.8246604621518232,
'min_child_weight': 53,
'gamma': 9.943467991283027,
'silent': 1}
train = train[train.passenger_count.between(1,6)]<define_variables>
%%time
oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(train_df, y_train)) :
trn_x, trn_y = train_df[train_cols].iloc[trn_idx], y_train.iloc[trn_idx]
val_x, val_y = train_df[train_cols].iloc[val_idx], y_train.iloc[val_idx]
dtrain = xgb.DMatrix(trn_x, trn_y, feature_names=trn_x.columns)
 dval = xgb.DMatrix(val_x, val_y, feature_names=val_x.columns)
 clf = xgb.train(params=params, dtrain=dtrain, num_boost_round=4000, evals=[(dtrain, "Train"),(dval, "Val")],
verbose_eval= 100, early_stopping_rounds=50)
 oof_preds[val_idx] = clf.predict(xgb.DMatrix(val_x))
sub_preds += clf.predict(xgb.DMatrix(test_df[train_cols])) / folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = pd.DataFrame.from_dict(data=clf.get_fscore() , orient="index", columns=["FScore"])["FScore"].index
fold_importance_df["fscore"] = pd.DataFrame.from_dict(data=clf.get_fscore() , orient="index", columns=["FScore"])["FScore"].values
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('
Fold %1d AUC %.6f & std %.6f' %(n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx]), np.std([oof_preds[val_idx]])))
 print('Fold %1d Precision %.6f' %(n_fold + 1, precision_score(val_y, np.round(oof_preds[val_idx]))))
print('Fold %1d Recall %.6f' %(n_fold + 1, recall_score(val_y, np.round(oof_preds[val_idx]))))
print('Fold %1d F1 score %.6f' %(n_fold + 1,f1_score(val_y, np.round(oof_preds[val_idx]))))
print('Fold %1d Kappa score %.6f
' %(n_fold + 1,cohen_kappa_score(val_y, np.round(oof_preds[val_idx]))))
gc.collect()
print('
CV AUC score %.6f & std %.6f' %(roc_auc_score(y_train, oof_preds), np.std(( oof_preds))))
print('CV Precision score %.6f' %(precision_score(y_train, np.round(oof_preds))))
print('CV Recall score %.6f' %(recall_score(y_train, np.round(oof_preds))))
print('CV F1 score %.6f' %(f1_score(y_train, np.round(oof_preds))))
print('CV Kappa score %.6f' %(cohen_kappa_score(y_train, np.round(oof_preds))))
percentile_99_73 = train.fare_amount.quantile(0.9973)
 percentile_99_73<define_variables>
print(confusion_matrix(y_train, np.round(oof_preds)) )
train = train[train.fare_amount.between(2.5, 60)]<define_variables>
feature_importance_df.groupby(["feature"])["fscore",].mean().sort_values("fscore", ascending=False )
str(100 * len(train[train.pickup_latitude.between(40.5, 40.9)])/ len(train)) + ' %'<feature_engineering>
test_df['target'] = sub_preds
str(100 * len(train[train.dropoff_latitude.between(40.5, 40.9)])/ len(train)) + ' %'<define_variables>
oof_roc = roc_auc_score(y_train, oof_preds)
 oof_roc
<feature_engineering><EOS> 
ss = pd.DataFrame({"ID_code":test_df["ID_code"], "target":test_df["target"]})
 ss.to_csv("sant_xgb_%sFold_%.6f.csv"%(folds.n_splits, oof_roc), index=None)
 ss.head()