我正在使用 Python 的 hyperopt 库来执行 ML 超参数的优化。特别是我正在尝试使用此函数找到 lightgbm 最佳超参数以最小化:
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
hyperopt 调用是:
best = fmin(fn=lgb_objective_map,
space=lgb_parameter_space,
algo=tpe.suggest,
max_evals=200,
trials=trials)
是否可以修改best
调用以将补充参数传递给lgb_objective_map
like as lgbtrain, X_test, y_test
?这将允许概括对 hyperopt 的调用。