Dustin Sun
Dustin Sun

Reputation: 5532

how to set max_bin params in LightGBM

Following code throws error saying "Unexpected keyword arguemnt 'max_bin'". Later I found 'max_bin' is depreciated. So how can I pass max_bin using 'params'? Can anyone show me a piece of sample code?

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)

Upvotes: 0

Views: 3318

Answers (2)

ruhong
ruhong

Reputation: 1903

Set max_bin in the params dictionary, then pass it to the lgb.Dataset constructor. Note that max_bin is specified as a Dataset parameter in the official doc.

import lightgbm as lgb

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

Upvotes: 0

user2752661
user2752661

Reputation:

The params mentioned in the error message refer to the Parameters that are passed to the train() function. If you use the sklearn classes of the python API, some of the parameters are also available as keyword arguments in the classifier's __init__() method.

Example:

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))

For a detailed example i recommend taking a look at the python examples that come with LGBM.

Upvotes: 2

Related Questions