Skip to content

FIX preserve labels in compute_class_weight dict lookup - #34983

Merged
lesteve merged 4 commits into
scikit-learn:mainfrom
TonyBagnall:fix-34883
Oct 12, 2026
Merged

lesteve merged 4 commits into
scikit-learn:mainfrom
TonyBagnall:fix-34883

Conversation

@TonyBagnall

@TonyBagnall TonyBagnall commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #34883

What does this implement/fix? Explain your changes.

Replaces the try ... except conversion that converts string labels which look like integers (e.g. "1") to integers. This caused dictionary lookup to fail when the original class labels were strings, see #34883

The labels are instead moved to NumPy on the CPU without changing their values:

for i, c in enumerate(move_to(classes, xp=np, device="cpu")):

Also adds a regression test.

Introduce yourself

hello, I am a core developer of the aeon toolkit and a computer scientist. My first contribution to scikit-learn makes me strangely nervous :) I'm going to use it as an example of open source in a lecture next week!

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation

I wrote the actual lines of code myself though and understand the implementation

Any other comments?

@github-actions

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.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Sep 17, 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 @TonyBagnall! Two small documentation requests, otherwise LGTM.

c = int(c)
except ValueError: # `classes` contains strings
c = str(c)
for i, c in enumerate(move_to(classes, xp=np, device="cpu")):

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 would add a (very) short comment on why the move_to is needed here.

@TonyBagnall TonyBagnall Sep 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added
Use NumPy scalars for dictionary lookup without changing label types.
for i, c in enumerate(move_to(classes, xp=np, device="cpu")):

],
)
def test_compute_class_weight_dict_preserves_labels(labels):
# Non-regression for https://github.com/scikit-learn/scikit-learn/issues/34883

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.

Please add a short docstring on why this test is needed, so one doesn't have to consult the issue (but keep the link for reference).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added
"""Ensure numeric-looking string labels are not converted to integer keys."""
Non-regression for #34883

@AnneBeyer AnneBeyer added Quick Review For PRs that are quick to review Waiting for Second Reviewer First reviewer is done, need a second one! good first PR to review Simple atomic PR to review labels Sep 22, 2026
@AnneBeyer AnneBeyer moved this to PR waiting for reviews in Labs Sep 23, 2026
@AnneBeyer AnneBeyer added this to Labs Sep 23, 2026

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

Thanks, LGTM, and looks like a useful fix! 👍

@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 for adding the suggestions, LGTM!
@ogrisel, can we merge this?

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

Official approval based on two trusted people having already looked at it and approved!

@lesteve
lesteve merged commit 782ce52 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

good first PR to review Simple atomic PR to review module:utils Quick Review For PRs that are quick to review Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] compute_class_weight coerces class labels to int in 1.9, breaking dict lookup for string labels

4 participants