Repository navigation
FIX preserve labels in compute_class_weight dict lookup - #34983
Conversation
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
AnneBeyer
left a comment
There was a problem hiding this comment.
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")): |
There was a problem hiding this comment.
I would add a (very) short comment on why the move_to is needed here.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
added
"""Ensure numeric-looking string labels are not converted to integer keys."""
Non-regression for #34883
cakedev0
left a comment
There was a problem hiding this comment.
Thanks, LGTM, and looks like a useful fix! 👍
lesteve
left a comment
There was a problem hiding this comment.
Official approval based on two trusted people having already looked at it and approved!
Reference Issues/PRs
Fixes #34883
What does this implement/fix? Explain your changes.
Replaces the
try ... exceptconversion 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 #34883The labels are instead moved to NumPy on the CPU without changing their values:
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:
I wrote the actual lines of code myself though and understand the implementation
Any other comments?