如何在 LightGBM 中设置 max_bin 参数

how to set max_bin params in LightGBM

以下代码抛出错误 "Unexpected keyword arguemnt 'max_bin'"。后来我找到了'max_bin' is depreciated。那么如何使用 'params' 传递 max_bin 呢?谁能给我一段示例代码?

lgb.Dataset(x_train, lable=y, max_bin=56)

/anaconda3/lib/python3.6/site-packages/lightgbm/basic.py:648: LGBMDeprecationWarning: The max_bin parameter is deprecated and will be removed in 2.0.12 version. Please use params to pass this parameter. 'Please use params to pass this parameter.', LGBMDeprecationWarning)

错误消息中提到的params指的是传递给train()函数的Parameters。如果你使用pythonAPI的sklearn类,一些参数在分类器的__init__()方法中也可以作为关键字参数。

示例:

import lightgbm as lgb
import numpy as np

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

iris = datasets.load_iris()

X_train, X_test, y_train, y_test = train_test_split(iris.data,
                                                    iris.target,
                                                    test_size=0.2)
lgb_train = lgb.Dataset(X_train, y_train)

# here are the parameters you need
params = {
    'task': 'train',
    'boosting_type': 'gbdt',
    'objective': 'multiclass',
    'num_class': 3,
    'max_bin': 4  # <-- max_bin
}

gbm = lgb.train(params,
                lgb_train,
                num_boost_round=20)

y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
y_pred = np.argmax(y_pred, axis=1)
print("Accuracy: ", accuracy_score(y_test, y_pred))

有关详细示例,我建议您查看 LGBM 附带的 python examples

params字典中设置max_bin,然后将其传递给lgb.Dataset构造函数。请注意,max_binofficial doc.

中指定为数据集参数
import lightgbm as lgb

lgb.Dataset(x_train, label=y_train, params={"max_bin": 63})