【问题标题】:XGBoost Hyperparameter Tuning using Hyperopt使用 Hyperopt 进行 XGBoost 超参数调优
【发布时间】:2020-09-10 09:00:52
【问题描述】:

我正在尝试调整我的 XGBClassifier 模型。但我没有这样做。请在下面找到代码,请帮我清理和编辑代码。

    import csv
from hyperopt import STATUS_OK
from timeit import default_timer as timer
MAX_EVALS = 200
N_FOLDS = 10
def objective(params, n_folds = N_FOLDS):
    """Objective function for Gradient Boosting Machine Hyperparameter Optimization"""
    # Keep track of evals
    global ITERATION
    ITERATION += 1
    # Retrieve the subsample if present otherwise set to 1.0
    subsample = params['boosting_type'].get('subsample', 1.0)
    # Extract the boosting type
    params['boosting_type'] = params['boosting_type']['boosting_type']
    params['subsample'] = subsample
    # Make sure parameters that need to be integers are integers
    for parameter_name in ['num_leaves', 'subsample_for_bin', 
                          'min_child_samples']:
        params[parameter_name] = int(params[parameter_name])
    start = timer()
    # Perform n_folds cross validation
    cv_results = lgb.cv(params, train_set, num_boost_round = 10000, 
                       nfold = n_folds, early_stopping_rounds = 100, 
                       metrics = 'auc', seed = 50)
    run_time = timer() - start
    # Extract the best score
    best_score = np.max(cv_results['auc-mean'])
    # Loss must be minimized
    loss = 1 - best_score
    # Boosting rounds that returned the highest cv score
    n_estimators = int(np.argmax(cv_results['auc-mean']) + 1)
    # Write to the csv file ('a' means append)
    of_connection = open(out_file, 'a')
    writer = csv.writer(of_connection)
    writer.writerow([loss, params, ITERATION, n_estimators, 
                   run_time])
    # Dictionary with information for evaluation
    return {'loss': loss, 'params': params, 'iteration': ITERATION,
           'estimators': n_estimators, 'train_time': run_time, 
           'status': STATUS_OK}

我认为我在目标函数中做错了,因为我正在尝试编辑 LightGBM 的目标函数。

请帮帮我。

【问题讨论】:

  • 您的代码到底有什么问题?你有错误吗?如果是这样 - 什么错误?
  • 嗨 Stan0。你能给我推荐一个已经解决了 xgboost 和 hyperopt 的好代码吗?
  • 对不起,我根本不知道 - 我没有例子。我的意思是这样说你的问题可能会被关闭并且没有答案。 StackOverflow 的目标是解决一些更具体的问题。在此处查看更多详细信息:stackoverflow.com/help/on-topic

标签: machine-learning data-science xgboost hyperparameters hyperopt


【解决方案1】:

hgboost 库使用 Hyperopt 提供 XGBoost 超参数调整。

pip install hgboost

例子可以在here找到

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 2020-06-10
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多