Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<define_variables>
%matplotlib inline
print(os.listdir(".. /input"))
h2o.init()
train_df = pd.read_csv(".. /input/train.csv")
 valid_rate =.15
train_df, valid_df, tr_y, va_y = train_test_split(train_df, train_df['target'], stratify = train_df['target'], test_size=valid_rate, random_state = 42)
 train = h2o.H2OFrame(train_df)
 valid = h2o.H2OFrame(valid_df)
 test = h2o.import_file(".. /input/test.csv")
 del train_df, valid_df, tr_y, va_y
gc.collect()
features_to_keep_0=[]
features_to_keep_0+=['item_category_id']
features_to_keep_0+=['item_category_freq']
features_to_keep_0+=['item_supercategory_freq']
features_to_keep_0+=['item_category_console_freq']
features_to_keep_0+=['item_category_freq_in_seniority']
features_to_keep_0+=['item_supercategory_freq_in_seniority']
features_to_keep_0+=['item_category_console_freq_in_seniority']
features_to_keep_0+=['shop_category_price_median_absolute_mean']
features_to_keep_0+=['shop_category_price_median_lag_12']
features_to_keep_0+=['category_price_median_recent_mean']
features_to_keep_0+=['category_price_median_lag_12']
features_to_keep_0+=['supercategory_price_median_lag_12']
features_to_keep_0+=['category_price_min_absolute_min']
features_to_keep_0+=['category_price_max_absolute_max']
features_to_keep_0+=['category_price_min_annual_min']
features_to_keep_0+=['category_price_max_annual_max']
features_to_keep_0+=['category_price_median_compared_to_supercategory_price_median_absolute_mean']
features_to_keep_0+=['shop_avg_sales_seniority_0_semiannual_mean']
features_to_keep_0+=['supercategory_avg_sales_seniority_0_semiannual_mean']
features_to_keep_0+=['supercategory_avg_sales_seniority_0_lag_12']
features_to_keep_0+=['shop_supercategory_avg_sales_seniority_0_annual_mean']
features_to_keep_0+=['shop_supercategory_avg_sales_seniority_0_lag_12']
features_to_keep_0+=['category_avg_sales_seniority_0_annual_mean']
features_to_keep_0+=['category_avg_sales_seniority_0_semiannual_mean']
features_to_keep_0+=['shop_category_avg_sales_seniority_0_annual_mean']
features_to_keep_0+=['shop_category_avg_sales_seniority_0_semiannual_mean']
features_to_keep_0+=['shop_category_avg_sales_seniority_0_recent_mean']
features_to_keep_0+=['shop_category_avg_sales_seniority_0_lag_1']
features_to_keep_0+=['shop_category_avg_sales_seniority_0_lag_12']
features_to_keep_0+=['shop_category_avg_sales_compared_to_category_annual_mean']
features_to_keep_0+=['shop_category_avg_sales_compared_to_category_recent_mean']
features_to_keep_0+=['shop_category_avg_sales_compared_to_category_lag_12']
features_to_keep_0+=['shop_supercategory_avg_sales_compared_to_supercategory_recent_mean']
features_to_keep_0+=['supercategory_avg_sales_compare_recent_mean_to_annual_mean']
features_to_keep_0+=['supercategory_avg_sales_compare_lag12_to_annual_mean']
features_to_keep_0+=['category_avg_sales_compare_lag12_to_annual_mean']
features_to_keep_0+=['category_avg_sales_compared_to_supercategory_recent_mean']
features_to_keep_0+=['supercategory_avg_sales_seniority_0_compare_lag12_to_annual_mean'] 
<split>
y = 'target'
x = train.columns[2:]
train[y] = train[y].asfactor()
features_to_discard_0=list(set(list(train_X.columns)) - set(['month_id','item_quantity']+features_to_keep_0))
train_X.drop(features_to_discard_0,axis=1,inplace=True)
 train_X.fillna(-1,inplace=True)
(X_train,Y_train,X_val,Y_val)= datasplit_train_val(train_X,n_months_val,month_id_first)
 eval_set = [(X_train,Y_train),(X_val,Y_val)]+datasplit_evalset(train_X,month_id_first)
 print('number of features to keep : '+str(len(features_to_keep_0)))
 print('number of features kept : '+str(len(X_train.columns)))
 print(X_train.info(null_counts=True,verbose=True))
del train_X
gc.collect()<init_hyperparams>
n_models = 8
grid_params = {
'max_depth': [2, 3],
'col_sample_rate': [.6,.7],
'learn_rate': [.09,.1],
'learn_rate_annealing': [1],
'min_rows': [110, 90],
'sample_rate': [.7]
}
gbm_grid = H2OGridSearch(model=h2o.estimators.H2OGradientBoostingEstimator,
grid_id='gbm_grid',
hyper_params=grid_params,
search_criteria={'strategy': 'RandomDiscrete', 'max_models': n_models})
xgb_params_0={'objective': 'reg:squarederror',
'max_depth': 4,
'min_child_weight': 1e3,
'subsample': 0.8,
'colsample_bytree': 0.8,
'learning_rate': 0.01,
'n_estimators': 300,
'base_score': 0.6,
'n_jobs': 4,
'random_state': 2
}
ts=time.time()
xgbreg=XGBRegressor(**xgb_params_0)
 xgbreg.fit(X_train,Y_train,eval_set=eval_set,eval_metric='rmse',verbose=False)
 print(time.time() -ts )<drop_column>
gbm_grid.train(x=x, y=y, training_frame=train, validation_frame=valid,
distribution='bernoulli',
ntrees=2500,
score_tree_interval = 20,
stopping_rounds = 4,
stopping_metric = "AUC",
stopping_tolerance = 1e-4,
seed = 1 )
del X_train, Y_train, X_val, Y_val
del eval_set
del rmse_guess_train, rmse_base_train, rmse_guess_val, rmse_base_val, rmse_guess_evalset, rmse_base_evalset
del xgbreg
gc.collect()<define_variables>
gridperf = gbm_grid.get_grid(sort_by='auc', decreasing=True)
 best_model = gridperf.models[0]
for par in grid_params:
if par in best_model.params:
print('par: ' + par); print(best_model.params[par])
reset_variable_space<load_pretrained>
history = pd.DataFrame(best_model.scoring_history())
 history.plot(x='number_of_trees', y = ['validation_auc', 'validation_pr_auc'])
<split><EOS> 
preds = best_model.predict(test)
 submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = preds['p1'].as_data_frame()
submission.to_csv('gbm_submission.csv', index = False)
 submission.head()