[python package]: categorical_feature in lgb.Dataset object doesn't appear to matter
Setting a categorical_feature in the definition of lgb.Dataset() doesn't appear to do anything. However, setting categorical_feature in the train() function does appear to to do something.
It seems like that, by default, the train() function should treat the categorical columns of lgb.Dataset as categorical without having to manually respecify.
import lightgbm as lgb
import numpy as np
import pandas as pd
# construct an example in which the y-value is just the parity of the first column of X
X = np.array([np.random.randint(0,10, size=n), np.random.normal(size=n)]).transpose()
y = X[:,0]%2
# 1. train, stipulating that the first column is categorical
ds = lgb.Dataset(X, y, categorical_feature=[0])
params = {'objective':'regression_l2', 'metric':'rmse'}
gbm = lgb.train(params, ds,num_boost_round=10, valid_sets=[ds])
[1] training's rmse: 0.487698
# [2] training's rmse: 0.482899
# [3] training's rmse: 0.478976
# [4] training's rmse: 0.477071
# [5] training's rmse: 0.473938
# [6] training's rmse: 0.472413
# [7] training's rmse: 0.471083
# [8] training's rmse: 0.46857
# [9] training's rmse: 0.467454
# [10] training's rmse: 0.465393
# 2. training, except we don't set anything as categorical. Exact same as #1.
ds = lgb.Dataset(X, y)
params = {'objective':'regression_l2', 'metric':'rmse'}
gbm = lgb.train(params, ds,num_boost_round=10, valid_sets=[ds])
# [1] training's rmse: 0.487698
# [2] training's rmse: 0.482899
# [3] training's rmse: 0.478976
# [4] training's rmse: 0.477071
# [5] training's rmse: 0.473938
# [6] training's rmse: 0.472413
# [7] training's rmse: 0.471083
# [8] training's rmse: 0.46857
# [9] training's rmse: 0.467454
# [10] training's rmse: 0.465393
#3. now, set the categorical_feature in train(). This gives a different output than above.
ds = lgb.Dataset(X, y, categorical_feature=[0])
params = {'objective':'regression_l2', 'metric':'rmse'}
gbm = lgb.train(params, ds,num_boost_round=10, valid_sets=[ds], categorical_feature=[0])
# [1] training's rmse: 0.49152
# [2] training's rmse: 0.489862
# [3] training's rmse: 0.488515
# [4] training's rmse: 0.487341
# [5] training's rmse: 0.486353
# [6] training's rmse: 0.485551
# [7] training's rmse: 0.484853
# [8] training's rmse: 0.484287
# [9] training's rmse: 0.48353
# [10] training's rmse: 0.483057
# 4. what if we don't bother with categorical_feature in the lgb.Dataset definition?
# same thing as #3.
ds = lgb.Dataset(X, y)
params = {'objective':'regression_l2', 'metric':'rmse'}
gbm = lgb.train(params, ds,num_boost_round=10, valid_sets=[ds], categorical_feature=[0])
# [1] training's rmse: 0.49152
# [2] training's rmse: 0.489862
# [3] training's rmse: 0.488515
# [4] training's rmse: 0.487341
# [5] training's rmse: 0.486353
# [6] training's rmse: 0.485551
# [7] training's rmse: 0.484853
# [8] training's rmse: 0.484287
# [9] training's rmse: 0.48353
# [10] training's rmse: 0.483057
refer to our test here: https://github.com/Microsoft/LightGBM/blob/master/tests/python_package_test/test_engine.py#L211-L239
I think it should work.
maybe you can try to set 'boost_from_average': False,.
if your n is small, you also may need to set 'min_data': 1, and 'min_data_in_bin': 1, .
@wxchan it seems there has some bug with this:
ds = lgb.Dataset(X, y, categorical_feature=[0])
params = {'objective':'regression_l2', 'metric':'rmse'}
gbm = lgb.train(params, ds,num_boost_round=10, valid_sets=[ds])
the categorical_feature in lgb.Dataset will be override by lgb.train ?
@guolinke yes, there is definitely some bug. Let's discuss what should happen in case 3 & 4 below:
- Dataset: None, train: something -> use train setting
- Dataset: not None, train: None -> use Dataset setting
- Dataset: not None, train: auto -> use Dataset setting?
- Dataset: not None, train: others -> override or intersect?
@guolinke another thing is, we are using set_categorical_feature to set categorical_feature, but it's also a public method of Dataset. What if user wants to reset categorical_feature with this method, does those logic remain?
@wxchan for 3 & 4. We can give the warning. And override by lgb.train if dataset is not constructed.
@guolinke for 3, I think use Dataset is better, because 'auto' is default setting.
@wxchan I think as long as the dataset is not constructed, the categorical feature can be override. But we can always give a warning if previous is not none.
@wxchan it makes sense. But I think we still need to give a warning in that case.
@wxchan updates ?