Skip to content

ENH: add array API support for contingency_matrix - #34785

Merged
lucyleeow merged 6 commits into
scikit-learn:mainfrom
OmarManzoor:contigency_matrix_arr_api
Aug 24, 2026
Merged

lucyleeow merged 6 commits into
scikit-learn:mainfrom
OmarManzoor:contigency_matrix_arr_api

Conversation

@OmarManzoor

@OmarManzoor OmarManzoor commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Supersedes: #29251

What does this implement/fix? Explain your changes.

  • Adds array API support for the supervised cluster metric contingency_matrix

@OmarManzoor

Copy link
Copy Markdown
Contributor Author

CC: @lucyleeow @virchan @ogrisel @betatim I opened an updated version of the earlier PR. Could you kindly review and merge?

Comment on lines 292 to 295
def yield_metric_checker_combinations(metric_checkers=array_api_metric_checkers):
for metric, checkers in metric_checkers.items():
for checker in checkers:
yield metric, checker

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.

Nit not directly related to this PR (sorry) - instead of maintaining the array_api_metric_checkers list, what if we used the existing list names e.g. SUPERVISED_METRICS and then just yield the appropriate check? We would iterate over all cluster metrics and just maintain a temp list of metrics that do not yet support array api (during the period where we are in process of adding support)
(similar to what is done in sklearn/metrics/tests/test_common.py)

@OmarManzoor OmarManzoor Aug 24, 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.

I checked this and it seems like the main issue with adding this particular metric to the SUPERVISED_METRICS is that it returns a NumPy array instead of a Python float which all the others do so which would require skipping this from about 5 tests in the file.

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 how annoying. What about a new variable SUPERVISED_METRICS_CONTINGENCY that includes contingency_matrix and is used for array API testing?

Not ideal but we do it for classification_report in metrics test_common.py as this outputs a string:

CLASSIFICATION_METRICS_REPORT = {
**CLASSIFICATION_METRICS,
"classification_report": classification_report,

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.

I guess a separate mapping SUPERVISED_METRICS_CONTINGENCY that just contains contingency_matrix for now seems reasonable.

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

LGTM. The Intel CI passes as well. Thanks, @OmarManzoor!

Also, +1 on #34785 (comment).

@virchan virchan added the Waiting for Second Reviewer First reviewer is done, need a second one! label Aug 22, 2026

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

LGTM just 2 comments. I imagine #34785 (comment) would need to be in a different PR because there would need to be a bit of fiddlying, esp as contingency_matrix is currently not in the SUPERVISED_METRICS list and adding it may require fiddling with tests or excluding it from some tests.

metric,
array_namespace,
device,
dtype_name, # This is used for atol so it needs to be float or None

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.

this comment doesn't make sense as we add int above?

@OmarManzoor OmarManzoor Aug 24, 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.

Actually dtype_name is the original float dtype which we pass here. We can't simply pass the loop variable dt_name as that can contain int which doesn't work with atol

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 see the problem is that check_array_api_metric cannot handle ints - but I guess this works fine.

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.

Happy for me to merge then?

@OmarManzoor OmarManzoor Aug 24, 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.

Yeah the original float works fine. I guess we can merge but the refactoring change you suggested makes sense too. I guess a follow up could work for that to dedicatedly do the refactoring work. Thank you

@lucyleeow lucyleeow Aug 24, 2026 •

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.

Here, I wonder if it makes sense (later) to add int support to _atol_for_type?

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.

I guess that is something that could be considered. Not sure how much usage it would have though.

@lucyleeow
lucyleeow merged commit 16a5003 into scikit-learn:main Aug 24, 2026
37 checks passed
@OmarManzoor
OmarManzoor deleted the contigency_matrix_arr_api branch August 24, 2026 10:43
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

Labels

Array API module:metrics Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants