LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

optimal split for categorical features

Open guolinke opened this issue 9 years ago • 15 comments

We often convert the categorical features into one-hot coding. However, it is not a good solution in tree learner. The reason is, for the high cardinality categorical features, it will grow the very unbalance tree, and needs to grow very deep to achieve the good accuracy.

Actually, the optimal solution is partitioning the categorical feature into 2 subsets, and there are 2^(k-1) - 1 possible partitions. But there is a efficient solution for regression tree (refer to: http://www.jstor.org/stable/2281952 ). It needs about klogk to find the optimal partition.

The basic idea is sorting the histogram according to it's accumulate values (sum_gradient / sum_hessian), then find the best split on the sorted histogram, just like the numerical features.

guolinke avatar Jul 17 '17 07:07 guolinke

The new release GBDT tool CatBoost seems also can handle the categorical feature well.

guolinke avatar Jul 18 '17 15:07 guolinke

Reading CatBoost docs it seems categorical features are encoded numerically, sources:

  • https://tech.yandex.com/catboost/doc/dg/concepts/parameter-tuning_ctr-docpage/
  • https://tech.yandex.com/catboost/doc/dg/concepts/algorithm-main-stages_cat-to-numberic-docpage/

Laurae2 avatar Jul 19 '17 18:07 Laurae2

Yes, the way CatBoost using will benefit a lot when the samples are relative small and sparse, and offering the capability to predict on 'unseen' values. We may offer an preprocessor for categorical feature to do the transformation.

chivee avatar Jul 20 '17 05:07 chivee

Are the default values of max_cat_group, max_cat_threshold, cat_smooth enough for accuracy?

henry0312 avatar Aug 02 '17 02:08 henry0312

@henry0312 These parameters are used to deal with over-fitting. So the optimal parameters are depends.

max_cat_group is like the max_bin in numerical features, I think it is better to use small values. max_cat_threshold is used to reduce the communication cost in parallel learning, so it is safe to use a large value if only performing single machine training.

cat_smooth is a smooth term. The categories with few data are often noisy. Adding the smooth(refer to https://en.wikipedia.org/wiki/Additive_smoothing) can reduce the effect of these noise. When #data is small or #categorical is large, it is better to use large values.

updated:

cat_smooth is replaced with 3 new parameters, min_cat_smooth, max_cat_smooth, cat_smooth_ratio. The smooth denominator is a = min(max_cat_smooth, max(min_cat_smooth, num_data/num_category*cat_smooth_ratio)). The smooth numerator is b = a * sum_gradient / sum_hessian. Though it changes to 3 parameter, but you don't need to tune them much since they are more adaptable in most cases.

guolinke avatar Aug 02 '17 04:08 guolinke

@guolinke thank you for explanation. I'll try some experiments while reading your explaining.

BTW, I think it's about time to release v2.0.5.

henry0312 avatar Aug 02 '17 08:08 henry0312

@henry0312 yeah. It will be better if you can share some experiment results here.

guolinke avatar Aug 02 '17 08:08 guolinke

For some my tasks (that can't be published for detail), I got the best performance with #762.

henry0312 avatar Aug 23 '17 08:08 henry0312

@guolinke I recall seeing an update a couple weeks ago that this new treatment of categorical features has been temporarily removed. Is this still the case? Could you provide any additional information on what the issue is? Thanks!

rgranvil avatar Sep 01 '17 14:09 rgranvil

Is there any difference how to treat binary feature (with only two values) as categorical or numerical?

tt83 avatar May 12 '18 04:05 tt83

@tt83 it has no difference

guolinke avatar May 12 '18 07:05 guolinke

Hi guys, I have a question about the threshold for categorical split in lightgbm. The threshold value for a categorical feature with k level can be any integer from 0 to 2^(k-1) -2 right? and each integer represents a subset of levels that splits the data to the left/right child? So there should be an internal mapping from integer value to subset of levels of a categorical feature. How is this mapping constructed? (is that just the bitset minus 1? I tried to find it in the source code but failed). If there is an internal mapping then how do the model.txt and pmml file interpret it? How does pmml file know how to split a categorical feature during evaluation?

For example, I have a categorical feature that has 4 distinct value (10,11,12,13). Then the threshold value for splitting that feature can be any one of 0,1,2,3,4,5,6. Look into the pmml file, we can see a line looks like: <SimplePredicate field="categorical" operator="equal" value="3"/> . My question is : I didn't find any information about mapping between integer to subset of levels for that categorical feature in pmml file, will that cause problem when I use that pmml file to do prediction and will the categorical feature split correctly? @guolinke

Cuize avatar Mar 27 '19 18:03 Cuize

@Cuize the internal mapping is only used in training. the model file stores the original values without mapping.

guolinke avatar Mar 28 '19 02:03 guolinke

@guolinke Thanks for your reply! I realize it is the problem of my version of pmml generator. I tried another pmml generator: https://github.com/jpmml/jpmml-lightgbm and now it worked. It seems in the model.txt file, the cat_threshold is the actual bitset that maps to the subset of levels.

Cuize avatar Mar 28 '19 04:03 Cuize

The basic idea is sorting the histogram according to its accumulate values (sum_gradient / sum_hessian)

I'm trying to understand what this sentence means. By sorting the histogram, do you mean sorting the categories of a categorical feature? E.g. if the category feature is color, and it has categories and their corresponding accumulate values being

  • red=0.5
  • blue=0.7
  • yellow=0.3

After the sorting, it'll beyellow, red, blue, do I understand correctly?

Also, where is the accumulate values (ratio of sum_gradient / sum_hessian) from, where can I find more info about it?

zyxue avatar Jan 29 '22 23:01 zyxue