XGBoost版本: 1.4.2
首先,对于不同的任务,XGBoost有不同的超参数以及默认值。可以使用get_params()函数将当前模型使用的超参数打印出来:
import xgboost as xgb
params = {'n_estimators': 4,
'max_depth': 3}
model = xgb.XGBRegressor(**params)
model.fit(X, y)
## 输出超参数
print(model.get_params())
{'objective': 'reg:squarederror',
'base_score': 0.5,
'booster': 'gbtree',
'colsample_bylevel': 1,
'colsample_bynode': 1,
'colsample_bytree': 1,
'gamma': 0,
'gpu_id': -1,
'importance_type': 'gain',
'interaction_constraints': '',
'learning_rate': 0.300000012,
'max_delta_step': 0,
'max_depth': 6,
'min_child_weight': 1,
'missing': nan,
'monotone_constraints': '()',
'n_estimators': 100,
'n_jobs': 12,
'num_parallel_tree': 1,
'random_state': 0,
'reg_alpha': 0,
'reg_lambda': 1,
'scale_pos_weight': 1,
'subsample': 1,
'tree_method': 'exact',
'validate_parameters': 1,
'verbosity': None}
n_estimators
:使用多少棵树来拟合,也可以理解为多少次迭代。默认100;
learning_rate
:学习率,每棵树的预测结果都要乘以这个学习率,默认0.3;
树相关参数:
max_depth
:每一棵树最大深度,默认6;
min_child_weight
:可以理解为叶子节点最小样本数,默认1;
gamma
:叶节点上进行进一步分裂所需的最小"损失减少"。默认0;
抽样:
subsample
:训练集抽样比例,每次拟合一棵树之前,都会进行该抽样步骤。默认1,取值范围(0, 1]
列抽样:
colsample_bytree
:每次拟合一棵树之前,决定使用多少个特征。
colsample_bylevel
:每层树节点,可以使用多少个特征。
colsample_bynode
:每个节点分裂之前,决定使用多少个特征。
这三个参数默认都是1,取值范围(0, 1],列抽样也可以理解为特征抽样,注意这三个参数是同时作用的,比如训练集总共有64个特征,参数{‘colsample_bytree’:0.5, ‘colsample_bylevel’:0.5, ‘colsample_bynode’:0.5},则每次拟合一棵树之前,在64个特征中随机抽取其中32个特征,然后在树的每一层,在32个特征中随机抽取16个特征,然后每次节点分裂,从16个特征中随机抽取8个特征。
树方法:
tree_method
:默认是auto,会自动选择最保守的方式。这个是决定训练速度的关键超参数。一般有三种树方法:exact(精确方法),approx(近似方法),hist(直方图方法),其中hist就是LightGBM中的直方图方法,速度最快,approx速度次之,exact最慢。