Skip to content

Commit

Permalink
Change binning behavior to be same as PR microsoft#2342.
Browse files Browse the repository at this point in the history
  • Loading branch information
btrotta committed Sep 10, 2019
1 parent 23dbb29 commit 9ed04a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/io/bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,22 @@ namespace LightGBM {
}
}

// include zero bounds if possible
// include zero bounds and infinity bound
if (max_bin == 2) {
if (left_cnt == 0) {
bin_upper_bound.push_back(kZeroThreshold);
} else {
bin_upper_bound.push_back(-kZeroThreshold);
}
} else if (max_bin >= 3) {
bin_upper_bound.push_back(-kZeroThreshold);
bin_upper_bound.push_back(kZeroThreshold);
if (left_cnt > 0) {
bin_upper_bound.push_back(-kZeroThreshold);
}
if (right_start >= 0) {
bin_upper_bound.push_back(kZeroThreshold);
}
}
bin_upper_bound.push_back(std::numeric_limits<double>::infinity());

// add forced bounds, excluding zeros since we have already added zero bounds
int i = 0;
Expand All @@ -207,7 +212,6 @@ namespace LightGBM {
++i;
}
}
bin_upper_bound.push_back(std::numeric_limits<double>::infinity());
int max_to_insert = max_bin - static_cast<int>(bin_upper_bound.size());
int num_to_insert = std::min(max_to_insert, static_cast<int>(forced_upper_bounds.size()));
if (num_to_insert > 0) {
Expand Down Expand Up @@ -239,7 +243,7 @@ namespace LightGBM {
}
bin_upper_bound.insert(bin_upper_bound.end(), bounds_to_add.begin(), bounds_to_add.end());
std::stable_sort(bin_upper_bound.begin(), bin_upper_bound.end());
CHECK(bin_upper_bound.size() <= max_bin);
CHECK(bin_upper_bound.size() <= static_cast<size_t>(max_bin));
return bin_upper_bound;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ def test_max_bin_by_feature(self):
}
lgb_data = lgb.Dataset(X, label=y)
est = lgb.train(params, lgb_data, num_boost_round=1)
self.assertEqual(len(np.unique(est.predict(X))), 99)
self.assertEqual(len(np.unique(est.predict(X))), 100)
params['max_bin_by_feature'] = [2, 100]
lgb_data = lgb.Dataset(X, label=y)
est = lgb.train(params, lgb_data, num_boost_round=1)
Expand Down

0 comments on commit 9ed04a3

Please sign in to comment.