Skip to content

[WIP] Add array-api support to metrics.confusion_matrix #28867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

charlesjhill
Copy link

@charlesjhill charlesjhill commented Apr 21, 2024

Reference Issues/PRs

See #26024 for the array-api meta-issue tracking the "tools" in sklearn.

What does this implement/fix? Explain your changes.

This PR adds array-api compatibility to the sklearn.metrics.confusion_matrix method, aiming to support all of its current API surface. Many other classification metrics are or can be computed based on a confusion matrix so it seems fairly high value to port.

TODO:

  • Complete porting confusion_matrix test suite to check other array namespaces.

Any other comments?

None for now :)

Copy link

github-actions bot commented Apr 21, 2024

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 2ea2722. Link to the linter CI: here

Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Quick feedback.

# import array_api_strict as xp
X = xp.asarray(X, copy=copy)
dtype = X.dtype
isscaler = X.ndim == 0
Copy link
Member

Choose a reason for hiding this comment

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

Typo: scaler => scalar.

msg = (
"Cannot return indices with the torch backend yet. See" " array_api_compat."
)
raise NotImplementedError(msg)
Copy link
Member

Choose a reason for hiding this comment

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

I have opened data-apis/array-api-compat#135. If array_api_compat maintainers accept this suggestion, then it might be worth contributing such a temporary workaround to array_api_compat. If not, we can implement our own temporary workaround for torch in scikit-learn.

yield namespace, "cuda"
yield namespace, "mps"
else:
yield namespace, None
Copy link
Member

Choose a reason for hiding this comment

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

+1 for this. There once this is in, we should do a follow-up PR for occurrences of yield_namespace_device_dtype_combinations that discard the dtype value to avoid redundant test cases.

@ogrisel
Copy link
Member

ogrisel commented May 7, 2024

Please also add a changelog entry in v1.6.rst as soon as #27381 is merged.

f"Got y_true={xp.unique(y_true)} and "
f"y_pred={xp.unique(y_pred)}. Make sure that the "
f"Got y_true={xp.unique_values(y_true)} and "
f"y_pred={xp.unique_values(y_pred)}. Make sure that the "
"predictions provided by the classifier coincides with "
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"predictions provided by the classifier coincides with "
"predictions provided by the classifier coincide with "

unrelated grammar fix (I think)

if y_type not in ("binary", "multiclass"):
raise ValueError("%s is not supported" % y_type)

if labels is None:
labels = unique_labels(y_true, y_pred)
else:
labels = np.asarray(labels)
n_labels = labels.size
n_labels = size(labels)
if n_labels == 0:
raise ValueError("'labels' should contains at least one label.")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
raise ValueError("'labels' should contains at least one label.")
raise ValueError("'labels' should contain at least one label.")

unrelated typo fix

@@ -542,14 +567,19 @@ def get_namespace(*arrays, remove_none=True, remove_types=(str,), xp=None):
# message in case it is missing.
import array_api_compat

namespace, is_array_api_compliant = array_api_compat.get_namespace(*arrays), True
# Convert lists and tuple to numpy arrays.
Copy link
Member

Choose a reason for hiding this comment

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

Can we make the comment so it explains why it is a good idea to do this? I think that would be helpful to have here, at least for me it is not 100% clear that we should do this

if size(y_true) == 0:
return xp.zeros((n_labels, n_labels), dtype=xp.int32, device=device)

if size(_intersect1d(y_true, labels, xp=xp)) == 0:
Copy link
Member

Choose a reason for hiding this comment

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

What happens here if labels is a Python list of strings and y_true is, say, a torch array?

As a user I'd probably provide the labels as a Python list/tuple, mostly because it is convenient and not performance critical.

Is there a downside to being helpful to the callers and allowing list/tuple here?

@charlesjhill
Copy link
Author

Thanks for the comments @ogrisel, @betatim. I've been putting most of my dev time into #27113, but I will see if I can find some time to get back to this one in the next few days ~

@lesteve
Copy link
Member

lesteve commented Jan 23, 2025

I am going to close this one since there has been some work in the meantime on confusion_matrix see #30562 for example.

Hopefully you don't mind too much @charlesjhill 🙏.

@lesteve lesteve closed this Jan 23, 2025
@StefanieSenger
Copy link
Contributor

Sorry @charlesjhill, that was an oversight. I wasn't aware you had opened a WIP PR before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants