Repository navigation
FEA Add array API support for matthews_corrcoef - #34423
Conversation
Convert the post-confusion_matrix computation to the array API namespace of the inputs, following the cohen_kappa_score pattern. Register the metric in the array API common tests and document support. Towards scikit-learn#26024
|
Hi @Fazel94, thank you for the PR. I think the CI is failing because you put a different PR number in the changelog file name. |
|
Thanks for the heads up @AnneBeyer . |
| cov_ytyp = n_correct * n_samples - np.dot(t_sum, p_sum) | ||
| cov_ypyp = n_samples**2 - np.dot(p_sum, p_sum) | ||
| cov_ytyt = n_samples**2 - np.dot(t_sum, t_sum) | ||
| # array_api_strict only supports floating point dtypes for __truediv__ |
There was a problem hiding this comment.
The spec does not specify that inputs should only be float, see: https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__truediv__.html#truediv
If that is the case it may be worth asking in https://github.com/data-apis/array-api-strict if this is intentional
There was a problem hiding this comment.
Thank you for bringing that up, comment in the code needs modification,
The spec does say the inputs should just be numeric but the notes section adds:
If one or both of self and other have integer data types, the result is implementation-dependent, ...
Specification-compliant libraries may choose to raise an error or return an array containing the element-wise results. ...
And the implementation decides to raise an error as per spec's note.
I copied the comment verbatim from cohen_kappa_score.
I would change it to:
# The array API standard leaves integer-dtype __truediv__
# implementation-defined; array_api_strict raises for it. Convert to
# the maximum-precision float dtype so the divisions below are portable
# (and to avoid integer overflow in n_samples**2 for large inputs).Saying that I don't get int/int exception risen in array_api_strict 2.5 either.
In the source code for it and found:
# PEP 484 requires int to be a subtype of float, but __truediv__ should
# not accept int.
def __truediv__(self, other: Array | complex, /) -> Array:
...
other = self._check_allowed_dtypes(other, "floating-point", "__truediv__")It is indeed intentional.
I don't feel experienced enough to rise an issue with the spec by myself, If you think it is a good idea I can help.
There was a problem hiding this comment.
Ah I missed the note, i can see why they raise an error now.
Why _max_precision_float_dtype and not _find_matching_floating_dtype?
There was a problem hiding this comment.
original implementation had it casted to np.float64.
I did some digging and found out it was introduced at PR #9693 (2017) fixing overflow bug #9622 .
The reason seems to be cov_ypyp * cov_ytyt =< n_samples**4 wraping to negative values in int64 with n_samples > 55108. Regression test is at test_classification.py:1145.
I think it also suffers from numerical stability issues too, I did rough numerical analysis on it and found out cov_ypyp = n_samples**2 - xp.sum(p_sum * p_sum) relative error would be u*N/(2*m), where u is machine epsilon and m is binary minority count. That would lead to for example 2*10e-4 absolute error on N=1e7 and m=100 given the for float32 u=6*10e-8, that is the result would be correct up to 3-4 decimal places in float32 in imbalanced dataset.
…matthews_corrcoef
|
For information, I launched the tests on Intel GPUs here: |
|
Thanks @Fazel94. You might want to have a look at: #28668 (comment) next. |
Co-authored-by: Olivier Grisel <[email protected]>
OmarManzoor
left a comment
There was a problem hiding this comment.
Thank you for the PR @Fazel94
|
@ogrisel Thanks for the hint. I got ahead of myself and tried to pr before closing this one. |
|
@ogrisel Can you please run the intel workflow once again? |
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thanks @Fazel94
|
I guess let's merge this. |
Co-authored-by: Olivier Grisel <[email protected]>
Reference Issues/PRs
Towards #26024
What does this implement/fix? Explain your changes.
Convert the post-confusion_matrix computation to the array API namespace of the inputs, following the cohen_kappa_score pattern. Register the metric in the array API common tests and document support.
First time contributor introduction
I have used scikitlearn to a great extend during my education and in industry, I ought to give back somehow to the great community.
AI usage disclosure
I used AI assistance for:
Any other comments?
Array API is cool.