GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — FIX Bagging estimators and IsolationForest raise on zero sample_weight when bootstrap=False by amkkoussay · Pull Request #35365 · scikit-learn/scikit-learn

Conversation

@amkkoussay

@amkkoussay amkkoussay commented Nov 9, 2026

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #34673.

What does this implement/fix? Explain your changes.

Since 1.8, a single zero in sample_weight makes IsolationForest (on its
defaults) and both bagging estimators raise
ValueError: Fewer non-zero entries in p than size when bootstrap=False.

The cause is in _generate_bagging_indices: without replacement,
random_state.choice(..., p=normalized_sample_weight) cannot draw more rows
than the number of non-zero weights, while max_samples defaults to the full
number of rows. With replacement there is no such bound, which is why the bug
only exists on the bootstrap=False path.

My fix only touches that path: when bootstrap_samples is False, clamp
max_samples to np.count_nonzero(sample_weight) before drawing. A
zero-weighted row has probability zero and is never drawn, so it is excluded
exactly as if it had been removed from the data, which is what a zero weight
means. If the user asked for fewer samples than the number of non-zero
weights, min(...) leaves their value unchanged.

The regression test checks more than the absence of the exception: it also
asserts that the zero-weighted row never appears in estimators_samples_,
so the statistical meaning of the fix is verified, not just the crash.

Introduce yourself

I am a high-school student from Tunisia studying computer science. I recently
completed the Machine Learning Specialization (Stanford Online /
DeepLearning.AI), where I used scikit-learn throughout, and I am contributing
this fix as a way to learn the library from the inside.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

This pull request includes code written with the assistance of AI. I have
personally reviewed, understood and tested the code.

Any other comments?

Before submitting, I checked the two call sites of
_generate_bagging_indices: max_samples is passed by value, so the clamp
only affects the local draw, and both callers only consume the returned
indices. estimators_samples_ regenerates the indices from the stored
_max_samples and _sample_weight, and since the clamp depends only on the
number of non-zero weights, the regenerated indices are identical to the
ones used during fitting.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
amkkoussay added 3 commits November 9, 2026 20:37
@amkkoussay
Fix zero sample_weight crash in bagging estimators with bootstrap=False
ffea320
@amkkoussay
Add regression test for zero sample_weight with bootstrap=False
c723ff6
@amkkoussay
Add changelog entry for the zero sample_weight bagging fix
4234775
@github-actions github-actions Bot added module:ensemble size/S labels Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Nov 9, 2026
amkkoussay added 3 commits November 9, 2026 20:43
@amkkoussay
Rename changelog entry to the PR number
3457c09
@amkkoussay
Remove the issue-numbered changelog entry (renamed to 35365)
5ba4d50
@amkkoussay
MNT Fix ruff formatting in zero sample_weight bagging test
9f64586
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Nov 10, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

module:ensemble size/S

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Zero sample_weight raises ValueError in IsolationForest and Bagging estimators when bootstrap=False

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@amkkoussay
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.