Python:LightGBM交叉验证。如何使用lightgbm.cv进行回归? [英] Python: LightGBM cross validation. How to use lightgbm.cv for regression?

查看:1278
本文介绍了Python:LightGBM交叉验证。如何使用lightgbm.cv进行回归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 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:


分层(布尔型,可选(默认= True))–是否执行
分层抽样。

stratified (bool, optional (default=True)) – Whether to perform stratified sampling.

但是分层仅适用于分类问题。因此,要使用回归,您需要将其设为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进行回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆