Skip to content

Move mixed array input support of classification metrics to _check_targets - #34221

Merged
OmarManzoor merged 12 commits into
scikit-learn:mainfrom
lucyleeow:mixed_array_class_metrics
Jul 7, 2026
Merged

OmarManzoor merged 12 commits into
scikit-learn:mainfrom
lucyleeow:mixed_array_class_metrics

Conversation

@lucyleeow

@lucyleeow lucyleeow commented Jun 9, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Towards #33519 along with #33778 (which moves support for regression metrics)
Follows from #32755
As discussed in #33515 (comment)

What does this implement/fix? Explain your changes.

  • Moves support for mixed array namespace/device inputs to classification metrics to _check_targets
  • labels - a common parameter that also supports array-like inputs, is not handled commonly in _check_targets because it is treated differently (in confusion_matrix labels is converted to numpy array, whereas for multilabel_confusion_matrix it is converted to xp array)
  • Moves attach_unique inside _check_targets - previously this was done at the start of functions, but it did not make sense to do this before move_to as you could be attaching it to the 'old' array.

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

Any other comments?

Will keep this as draft as it sort of depends on #34046

@lucyleeow lucyleeow left a comment

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.

Some notes, before I forget

Comment on lines +1916 to +1917
y_true, y_pred, average, labels, pos_label, xp=None, device=None
):

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.

This was definitely not needed, it just removes a get_namespace_and_device call.
Happy to remove.

# labelled micro average
micro_is_accuracy = (y_type == "multiclass" or y_type == "binary") and (
not labels_given or (set(labels) >= set(unique_labels(y_true, y_pred)))
not labels_given or (set(labels) >= set(unique_labels_))

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.

This is the same as unique_labels_ which was obtained in _check_targets via unique_labels()

Comment thread sklearn/metrics/_classification.py Resolved
Comment on lines -537 to -552
if sample_weight is None:
sample_weight = np.ones(y_true.shape[0], dtype=np.int64)
else:
sample_weight = move_to(sample_weight, xp=np, device="cpu")

if len(sample_weight) > 0:
y_type, unique_labels, y_true, y_pred, sample_weight = _check_targets(
y_true, y_pred, sample_weight
)
else:
# This is needed to handle the special case where y_true, y_pred and
# sample_weight are all empty.
# In this case we don't pass sample_weight to _check_targets that would
# check that sample_weight is not empty and we don't reuse the returned
# sample_weight
y_type, unique_labels, y_true, y_pred, _ = _check_targets(y_true, y_pred)

@lucyleeow lucyleeow Jun 19, 2026 •

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.

The y_true move to is not needed because we move everything to the same ns and device as y_pred in _check_targets.

The sample weight stuff is all handled in _check_sample_weights, which is called in _check_targest -> see #30562 (comment)

@ogrisel ogrisel moved this to In Progress in Array API Jun 22, 2026
@ogrisel

ogrisel commented Jun 22, 2026 •

Copy link
Copy Markdown
Member

Is this PR ready review? The tests pass. Are there still things to do?

EDIT: I just saw this justification in the description:

Will keep this as draft as it sort of depends on #34046

I will give this a review anyway and then we can come back to the discussion about dataframe support for move_to.

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

If I am not mistaken, this PR seems to be a private refactoring that does not impact the user experience but simplifies the code quite a bit.

So +1 for merge.

Comment thread sklearn/metrics/_classification.py Resolved
Comment thread sklearn/metrics/_classification.py Resolved
@lucyleeow lucyleeow added the Waiting for Second Reviewer First reviewer is done, need a second one! label Jul 3, 2026
@lucyleeow
lucyleeow marked this pull request as ready for review July 3, 2026 07:16
@lucyleeow

lucyleeow commented Jul 3, 2026 •

Copy link
Copy Markdown
Member Author

Thanks @ogrisel ! I originally thought it should depend on #34046 but I think that will take a while to come to a decision, and existing behaviour does not support list and non-NumPy inputs anyway - and I don't think this PR needs to fix things.

Marking as ready for review. Thanks!

@ogrisel

ogrisel commented Jul 6, 2026

Copy link
Copy Markdown
Member

cc @betatim @virchan @OmarManzoor

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thank you @lucyleeow

Comment thread sklearn/metrics/_classification.py Resolved
@OmarManzoor
OmarManzoor enabled auto-merge (squash) July 7, 2026 06:29
@OmarManzoor
OmarManzoor merged commit 990f7e0 into scikit-learn:main Jul 7, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Jul 7, 2026
@lucyleeow
lucyleeow deleted the mixed_array_class_metrics branch July 8, 2026 05:18
lucyleeow added a commit to lucyleeow/scikit-learn that referenced this pull request Jul 9, 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 done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants