Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH Array API for check_consistent_length #29519

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 21 commits into from
Dec 5, 2024
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
1 change: 1 addition & 0 deletions doc/modules/array_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Tools
-----

- :func:`model_selection.train_test_split`
- :func:`utils.check_consistent_length`

Coverage is expected to grow over time. Please follow the dedicated `meta-issue on GitHub
<https://github.com/scikit-learn/scikit-learn/issues/22352>`_ to track progress.
Expand Down
3 changes: 3 additions & 0 deletions doc/whats_new/upcoming_changes/array-api/29519.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- :func:`sklearn.utils.check_consistent_length` now supports Array API compatible
inputs.
By :user:`Stefanie Senger <StefanieSenger>`
2 changes: 1 addition & 1 deletion sklearn/metrics/cluster/_supervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ def fowlkes_mallows_score(labels_true, labels_pred, *, sparse=False):

.. versionadded:: 0.18

The Fowlkes-Mallows index (FMI) is defined as the geometric mean between of
The Fowlkes-Mallows index (FMI) is defined as the geometric mean of
Copy link
Member

Choose a reason for hiding this comment

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

I agree with the change, but this seems unrelated to the scope of the PR. Maybe next time open small quick dedicated PRs for such quick documentation fixes.

the precision and recall::

FMI = TP / sqrt((TP + FP) * (TP + FN))
Expand Down
5 changes: 3 additions & 2 deletions sklearn/utils/_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,11 @@ def get_namespace(*arrays, remove_none=True, remove_types=(str,), xp=None):
-------
namespace : module
Namespace shared by array objects. If any of the `arrays` are not arrays,
the namespace defaults to NumPy.
the namespace defaults to the NumPy namespace.

is_array_api_compliant : bool
True if the arrays are containers that implement the Array API spec.
True if the arrays are containers that implement the array API spec (see
https://data-apis.org/array-api/latest/index.html).
Always False when array_api_dispatch=False.
"""
array_api_dispatch = get_config()["array_api_dispatch"]
Expand Down
29 changes: 27 additions & 2 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
check_X_y,
deprecated,
)
from sklearn.utils._array_api import yield_namespace_device_dtype_combinations
from sklearn.utils._mocking import (
MockDataFrame,
_MockEstimatorOnOffPrediction,
)
from sklearn.utils._testing import (
SkipTest,
TempMemmap,
_array_api_for_tests,
_convert_container,
assert_allclose,
assert_allclose_dense_sparse,
Expand Down Expand Up @@ -1002,6 +1004,8 @@ def test_check_is_fitted_with_attributes(wrap):


def test_check_consistent_length():
"""Test that `check_consistent_length` raises on inconsistent lengths and wrong
input types trigger TypeErrors."""
check_consistent_length([1], [2], [3], [4], [5])
check_consistent_length([[1, 2], [[1, 2]]], [1, 2], ["a", "b"])
check_consistent_length([1], (2,), np.array([3]), sp.csr_matrix((1, 2)))
Expand All @@ -1011,16 +1015,37 @@ def test_check_consistent_length():
check_consistent_length([1, 2], 1)
with pytest.raises(TypeError, match=r"got <\w+ 'object'>"):
check_consistent_length([1, 2], object())

with pytest.raises(TypeError):
check_consistent_length([1, 2], np.array(1))

# Despite ensembles having __len__ they must raise TypeError
with pytest.raises(TypeError, match="Expected sequence or array-like"):
check_consistent_length([1, 2], RandomForestRegressor())
# XXX: We should have a test with a string, but what is correct behaviour?


@pytest.mark.parametrize(
"array_namespace, device, _", yield_namespace_device_dtype_combinations()
)
def test_check_consistent_length_array_api(array_namespace, device, _):
"""Test that check_consistent_length works with different array types."""
xp = _array_api_for_tests(array_namespace, device)

with config_context(array_api_dispatch=True):
check_consistent_length(
xp.asarray([1, 2, 3], device=device),
xp.asarray([[1, 1], [2, 2], [3, 3]], device=device),
[1, 2, 3],
["a", "b", "c"],
np.asarray(("a", "b", "c"), dtype=object),
sp.csr_array([[0, 1], [1, 0], [0, 0]]),
)

with pytest.raises(ValueError, match="inconsistent numbers of samples"):
check_consistent_length(
xp.asarray([1, 2], device=device), xp.asarray([1], device=device)
)


def test_check_dataframe_fit_attribute():
# check pandas dataframe with 'fit' column does not raise error
# https://github.com/scikit-learn/scikit-learn/issues/8415
Expand Down
4 changes: 1 addition & 3 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,8 @@ def check_consistent_length(*arrays):
>>> b = [2, 3, 4]
>>> check_consistent_length(a, b)
"""

lengths = [_num_samples(X) for X in arrays if X is not None]
uniques = np.unique(lengths)
if len(uniques) > 1:
if len(set(lengths)) > 1:
raise ValueError(
"Found input variables with inconsistent numbers of samples: %r"
% [int(l) for l in lengths]
Expand Down