optimal split for categorical features
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.
The new release GBDT tool CatBoost seems also can handle the categorical feature well.
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/
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.
Are the default values of max_cat_group, max_cat_threshold, cat_smooth enough for accuracy?
@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_smoothis 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 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 yeah. It will be better if you can share some experiment results here.
For some my tasks (that can't be published for detail), I got the best performance with #762.
@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!
Is there any difference how to treat binary feature (with only two values) as categorical or numerical?
@tt83 it has no difference
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 the internal mapping is only used in training. the model file stores the original values without mapping.
@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.
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?