Skip to content

FEA Add array API support for matthews_corrcoef - #34423

Merged
OmarManzoor merged 5 commits into
scikit-learn:mainfrom
Fazel94:feat/array-api-matthews-corrcoef
Jul 16, 2026
Merged

OmarManzoor merged 5 commits into
scikit-learn:mainfrom
Fazel94:feat/array-api-matthews-corrcoef

Conversation

@Fazel94

@Fazel94 Fazel94 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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:

  • Research and understanding

Any other comments?

Array API is cool.

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

Copy link
Copy Markdown
Member

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.

@Fazel94

Fazel94 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the heads up @AnneBeyer .

Comment thread sklearn/metrics/_classification.py Outdated
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__

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

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.

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?

@Fazel94 Fazel94 Jul 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@ogrisel

ogrisel commented Jul 13, 2026

Copy link
Copy Markdown
Member

For information, I launched the tests on Intel GPUs here:

https://github.com/probabl-ai/scikit-learn-intel-workflow/actions/runs/29262747564

@ogrisel ogrisel moved this to In Progress in Array API Jul 13, 2026
Comment thread doc/modules/array_api.rst Resolved
@ogrisel

ogrisel commented Jul 13, 2026

Copy link
Copy Markdown
Member

Thanks @Fazel94. You might want to have a look at: #28668 (comment) next.

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

Thank you for the PR @Fazel94

Comment thread sklearn/metrics/_classification.py Resolved
@Fazel94

Fazel94 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@ogrisel Thanks for the hint. I got ahead of myself and tried to pr before closing this one.

@OmarManzoor

Copy link
Copy Markdown
Contributor

@ogrisel Can you please run the intel workflow once again?

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

@OmarManzoor

Copy link
Copy Markdown
Contributor

I guess let's merge this.

@OmarManzoor
OmarManzoor merged commit 6099369 into scikit-learn:main Jul 16, 2026
38 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Jul 16, 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.

5 participants