问题描述
我想使用 lgb.Dataset 对LightGBM模型进行交叉验证,并使用 early_stopping_rounds 。 XGBoost的 xgboost.cv 使用以下方法时不会出现问题。我不想在GridSearchCV中使用Scikit Learn的方法,因为它不支持提早停止或lgb.Dataset。
I want to do a cross validation for LightGBM model with lgb.Dataset and use early_stopping_rounds. The following approach works without a problem with XGBoost's xgboost.cv. I prefer not to use Scikit Learn's approach with GridSearchCV, because it doesn't support early stopping or lgb.Dataset.
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error
dftrainLGB = lgb.Dataset(data = dftrain, label = ytrain, feature_name = list(dftrain))
params = {'objective': 'regression'}
cv_results = lgb.cv(
params,
dftrainLGB,
num_boost_round=100,
nfold=3,
metrics='mae',
early_stopping_rounds=10
)
任务是进行回归,但是以下代码引发错误:
支持的目标类型为:('binary','multiclass')。而是连续。
The task is to do regression, but the following code throws an error:Supported target types are: ('binary', 'multiclass'). Got 'continuous' instead.
LightGBM支持回归,还是我提供了错误的参数?
Does LightGBM support regression, or did I supply wrong parameters?
推荐答案
默认情况下,lightgbm.cv中的stratify参数为 True
。
根据:
By default, the stratify parameter in the lightgbm.cv is True
.According to the documentation:
但是分层仅适用于分类问题。因此,要使用回归,您需要将其设为False。
But stratify works only with classification problems. So to work with regression, you need to make it False.
cv_results = lgb.cv(
params,
dftrainLGB,
num_boost_round=100,
nfold=3,
metrics='mae',
early_stopping_rounds=10,
# This is what I added
stratified=False
)
现在可以正常工作了。
这篇关于Python:LightGBM交叉验证。如何使用lightgbm.cv进行回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!