Skip to content
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

MAINT Clean up deprecations for 1.6: in make_sparse_spd_matrix #30214

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
48 changes: 7 additions & 41 deletions sklearn/datasets/_samples_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import array
import numbers
import warnings
from collections.abc import Iterable
from numbers import Integral, Real

Expand All @@ -18,7 +17,7 @@
from ..preprocessing import MultiLabelBinarizer
from ..utils import check_array, check_random_state
from ..utils import shuffle as util_shuffle
from ..utils._param_validation import Hidden, Interval, StrOptions, validate_params
from ..utils._param_validation import Interval, StrOptions, validate_params
from ..utils.random import sample_without_replacement


Expand Down Expand Up @@ -1667,7 +1666,7 @@ def make_spd_matrix(n_dim, *, random_state=None):

@validate_params(
{
"n_dim": [Hidden(None), Interval(Integral, 1, None, closed="left")],
"n_dim": [Interval(Integral, 1, None, closed="left")],
"alpha": [Interval(Real, 0, 1, closed="both")],
"norm_diag": ["boolean"],
"smallest_coef": [Interval(Real, 0, 1, closed="both")],
Expand All @@ -1677,23 +1676,18 @@ def make_spd_matrix(n_dim, *, random_state=None):
None,
],
"random_state": ["random_state"],
"dim": [
Interval(Integral, 1, None, closed="left"),
Hidden(StrOptions({"deprecated"})),
],
},
prefer_skip_nested_validation=True,
)
def make_sparse_spd_matrix(
n_dim=None,
n_dim=1,
*,
alpha=0.95,
norm_diag=False,
smallest_coef=0.1,
largest_coef=0.9,
sparse_format=None,
random_state=None,
dim="deprecated",
):
"""Generate a sparse symmetric definite positive matrix.

Expand Down Expand Up @@ -1732,12 +1726,6 @@ def make_sparse_spd_matrix(
for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.

dim : int, default=1
The size of the random matrix to generate.

.. deprecated:: 1.4
`dim` is deprecated and will be removed in 1.6.

Returns
-------
prec : ndarray or sparse matrix of shape (dim, dim)
Expand Down Expand Up @@ -1765,32 +1753,10 @@ def make_sparse_spd_matrix(
"""
random_state = check_random_state(random_state)

# TODO(1.6): remove in 1.6
# Also make sure to change `n_dim` default back to 1 and deprecate None
if n_dim is not None and dim != "deprecated":
raise ValueError(
"`dim` and `n_dim` cannot be both specified. Please use `n_dim` only "
"as `dim` is deprecated in v1.4 and will be removed in v1.6."
)

if dim != "deprecated":
warnings.warn(
(
"dim was deprecated in version 1.4 and will be removed in 1.6."
"Please use ``n_dim`` instead."
),
FutureWarning,
)
_n_dim = dim
elif n_dim is None:
_n_dim = 1
else:
_n_dim = n_dim

chol = -sp.eye(_n_dim)
chol = -sp.eye(n_dim)
aux = sp.random(
m=_n_dim,
n=_n_dim,
m=n_dim,
n=n_dim,
density=1 - alpha,
data_rvs=lambda x: random_state.uniform(
low=smallest_coef, high=largest_coef, size=x
Expand All @@ -1802,7 +1768,7 @@ def make_sparse_spd_matrix(

# Permute the lines: we don't want to have asymmetries in the final
# SPD matrix
permutation = random_state.permutation(_n_dim)
permutation = random_state.permutation(n_dim)
aux = aux[permutation].T[permutation]
chol += aux
prec = chol.T @ chol
Expand Down
20 changes: 0 additions & 20 deletions sklearn/datasets/tests/test_samples_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,26 +552,6 @@ def test_make_sparse_spd_matrix(norm_diag, sparse_format, global_random_seed):
assert_array_almost_equal(Xarr.diagonal(), np.ones(n_dim))


# TODO(1.6): remove
def test_make_sparse_spd_matrix_deprecation_warning():
"""Check the message for future deprecation."""
warn_msg = "dim was deprecated in version 1.4"
with pytest.warns(FutureWarning, match=warn_msg):
make_sparse_spd_matrix(
dim=1,
)

error_msg = "`dim` and `n_dim` cannot be both specified"
with pytest.raises(ValueError, match=error_msg):
make_sparse_spd_matrix(
dim=1,
n_dim=1,
)

X = make_sparse_spd_matrix()
assert X.shape[1] == 1


@pytest.mark.parametrize("hole", [False, True])
def test_make_swiss_roll(hole):
X, t = make_swiss_roll(n_samples=5, noise=0.0, random_state=0, hole=hole)
Expand Down