Kaggle competition: Categorical Feature Encoding Challenge II
def distinguish_str(value_list):
output = []
regex = re.compile('[0-9]')
 for i,element in enumerate(value_list):
if regex.search(element):
output.append(float(element))
else:
output.append(element)
 return output<create_dataframe>
print(h2o.__version__)
 h2o.init(max_mem_size='16G' )
def model_gbm(train,train_label,test,test_ids,nfolds=5,hyperparameters=None):
feature_names = list(train.columns)
 valid_scores = np.zeros(len(train))
predictions = np.zeros(len(test))
feature_importance_df = pd.DataFrame()
max_iters_df = pd.DataFrame(columns=["folds","iters"])
 iters = []
folds = []
if hyperparameters:
params = hyperparameters
del(params['n_estimators'])
 params['boost_from_average'] = True
params['seed'] = 31452
params['feature_fraction_seed'] = 31415
params['bagging_seed'] = 31415
params['drop_seed'] = 31415
params['data_random_seed'] =31415
params['metric'] = 'auc'
else:
params = {
'num_leaves': 6,
'max_bin': 63,
'min_data_in_leaf': 45,
'learning_rate': 0.01,
'min_sum_hessian_in_leaf': 0.000446,
'bagging_fraction': 0.55,
'bagging_freq': 5,
'max_depth': 14,
'save_binary': True,
'seed': 31452,
'feature_fraction_seed': 31415,
'feature_fraction': 0.51,
'bagging_seed': 31415,
'drop_seed': 31415,
'data_random_seed': 31415,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False
}
strfkold = StratifiedKFold(n_splits=nfolds,shuffle=True,random_state=12)
 for i,(train_indices,valid_indices)in enumerate(strfkold.split(train.values,train_label.values)) :
print("{} fold processing".format(i+1)+"
d_train = lgb.Dataset(train.values[train_indices,:],label = train_label[train_indices])
 d_valid = lgb.Dataset(train.values[valid_indices,:],label = train_label[valid_indices])
 n_rounds = 15000
lgb_model = lgb.train(params,d_train,num_boost_round=n_rounds,valid_sets=[d_train,d_valid],valid_names=['train','valid'],verbose_eval=1000,early_stopping_rounds=250)
 valid_scores[valid_indices] = lgb_model.predict(train.values[valid_indices,:],num_iteration=lgb_model.best_iteration)
 fold_importance_df = pd.DataFrame(columns=["Feature","importance","fold"])
 fold_importance_df["Feature"] = feature_names
fold_importance_df["importance"] = lgb_model.feature_importance()
fold_importance_df["fold"] = i + 1
feature_importance_df = pd.concat([feature_importance_df,fold_importance_df],axis=0)
 folds.append(i+1)
 iters.append(lgb_model.best_iteration)
 predictions += lgb_model.predict(test.values,num_iteration=lgb_model.best_iteration)/nfolds
display("valid_set score is %f and best_iteration is %d of %d fold"%(roc_auc_score(train_label[valid_indices],valid_scores[valid_indices]),lgb_model.best_iteration,i+1))
max_iters_df["folds"] = folds
max_iters_df["iters"] = iters
display("CV score of valid_set for %d fold is %f and maximum of best_iteration is %d of %d fold"%(nfolds,roc_auc_score(train_label,valid_scores),max_iters_df['iters'].max() ,max_iters_df['iters'].idxmax() +1))
return valid_scores,predictions,feature_importance_df<import_modules>
%%time
train = h2o.import_file(".. /input/multi-cat-encodings/X_train_te.csv")
 test = h2o.import_file(".. /input/multi-cat-encodings/X_test_te.csv" )
from hyperopt import hp,tpe,Trials,fmin, STATUS_OK
from hyperopt.pyll.stochastic import sample<import_modules>
x = test.columns
y = 'target'
train[y] = train[y].asfactor()
import csv
import ast
from timeit import default_timer as timer<compute_test_metric>
aml = H2OAutoML(max_models=50, seed=47, max_runtime_secs=30000)
 aml.train(x=x, y=y, training_frame=train, fold_column='fold_column' )
def lgb_roc_auc(labels,predictions):
metric_value = roc_auc_score(labels,predictions)
 return 'roc_auc',metric_value,True<choose_model_class>
aml.leader
def objective(hyperparameters, nfold=5):
global ITERATION
ITERATION += 1
for parameter_name in ['max_depth','num_leaves','subsample_for_bin','min_child_samples','subsample_freq']:
hyperparameters[parameter_name] = int(hyperparameters[parameter_name])
 strkfold = StratifiedKFold(n_splits=nfold,shuffle=True)
 features = np.array(train)
 labels = np.array(train_label ).reshape(( -1))
valid_scores = []
best_estimators = []
run_times = []
model = lgb.LGBMClassifier(**hyperparameters,n_jobs=-1,metric='None',n_estimators=1000)
 for i,(train_indices,valid_indices)in enumerate(strkfold.split(features,labels)) :
print("
X_train,X_valid = features[train_indices],features[valid_indices]
y_train,y_valid = labels[train_indices], labels[valid_indices]
start = timer()
model.fit(X_train,y_train,early_stopping_rounds=50,
eval_metric=lgb_roc_auc,eval_set=[(X_train,y_train),(X_valid,y_valid)],
eval_names=['train','valid'],verbose=200)
 end = timer()
valid_scores.append(model.best_score_['valid']['roc_auc'])
 best_estimators.append(model.best_iteration_)
 run_times.append(end-start)
 score = np.mean(valid_scores)
 score_std = np.std(valid_scores)
 loss = 1-score
run_time = np.mean(run_times)
 run_time_std = np.std(run_times)
 estimators = int(np.mean(best_estimators))
hyperparameters['n_estimators'] = estimators
of_connection = open(OUT_FILE,'a')
 writer = csv.writer(of_connection)
 writer.writerow([loss,hyperparameters,ITERATION,run_time,score,score_std])
 of_connection.close()
display(f'Iteration: {ITERATION}, Score: {round(score, 4)}.')
 if ITERATION % PROGRESS == 0:
display(f'Iteration: {ITERATION}, Current Score: {round(score, 4)}.')
 return {'loss': loss, 'hyperparameters': hyperparameters, 'iteration': ITERATION,
'time': run_time, 'time_std': run_time_std, 'status': STATUS_OK,
'score': score, 'score_std': score_std}<init_hyperparams>
preds = aml.predict(test)
 preds['p1'].as_data_frame().values.flatten().shape
space = {
'boosting_type':'gbdt',
'objective':'binary',
'is_unbalance':True,
'subsample': hp.uniform('gbdt_subsample',0.5,1),
'subsample_freq':hp.quniform('gbdt_subsample_freq',1,10,1),
'max_depth': hp.quniform('max_depth',5,20,3),
'num_leaves': hp.quniform('num_leaves',20,60,10),
'learning_rate':hp.loguniform('learning_rate',np.log(0.025),np.log(0.25)) ,
'subsample_for_bin':hp.quniform('subsample_for_bin',2000,100000,2000),
'min_child_samples': hp.quniform('min_child_samples',5,80,5),
'colsample_bytree': hp.uniform('colsample_by_tree', 0.5, 1.0),
'min_child_weight':hp.uniform('min_child_weight',0.01,0.000001)
 }<find_best_params>
sample_submission = pd.read_csv('.. /input/cat-in-the-dat-ii/sample_submission.csv')
 sample_submission.shape
<save_to_csv><EOS> 
sample_submission['target'] = preds['p1'].as_data_frame().values
sample_submission.to_csv('h2o_automl_submission_4.csv', index=False )