Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<remove_duplicates>
import os
import random
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
from sklearn.metrics import auc, roc_auc_score
from lightgbm import LGBMClassifier
import lightgbm as lgb
from hyperopt import hp, tpe, Trials, fmin
popular_games = X[(X.item_category_id==31)&(X.prev1>100)].sort_values(by = 'prev1', ascending = False ).item_id.drop_duplicates().head(10 ).to_list()<choose_model_class>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 submission = pd.read_csv(".. /input/sample_submission.csv" )
game = 3730 
<groupby>
np.sum(train['target'])/ len(train['target'] )
X[X.item_id==game].groupby('target_block_num',as_index = False)['prev1'].sum()<count_unique_values>
X_train, X_test, y_train, y_test = train_test_split(
 train.drop(['ID_code', 'target'], axis = 1),
train['target'],
test_size = 0.15,
random_state = 42,
stratify = train['target']
)
X[X.prev1>100].item_id.nunique()<merge>
np.sum(y_train)/ len(y_train), np.sum(y_test)/ len(y_test )
col = 'item_category_id'
for col in ['item_category_id','item_id','shop_id','city_id']:
X = X.merge(
 X.groupby(col, as_index=False)['prev1'].agg({col+'_mean':'mean',col+'_median':'median',col+'_std':'std'}),
on = col,
how = 'left'
)<define_variables>
def objective(params):
n_folds = 3
params = {
'n_estimators': int(params['n_estimators']),
'num_leaves': int(params['num_leaves']),
'learning_rate': float(params['learning_rate']),
'subsample_for_bin': int(params['subsample_for_bin']),
'min_child_samples': int(params['min_child_samples']),
'reg_alpha': float(params['reg_alpha']),
'reg_lambda': float(params['reg_lambda'])
 }
clf = LGBMClassifier(**params)
 score1 = cross_val_score(clf, X_train, y_train, scoring = 'roc_auc', cv = StratifiedKFold(n_splits = n_folds)).mean()
clf.fit(
 X_train, y_train,
eval_set = [(X_test, y_test)],
eval_metric = 'auc',
early_stopping_rounds = 3,
verbose = False, 
)
 score2 = roc_auc_score(y_test, clf.predict_proba(X_test)[:, 1])
 score = 2 /(( 1 / score1)+(1 / score2))
return 1 - score
ftr = [
'month',
'city_id',
'item_id',
'shop_id',
'date_block_num',
'prev1','prev2','prev3',
'item_category_id',
'mean_price',
'price_count',
'sold_total',
'is_internet',
'is_bag'
]<feature_engineering>
space = {
'n_estimators': hp.quniform('n_estimators', 50, 1500, 25),
'num_leaves': hp.quniform('num_leaves', 30, 150, 1),
'learning_rate': hp.loguniform('learning_rate', np.log(0.01), np.log(0.2)) ,
'subsample_for_bin': hp.quniform('subsample_for_bin', 20000, 300000, 20000),
'min_child_samples': hp.quniform('min_child_samples', 20, 500, 5),
'reg_alpha': hp.uniform('reg_alpha', 0.0, 1.0),
'reg_lambda': hp.uniform('reg_lambda', 0.0, 1.0)
 }
tpe_algo = tpe.suggest
tpe_trials = Trials()
for i in [x for x in ftr if x not in ['prev1','prev2','prev3']]:
X[i] = X[i].clip(0,np.percentile(X[i],99.9))<feature_engineering>
params = {
'bagging_freq': 5,
'bagging_fraction': 0.331,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.0405,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}
params = {
'num_leaves': 8,
'min_data_in_leaf': 42,
'objective': 'binary',
'max_depth': 16,
'learning_rate': 0.0123,
'boosting': 'gbdt',
'bagging_freq': 5,
'feature_fraction': 0.8201,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 4
}
lgb_base = LGBMClassifier(**params, n_estimators = 20000)
 lgb_base.fit(
 X_train, y_train,
eval_set = [(X_test, y_test)],
eval_metric = 'auc',
early_stopping_rounds = 200,
verbose = 1000,
)
for i in ['prev1','prev2','prev3']:
X[i] = X[i].clip(0,20 )<choose_model_class>
print("Training AUC:")
 print(roc_auc_score(y_train, lgb_base.predict_proba(X_train)[:, 1]))
print("
")
 print("Testing AUC:")
 print(roc_auc_score(y_test, lgb_base.predict_proba(X_test)[:, 1]))
lgbm = LGBMRegressor(random_state = 42,
max_depth = 8,
objective = 'rmse',
)<predict_on_test>
submission['target'] = 1 - lgb_base.predict_proba(test.drop('ID_code', axis = 1))
<compute_train_metric><EOS> 
submission.to_csv("submission_hyperparams_borrowed.csv", index = False )