Skip to content

FIX improve warning message in _ensure_sparse_format #27757

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 6 commits into from
Nov 14, 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
4 changes: 4 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ Changelog
misdetects the CPU architecture.
:pr:`27614` by :user:`Olivier Grisel <ogrisel>`.

- |Fix| Error message in :func:`~utils.check_array` when a sparse matrix was
passed but `accept_sparse` is `False` now suggests to use `.toarray()` and not
`X.toarray()`. :pr:`27757` by :user:`Lucy Liu <lucyleeow>`.

Code and Documentation Contributors
-----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/tests/test_affinity_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_affinity_propagation_affinity_shape():

@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
def test_affinity_propagation_precomputed_with_sparse_input(csr_container):
err_msg = "A sparse matrix was passed, but dense data is required"
err_msg = "Sparse data was passed for X, but dense data is required"
with pytest.raises(TypeError, match=err_msg):
AffinityPropagation(affinity="precomputed").fit(csr_container((3, 3)))

Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_linear_regression_sample_weights(


def test_raises_value_error_if_positive_and_sparse():
error_msg = "A sparse matrix was passed, but dense data is required."
error_msg = "Sparse data was passed for X, but dense data is required."
# X must not be sparse if positive == True
X = sparse.eye(10)
y = np.ones(10)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/metrics/tests/test_dist_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def test_readonly_kwargs():
(
csr_container([1, 1.5, 1]),
TypeError,
"A sparse matrix was passed, but dense data is required",
"Sparse data was passed for w, but dense data is required",
)
for csr_container in CSR_CONTAINERS
],
Expand Down
2 changes: 1 addition & 1 deletion sklearn/preprocessing/tests/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ def test_ordinal_encoder_sparse(csr_container):

encoder = OrdinalEncoder()

err_msg = "A sparse matrix was passed, but dense data is required"
err_msg = "Sparse data was passed, but dense data is required"
with pytest.raises(TypeError, match=err_msg):
encoder.fit(X_sparse)
with pytest.raises(TypeError, match=err_msg):
Expand Down
4 changes: 2 additions & 2 deletions sklearn/tests/test_multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,11 @@ def test_ecoc_delegate_sparse_base_estimator(csc_container):
)
ecoc = OutputCodeClassifier(base_estimator, random_state=0)

with pytest.raises(TypeError, match="A sparse matrix was passed"):
with pytest.raises(TypeError, match="Sparse data was passed"):
ecoc.fit(X_sp, y)

ecoc.fit(X, y)
with pytest.raises(TypeError, match="A sparse matrix was passed"):
with pytest.raises(TypeError, match="Sparse data was passed"):
ecoc.predict(X_sp)

# smoke test to check when sparse input should be supported
Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/tests/test_mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_checking_classifier_with_params(iris, csr_container):
check_X=check_array, check_X_params={"accept_sparse": False}
)
clf.fit(X, y)
with pytest.raises(TypeError, match="A sparse matrix was passed"):
with pytest.raises(TypeError, match="Sparse data was passed"):
clf.fit(X_sparse, y)


Expand Down
2 changes: 1 addition & 1 deletion sklearn/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_resample_stratify_sparse_error(csr_container):
X = rng.normal(size=(n_samples, 2))
y = rng.randint(0, 2, size=n_samples)
stratify = csr_container(y)
with pytest.raises(TypeError, match="A sparse matrix was passed"):
with pytest.raises(TypeError, match="Sparse data was passed"):
X, y = resample(X, y, n_samples=50, random_state=rng, stratify=stratify)


Expand Down
4 changes: 2 additions & 2 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ def test_check_array_accept_sparse_type_exception():
invalid_type = SVR()

msg = (
"A sparse matrix was passed, but dense data is required. "
r"Use X.toarray\(\) to convert to a dense numpy array."
"Sparse data was passed, but dense data is required. "
r"Use '.toarray\(\)' to convert to a dense numpy array."
)
with pytest.raises(TypeError, match=msg):
check_array(X_csr, accept_sparse=False)
Expand Down
5 changes: 3 additions & 2 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,10 @@ def _ensure_sparse_format(
_check_large_sparse(sparse_container, accept_large_sparse)

if accept_sparse is False:
padded_input = " for " + input_name if input_name else ""
raise TypeError(
"A sparse matrix was passed, but dense data is required. Use X.toarray() "
"to convert to a dense numpy array."
f"Sparse data was passed{padded_input}, but dense data is required. "
"Use '.toarray()' to convert to a dense numpy array."
)
elif isinstance(accept_sparse, (list, tuple)):
if len(accept_sparse) == 0:
Expand Down