better FindBin algorithm
The new FindBin function first considers big counts as unique bins. Is it correct?
Consider a case like this: a sample of feature has 10 values: 1 2 2 2 2 3 3 3 3 4, and max_bin=3 For old algorithm: 1 and 2 in a bin, 3 in a bin, 4 in a bin For new algorithm: 2 in a bin, 3 in a bin, 1 and 4 in a bin?
And there is a bigger bug I think, current code actually works like this: init: lower_bounds: inf, inf, inf upper_bounds: inf, inf, inf before SortForPair(lower_bounds, upper_bounds, 0, false): bin_cnt = 2 lower_bounds: 2, 3, 1 upper_bounds: 2, 3, inf after SortForPair(lower_bounds, upper_bounds, 0, false): bin_cnt = 2 lower_bounds: 1, 2, 3 upper_bounds: inf, 2, 3 final: num_bin_ = 2 bin_upper_bound_: inf, inf
Thanks. This really is a bug. I think a better algorithm is like:
for (int i = 0; i < num_values; ++i) {
if (counts[i] > mean_bin_size) { is_big_value[i] = true; }
}
bin_cnt = 0
for (int i = 0; i < num_values - 1; ++i) {
// meet "big count values", construt previous value as one bin immediately
if(is_big_value[i]){
upper_bounds[bin_cnt] = distinct_values[i];
++bin_cnt;
lower_bounds[bin_cnt] = distinct_values[i + 1];
cur_cnt_inbin = 0;
mean_bin_size = rest_sample_cnt / static_cast<double>(max_bin - bin_cnt);
}
rest_sample_cnt -= counts[i];
cur_cnt_inbin += counts[i];
// need a new bin
if (cur_cnt_inbin >= mean_bin_size) {
upper_bounds[bin_cnt] = distinct_values[i];
++bin_cnt;
lower_bounds[bin_cnt] = distinct_values[i + 1];
cur_cnt_inbin = 0;
mean_bin_size = rest_sample_cnt / static_cast<double>(max_bin - bin_cnt);
}
}
However, when "big counts" and "small counts" are crossed together, it may have some problems. Do you have better idea?
Might add another threshold (e.g. mean_bin_size/2), when meets "big counts" and previous value < threshold, merge it into previous bin or "big counts". So the "small counts" would not be too small.
@wxchan , good suggestion, I think it can work
@wxchan, I have updated the code, you can check it again.
I think the bug is fixed now. is_big_value is not needed, you can check it during iteration.
I think there may be improvement, but it may not be necessary. For example, "small counts" smaller than threshold can be divided into two parts and merged into previous and current bins more evenly. Is the predict accuracy sensitive to the FindBin algorithm?
@wxchan , mean_bin_size will be changed during the iteration, so we still need this.
A better FindBin Algorithm can improve traning accuracy, but not much on validation/test data.
For the improvement, I think it worth to try. Could you help to check this? Thanks!
You only need another variable to store mean_bin_size at the beginning.
I will give a try when I have time.
I do some experiments on this. The accuracy seems not improved with more even bins. I think the purpose for building bins is to sample split points, so it might not need to be very accurate?
Actually my last suggestion (mean_bin_size/2 threshold) might not be good, here is a comparsion.
Higgs: both 0.845123
Yahoo LTR: with mean_bin_size/2 threshold: [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@1 : 0.731413 [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@3 : 0.738146 [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@5 : 0.756283 [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@10 : 0.796701
without mean_bin_size/2 threshold: [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@1 : 0.731241 [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@3 : 0.738105 [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@5 : 0.757048 [LightGBM] [Info] Iteration: 500, ../data/yahoo.test's : NDCG@10 : 0.797326
MS LTR: with mean_bin_size/2 threshold: [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@1 : 0.520082 [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@3 : 0.503170 [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@5 : 0.508764 [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@10 : 0.527204
without mean_bin_size/2 threshold: [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@1 : 0.522184 [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@3 : 0.506437 [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@5 : 0.509932 [LightGBM] [Info] Iteration: 500, ../data/msltr.test's : NDCG@10 : 0.527850
@wxchan, Maybe the reason is over-fitting. Do you take a look for the accuracy on training data?
It can be overfitting. I think it's indeed not proper to decide upon the accuracy because they don't make significant differences. I find another possible problem, and I will open another PR for that. I will close this issue for now.
This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.