Xgboost系列-XGB实际参数调优指南附源码

 Xgboost模型在机器学习、深度学习中经久不衰,不论是分类还是回归任务都是一个不错的baseline甚至最终可用的模型,XGB对任务的普适性也决定了其具有大量的可调节参数,针对同一个任务,不同的参数设置可能带来不同甚至相差甚远的性能结果,因为寻找当前任务下可用、有效的参数是一个必不可少的过程,在上一篇文章XGB系列-XGB参数指南_wwlsm_zql的博客-CSDN博客在运行 XGBoost 之前,我们必须设置三种类型的参数: 通用参数、提升参数和任务参数。本文提供了对XGB模型的全部参数的介绍,用于指导对参数的选择https://blog.csdn.net/wwlsm_zql/article/details/126192959介绍了XGB的所有参数,针对如果繁多的参数,试探枚举是一个非常庞大的工作量,因此本文介绍通过hyperopt实现自动参数寻优,找到适合自己任务的最佳参数。

代码链接:colab代码https://colab.research.google.com/drive/1dm3Bk0VlEuBed8FMMeoZGWUR8xY84Ho9#scrollTo=ILPR3vXWdAvY

安装依赖的包

!pip install xgboost sklearn hyperopt

导入基本库

# 导入基本包
import pandas as pd
import numpy as np
import xgboost as xgb
from sklearn.metrics import accuracy_score
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe
from sklearn.model_selection import train_test_split

加载数据,并拆分

df = pd.read_csv("drive/MyDrive/data_daily/Wholesalecustomersdata.csv")

x = df.drop('Channel', axis=1)
y = df['Channel']
"""将分类任务转换为0-1"""
y[y == 2] = 0
y[y == 1] = 1

"""切分数据集"""
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)

使用优化器进行参数寻优

  1. 定义参数空间,指定参数的所有候选空间
  2. 定义训练过程和评估的目标(损失函数)
  3. 执行寻优过程
  4. 获取最优的参数组合

初始化参数空间

The available hyperopt optimization algorithms are -

  • hp.choice(label, options) — Returns one of the options, which should be a list or tuple.

  • hp.randint(label, upper) — Returns a random integer between the range [0, upper).

  • hp.uniform(label, low, high) — Returns a value uniformly between low and high.

  • hp.quniform(label, low, high, q) — Returns a value round(uniform(low, high) / q) * q, i.e it rounds the decimal values and returns an integer.

  • hp.normal(label, mean, std) — Returns a real value that’s normally-distributed with mean and standard deviation sigma.

space={'max_depth': hp.quniform("max_depth", 3, 18, 1),
        'gamma': hp.uniform ('gamma', 1,9),
        'reg_alpha' : hp.quniform('reg_alpha', 40,180,1),
        'reg_lambda' : hp.uniform('reg_lambda', 0,1),
        'colsample_bytree' : hp.uniform('colsample_bytree', 0.5,1),
        'min_child_weight' : hp.quniform('min_child_weight', 0, 10, 1),
        'n_estimators': 180,
        'seed': 0
    }

定义优化目标

def objective(space):
    clf=xgb.XGBClassifier(
                    n_estimators =space['n_estimators'], max_depth = int(space['max_depth']), gamma = space['gamma'],
                    reg_alpha = int(space['reg_alpha']),min_child_weight=int(space['min_child_weight']),
                    colsample_bytree=int(space['colsample_bytree']))
    
    evaluation = [( X_train, y_train), ( X_test, y_test)]
    
    clf.fit(X_train, y_train,
            eval_set=evaluation, eval_metric="auc",
            early_stopping_rounds=10,verbose=False)
    

    pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, pred>0.5)
    print ("SCORE:", accuracy)
    return {'loss': -accuracy, 'status': STATUS_OK }

寻优过程

trials = Trials()

best_hyperparams = fmin(fn = objective,
                        space = space,
                        algo = tpe.suggest,
                        max_evals = 100,
                        trials = trials)

打印结果

  • Here best_hyperparams gives us the optimal parameters that best fit model and better loss function value.

  • trials is an object that contains or stores all the relevant information such as hyperparameter, loss-functions for each set of parameters that the model has been trained.

  • 'fmin' is an optimization function that minimizes the loss function and takes in 4 inputs - fn, space, algo and max_evals.

  • Algorithm used is tpe.suggest.

print("The best hyperparameters are : ","\n")
print(best_hyperparams)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
XGBoost 进行参数调优可以提高模型的预测性能和泛化能力,下面是一些常用的参数调优方法: 1. 学习率:学习率越小,模型越稳定,但是训练时间会变长。一般建议初始学习率为0.1,然后根据实验结果进行调整。 2. 树的深度:树的深度越大,模型的拟合能力越强,但是容易过拟合。一般建议初始深度为3-8,然后根据实验结果进行调整。 3. 正则化参数:正则化参数可以控制模型的复杂度,防止过拟合。常用的正则化参数包括 L1 正则化和 L2 正则化。 4. 样本权重:可以为不同的样本设置不同的权重,提高模型对少数类样本的识别能力。 5. 特征子采样:可以控制每次分裂时考虑的特征数量,防止过拟合。 6. 早停法:可以设置一个阈值,当模型在验证集上的性能连续若干次没有提升时,就停止训练。 7. 网格搜索:可以通过网格搜索的方式找到最优的参数组合。 下面是一个简单的 XGBoost 参数调优的示例代码: ```python import xgboost as xgb from sklearn.model_selection import GridSearchCV # 读取数据 X_train, y_train = ... X_test, y_test = ... # 构建模型 model = xgb.XGBClassifier() # 定义参数范围 param_grid = { 'max_depth': [3, 5, 7], 'learning_rate': [0.1, 0.01, 0.001], 'n_estimators': [100, 200, 300] } # 网格搜索 grid_search = GridSearchCV(model, param_grid, cv=5, scoring='accuracy') grid_search.fit(X_train, y_train) # 输出最优参数组合和验证集上的性能 print('Best parameters:', grid_search.best_params_) print('Validation accuracy:', grid_search.best_score_) # 在测试集上评估性能 y_pred = grid_search.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print('Test accuracy:', accuracy) ``` 通过网格搜索,我们可以找到最优的参数组合,并在测试集上进行评估。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝士AI吃鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值