I am using Python's hyperopt library to perform ML hyperparameters' optimization. In particular I am trying to find lightgbm optimal hyperparameter using this function to minimize:
def lgb_objective_map(params):
"""
objective function for lightgbm using MAP as success metric.
"""
# hyperopt casts as float
params['num_boost_round'] = int(params['num_boost_round'])
params['num_leaves'] = int(params['num_leaves'])
params['min_data_in_leaf'] = int(params['min_data_in_leaf'])
# need to be passed as parameter
params['verbose'] = -1
params['seed'] = 1
# Cross validation
cv_result = lgb.cv(
params,
lgtrain,
nfold=3,
metrics='binary_logloss',
num_boost_round=params['num_boost_round'],
early_stopping_rounds=20,
stratified=False,
)
# Update the number of trees based on the early stopping results
early_stop_dict[lgb_objective_map.i] = len(cv_result['binary_logloss-mean'])
params['num_boost_round'] = len(cv_result['binary_logloss-mean'])
# fit and predict
#model = lgb.LGBMRegressor(**params)
#model.fit(train,y_train,feature_name=all_cols,categorical_feature=cat_cols)
model= lgb.train(params=params,train_set=lgtrain)
preds = model.predict(X_test)
# add a column with predictions and rank
result = log_loss(y_test,preds)
# actual_predicted
actual_predicted = np.sum(y_test)/np.sum(preds)
print("INFO: iteration {} logloss {:.3f} actual on predicted ratio {:.3f}".format(lgb_objective_map.i,
result,actual_predicted))
lgb_objective_map.i+=1
return result
The hyperopt call is:
best = fmin(fn=lgb_objective_map,
space=lgb_parameter_space,
algo=tpe.suggest,
max_evals=200,
trials=trials)
Is is possible to modify the best
call in order to pass supplementary parameter to lgb_objective_map
like as lgbtrain, X_test, y_test
? This would allow to generalize the call to hyperopt.