首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Hyperopt调整XGBoost超参数

使用Hyperopt调整XGBoost超参数
EN

Stack Overflow用户
提问于 2020-05-23 22:14:59
回答 1查看 232关注 0票数 0

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

代码语言:javascript
复制
    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的目标函数。

请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2020-09-01 03:58:12

hgboost库提供了使用Hyperopt超参数调优的XGBoost。

代码语言:javascript
复制
pip install hgboost

示例可以在here中找到

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61973342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档