Skip to content

FIX Fixes check_array nonfinite checks with ArrayAPI specification #25619

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

Merged
merged 3 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ Changelog
when the global configuration sets `transform_output="pandas"`.
:pr:`25500` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.utils`
....................

- |Fix| Fixes a bug in :func:`utils.check_array` which now correctly performs
non-finite validation with the Array API specification. :pr:`25619` by
`Thomas Fan`_.

.. _changes_1_2_1:

Version 1.2.1
Expand Down
17 changes: 17 additions & 0 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import numpy as np
import scipy.sparse as sp

from sklearn._config import config_context
from sklearn.utils._testing import assert_no_warnings
from sklearn.utils._testing import ignore_warnings
from sklearn.utils._testing import SkipTest
Expand Down Expand Up @@ -1759,3 +1760,19 @@ def test_boolean_series_remains_boolean():

assert res.dtype == expected.dtype
assert_array_equal(res, expected)


@pytest.mark.parametrize("array_namespace", ["numpy.array_api", "cupy.array_api"])
def test_check_array_array_api_has_non_finite(array_namespace):
"""Checks that Array API arrays checks non-finite correctly."""
xp = pytest.importorskip(array_namespace)

X_nan = xp.asarray([[xp.nan, 1, 0], [0, xp.nan, 3]], dtype=xp.float32)
with config_context(array_api_dispatch=True):
with pytest.raises(ValueError, match="Input contains NaN."):
check_array(X_nan)

X_inf = xp.asarray([[xp.inf, 1, 0], [0, xp.inf, 3]], dtype=xp.float32)
with config_context(array_api_dispatch=True):
with pytest.raises(ValueError, match="infinity or a value too large"):
check_array(X_inf)
4 changes: 2 additions & 2 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def _assert_all_finite(
has_nan_error = False if allow_nan else out == FiniteStatus.has_nan
has_inf = out == FiniteStatus.has_infinite
else:
has_inf = np.isinf(X).any()
has_nan_error = False if allow_nan else xp.isnan(X).any()
has_inf = xp.any(xp.isinf(X))
has_nan_error = False if allow_nan else xp.any(xp.isnan(X))
if has_inf or has_nan_error:
if has_nan_error:
type_err = "NaN"
Expand Down