Repository navigation
ENH Array API support for confusion_matrix - #30562
OmarManzoor merged 50 commits into
Conversation
| result = confusion_matrix(y_true, y_pred) | ||
| xp_result, _ = get_namespace(result) | ||
| assert _is_numpy_namespace(xp_result) | ||
|
|
||
| # Since the computation always happens with NumPy / SciPy on the CPU, this | ||
| # function is expected to return an array allocated on the CPU even when it does | ||
| # not match the input array's device. | ||
| assert result.device == "cpu" |
There was a problem hiding this comment.
I have adjusted this test to your suggestions from this comment, @ogrisel. But here, the test is narrower because we made the return value of confusion_matrix to always be a numpy array on cpu.
|
Regarding the question how to document the return type of |
virchan
left a comment
There was a problem hiding this comment.
I suspect the CI failure (about the Test Library) is a false positive, as I couldn't reproduce the same error after running the tests multiple times on my local machine. Instead, I encountered a different set of errors related to CUDA. I believe the issue might be linked to how we convert sample_weight into a NumPy array.
StefanieSenger
left a comment
There was a problem hiding this comment.
Thanks for your reviews, @OmarManzoor and @lesteve!
I have applied your suggestions and commented regarding the documentation.
Now, this PR looks very straightforward. :)
ogrisel
left a comment
There was a problem hiding this comment.
LGTM beside the point below.
I am not sure how to best document which scikit-learn functions and classes intentionally rely on internal array API namespace conversions (https://github.com/scikit-learn/scikit-learn/pull/30562/files#r2207423623). I think it's interesting to do it but I agree we can discuss that in a follow-up issue or PR.
Co-authored-by: Olivier Grisel <[email protected]>
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thanks @StefanieSenger
|
There are some tests failing which may need to be checked |
|
Hmm probably some weird interaction with #31701? |
|
In d1b3439 we fixed the latest failures in the most straightforward (but maybe a bit hacky) way. We also added a test that would have failed in #31701 and would have surfaced the issue. The tension comes from:
A maybe more clean approach would be to pass an additional argument |
|
@lesteve Thanks for the fixes. I think the current changes look fine. However the approach that you have mentioned about adding in |
|
I am also fine the current workaround implemented in this PR as it is well explained by the inline comment. +1 for exploring the refactoring suggested by @lesteve in a follow-up PR. |
|
Let's merge this then! |
Co-authored-by: Virgil Chan <[email protected]> Co-authored-by: Olivier Grisel <[email protected]> Co-authored-by: Loïc Estève <[email protected]> Co-authored-by: Omar Salman <[email protected]>
| # 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, y_true, y_pred, _ = _check_targets(y_true, y_pred) |
There was a problem hiding this comment.
@StefanieSenger could you expand on why this branch is needed?
In the case where "y_true, y_pred and sample_weight are all empty" (as per your comment), it would error here:
scikit-learn/sklearn/metrics/_classification.py
Lines 118 to 122 in dd22089
When just y_true, y_pred are empty, not sample_weight provided, it would also error at the same place.
If only sample_weight was empty, this then raises an error:
scikit-learn/sklearn/metrics/_classification.py
Lines 598 to 602 in dd22089
But I am not sure why this would be better than passing sample_weight to _check_targets and letting it raise the error?
Maybe you could provide an example/test for the case that this branch is for? Thanks!
There was a problem hiding this comment.
I'm afraid I cannot answer that question as I didn't commit this: d1b3439
I would agree that erroring in _check_targets if no sample_weight is provided is fine.
There was a problem hiding this comment.
Also, it seems this test were @lesteve had added the condition (test_confusion_matrix_on_zero_length_input) is now gone.
I am not sure where and why it was removed. I will check with git grap (later)
There was a problem hiding this comment.
I have removed it in #32549 when we removed support for empty inputs. 😆
So the if-else is indeed not needed anymore.
Reference Issues/PRs
towards #26024
closes #30440 (supercedes)
This PR is an alternative, discussed in #30440. It accepts array inputs from all namespaces, converts the input arrays to numpy arrays right away to do the calculations in numpy (which is necessary for the coo_matrix at least) and returns the confusion_matrix in the same namespace as the input and on a cpu device.
That's what we had discussed. For more details see the discussions on both PRs.