Skip to content

MNT Refactor/Improve features sampling in HGB splitting - #34786

Merged
lesteve merged 6 commits into
scikit-learn:mainfrom
cakedev0:hgb/split_candidates_sampling
Oct 12, 2026
Merged

lesteve merged 6 commits into
scikit-learn:mainfrom
cakedev0:hgb/split_candidates_sampling

Conversation

@cakedev0

@cakedev0 cakedev0 commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Will help in implementing strategy 4 described in #34764 (comment)

What does this implement/fix? Explain your changes.

It changes the features subsampling of HGB (when max_features < 1.0): instead of a boolean mask, it samples without replacement.

It has two benefits:

  • no "no-op" iteration inside the prange(..., schedule="static"), which will help for future scalability heuristic I'm working on
  • unbiased tie breaking behavior (but only for max_features < 1.0), as it randomized the order in which features are split (and we use > in the loop).

The effect on runtime is not visible as far as I could experiment, even in extreme cases where split time is dominant. But I think it should be visible in some (relatively rare) cases once combine with the "use_threads_if" heuristic as it allow having a more accurate estimate of the work to do.

There is no added complexity so it's worth the PR/merge I'd say.

AI usage disclosure

Very guided session, I had a precise idea of what I wanted.

@cakedev0 cakedev0 changed the title Improve split candidates sampling MNT Refactor/Improve features sampling in HGB splitting Aug 21, 2026

@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.

LGTM. I agree that it's having parallel loops with more homogeneous workloads should help optimize the thread-scalability in subsequent PRs.

Another benefit: I find the code easier to reason about this way.

@ogrisel ogrisel added the Quick Review For PRs that are quick to review label Sep 10, 2026

@AnneBeyer AnneBeyer 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.

Thank you @cakedev0, this was a good staring point for me to get into cython and the trees a bit more! A few comments below.

subsample_mask = subsample_mask_arr

if has_interaction_cst:
split_features = self.rng.choice(

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.

If I'm not mistaken, this can lead to different features being selected than the previous shuffling, so we probably need to mention this in a changelog here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually, after discussing with @ogrisel last week, I realized this was not a good idea: it introduce some kind of behavior discontinuity:

  • max_features=1. => features are considered in order
  • max_features=1. - eps => features are shuffled.

I added a sort after rng.choice to preserve behavior.

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.

That sounds reasonable. But the trees can still differ in terms of which features are selected for feature_fraction_per_split < 1.0 between the masking approach in main and the sampling in this PR, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah yes, I can mention this in a changelog.

@cakedev0
cakedev0 requested a review from AnneBeyer September 28, 2026 13:10
@cakedev0 cakedev0 added this to Labs Sep 28, 2026
@cakedev0 cakedev0 moved this to PR waiting for reviews in Labs Sep 28, 2026

@AnneBeyer AnneBeyer 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.

I still think this could need a changelog as mentioned here (not sure why the CI doesn't ask for one here 🤔), otherwise LGTM!

@lesteve

lesteve commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

(not sure why the CI doesn't ask for one here 🤔)

We have a small imperfect heuristic: if a test file was changed in the PR, it probably means that it's a user facing change and that a changelog is needed, otherwise we don't require a changelog.

See

- name: Check if tests have changed
id: tests_changed
run: |
set -xe
changed_files=$(git diff --name-only origin/main)
# Changelog should be updated only if tests have been modified
if [[ "$changed_files" =~ sklearn\/.+test_.+\.py ]]
then
echo "check_changelog=true" >> $GITHUB_OUTPUT
fi

As many things, it could probably be improved but maybe good enough for now 🤷

@cakedev0

Copy link
Copy Markdown
Member Author

Changelog added, let's merge once CI is green.

@lesteve
lesteve merged commit 6a40976 into scikit-learn:main Oct 12, 2026
37 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cython module:ensemble Quick Review For PRs that are quick to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants