Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PRF: Optimize binning in HGBT - #34248

Merged
lorentzenchr merged 24 commits into
scikit-learn:mainfrom
cakedev0:opt/hgb/binning_fit
Jun 24, 2026
Merged

PRF: Optimize binning in HGBT#34248
lorentzenchr merged 24 commits into
scikit-learn:mainfrom
cakedev0:opt/hgb/binning_fit

Conversation

@cakedev0

@cakedev0 cakedev0 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

This PR optimizes binning in histogram gradient boosting, specifically _find_binning_thresholds.

Closes #34304

Initial motivation

The weighted path is currently very slow for datasets with fewer than 200k samples because _weighted_percentile dominates the binning time. For larger datasets, sample weights are consumed by subsampling, so this bottleneck does not apply in the same way.

For example, on a 100k x 32 weighted dataset, binning takes more around 90% of the total fit time. The main issue is that the current implementation loops over percentiles and calls _weighted_percentile once per percentile. A single call to _weighted_percentile is still not that efficient, especially with many threads, so this PR adds a small local helper optimized for this use case.

What does this implement/fix? Explain your changes.

This PR optimizes threshold computation by:

  • using a local _weighted_percentile_1d_sorted helper instead of _weighted_percentile;
  • using np.sort instead of np.argsort for the unweighted case;
  • avoiding a mask allocation for NaNs in the unweighted case by using np.searchsorted;
  • replacing np.unique with a simple distinct-value mask on already sorted data.

AI usage disclosure

I used AI assistance for:

  • test and benchmark generation.

Speed-up

Weighted datasets under 200k samples with more than 255 distinct values get the largest speed-ups. For small datasets the speed-up can be extremely large, and for a typical 100k x 32 dataset it is already around 100x.

For unweighted datasets:

  • single-threaded execution is typically 2-3x faster;
  • multi-threaded execution can be up to 2x faster, although the gains are often smaller.

Detailed benchmark results were produced with this benchmark script and this runner script. The tables report speed-up of this branch relative to main.

On a large machine:

case_name 1 thread 16 threads
binary_nan_unweighted 2.03 1.30
binary_nan_weighted 1.02 1.04
binary_no_nan_unweighted 3.38 1.10
binary_no_nan_weighted 0.99 1.04
uniform_nan_unweighted 1.99 1.30
uniform_nan_weighted 62.79 180.06
uniform_no_nan_unweighted 2.89 1.00
uniform_no_nan_weighted 73.40 220.37

On my laptop:

case_name 1 thread 12 threads
binary_nan_unweighted 2.19 1.61
binary_nan_weighted 1.16 0.99
binary_no_nan_unweighted 3.59 2.09
binary_no_nan_weighted 1.03 0.97
uniform_nan_unweighted 2.16 1.02
uniform_nan_weighted 43.75 134.02
uniform_no_nan_unweighted 3.59 1.02
uniform_no_nan_weighted 65.46 161.34

@cakedev0
cakedev0 marked this pull request as ready for review June 11, 2026 16:11
@cakedev0 cakedev0 changed the title [WIP] PRF: Optimize binning in HGBT PRF: Optimize binning in HGBT Jun 11, 2026
@cakedev0

Copy link
Copy Markdown
Contributor Author

A single call to _weighted_percentile is still not that efficient, especially with many threads

Some data to back-up this claim: on my laptop, it's 4x slower for 1 thread and 20x slower for 12 threads (for the cases that pass into the code path calling _weighted_percentile).

@lorentzenchr lorentzenchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Some minor comments. In particular a missing test.

Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py
cakedev0 and others added 3 commits June 15, 2026 10:52
Comments improvement

Co-authored-by: Christian Lorentzen <[email protected]>
Co-authored-by: Christian Lorentzen <[email protected]>
Co-authored-by: Christian Lorentzen <[email protected]>
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Jun 15, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 17, 2026

@lorentzenchr lorentzenchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 nits then LGTM

Comment thread sklearn/utils/tests/test_stats.py Outdated
Comment thread sklearn/utils/stats.py Outdated
@cakedev0 cakedev0 added this to Labs Jun 24, 2026
@cakedev0 cakedev0 moved this to In progress in Labs Jun 24, 2026
@cakedev0

Copy link
Copy Markdown
Contributor Author

Hi @lorentzenchr, I think this PR should be good to merge after a second review, right?

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few nits but LGTM. Thanks!

Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
Comment thread sklearn/ensemble/_hist_gradient_boosting/binning.py Outdated
@cakedev0

Copy link
Copy Markdown
Contributor Author

Thx!

I should really take the time to install and configure a spellchecker in VS-code 😅 I had that in my previous set-up...

@lorentzenchr
lorentzenchr merged commit c8f2a90 into scikit-learn:main Jun 24, 2026
38 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Jun 24, 2026
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speed up histogram bin creation in sklearn.ensemble._hist_gradient_boosting.binning

3 participants