Repository navigation
Move mixed array input support of classification metrics to _check_targets - #34221
Conversation
lucyleeow
left a comment
There was a problem hiding this comment.
Some notes, before I forget
| y_true, y_pred, average, labels, pos_label, xp=None, device=None | ||
| ): |
There was a problem hiding this comment.
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_)) |
There was a problem hiding this comment.
This is the same as unique_labels_ which was obtained in _check_targets via unique_labels()
| 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) |
There was a problem hiding this comment.
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)
|
Is this PR ready review? The tests pass. Are there still things to do? EDIT: I just saw this justification in the description:
I will give this a review anyway and then we can come back to the discussion about dataframe support for |
ogrisel
left a comment
There was a problem hiding this comment.
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.
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thank you @lucyleeow
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.
_check_targetslabels- a common parameter that also supports array-like inputs, is not handled commonly in_check_targetsbecause it is treated differently (inconfusion_matrixlabelsis converted to numpy array, whereas formultilabel_confusion_matrixit is converted toxparray)attach_uniqueinside_check_targets- previously this was done at the start of functions, but it did not make sense to do this beforemove_toas you could be attaching it to the 'old' array.AI usage disclosure
I used AI assistance for:
Any other comments?
Will keep this as draft as it sort of depends on #34046