Skip to content

MAINT Deprecate utils.extmath.safe_min #14554

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 5 commits into from
Aug 2, 2019
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
3 changes: 1 addition & 2 deletions sklearn/decomposition/nmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from ..base import BaseEstimator, TransformerMixin
from ..utils import check_random_state, check_array
from ..utils.extmath import randomized_svd, safe_sparse_dot, squared_norm
from ..utils.extmath import safe_min
from ..utils.validation import check_is_fitted, check_non_negative
from ..exceptions import ConvergenceWarning
from .cdnmf_fast import _update_cdnmf_fast
Expand Down Expand Up @@ -1000,7 +999,7 @@ def non_negative_factorization(X, W=None, H=None, n_components=None,
check_non_negative(X, "NMF (input X)")
beta_loss = _check_string_param(solver, regularization, beta_loss, init)

if safe_min(X) == 0 and beta_loss <= 0:
if X.min() == 0 and beta_loss <= 0:
raise ValueError("When beta_loss <= 0 and X contains zeros, "
"the solver may diverge. Please add small values to "
"X, or use a positive beta_loss.")
Expand Down
7 changes: 6 additions & 1 deletion sklearn/utils/extmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ._logistic_sigmoid import _log_logistic_sigmoid
from .sparsefuncs_fast import csr_row_norms
from .validation import check_array
from .deprecation import deprecated


def squared_norm(x):
Expand Down Expand Up @@ -602,11 +603,15 @@ def softmax(X, copy=True):
return X


@deprecated("safe_min is deprecated in version 0.22 and will be removed "
"in version 0.24.")
def safe_min(X):
"""Returns the minimum value of a dense or a CSR/CSC matrix.

Adapated from https://stackoverflow.com/q/13426580

.. deprecated:: 0.22.0

Parameters
----------
X : array_like
Expand Down Expand Up @@ -646,7 +651,7 @@ def make_nonnegative(X, min_value=0):
ValueError
When X is sparse
"""
min_ = safe_min(X)
min_ = X.min()
if min_ < min_value:
if sparse.issparse(X):
raise ValueError("Cannot make the data matrix"
Expand Down
8 changes: 8 additions & 0 deletions sklearn/utils/tests/test_extmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from sklearn.utils.extmath import _deterministic_vector_sign_flip
from sklearn.utils.extmath import softmax
from sklearn.utils.extmath import stable_cumsum
from sklearn.utils.extmath import safe_min
from sklearn.datasets.samples_generator import make_low_rank_matrix


Expand Down Expand Up @@ -640,3 +641,10 @@ def test_stable_cumsum():
assert_array_equal(stable_cumsum(A, axis=0), np.cumsum(A, axis=0))
assert_array_equal(stable_cumsum(A, axis=1), np.cumsum(A, axis=1))
assert_array_equal(stable_cumsum(A, axis=2), np.cumsum(A, axis=2))


def test_safe_min():
msg = ("safe_min is deprecated in version 0.22 and will be removed "
"in version 0.24.")
with pytest.warns(DeprecationWarning, match=msg):
safe_min(np.ones(10))