如何在LightGBM中设置max_bin参数

时间:2018-01-25 17:21:59

标签: python lightgbm

以下代码抛出错误说"意外关键字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:不推荐使用max_bin参数   在2.0.12版本中删除。请使用params传递此信息   参数。       '请使用params传递此参数。',LGBMDeprecationWarning)

2 个答案:

答案 0 :(得分:1)

错误消息中提到的params是指传递给train()函数的Parameters。如果您使用python API的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

答案 1 :(得分:0)

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

import lightgbm as lgb

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