8000 Deprecate utils.{optimize.newton_cg,random.random_choice_csc} (#15040) · scikit-learn/scikit-learn@c4f372c · GitHub
[go: up one dir, main page]

Skip to content

Commit c4f372c

Browse files
NicolasHugrth
authored andcommitted
Deprecate utils.{optimize.newton_cg,random.random_choice_csc} (#15040)
1 parent c96c73b commit c4f372c

File tree

8 files changed

+73
-16
lines changed

8 files changed

+73
-16
lines changed

doc/whats_new/v0.22.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ Changelog
573573
- |API| The following utils have been deprecated and are now private:
574574
- ``choose_check_classifiers_labels``
575575
- ``enforce_estimator_tags_y``
576+
- ``optimize.newton_cg`
577+
- ``random.random_choice_csc``
576578

577579
- |Enhancement| :func:`utils.safe_indexing` accepts an ``axis`` parameter to
578580
index array-like across rows and columns. The column indexing can be done on

sklearn/dummy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .utils.validation import check_array
1515
from .utils.validation import check_consistent_length
1616
from .utils.validation import check_is_fitted
17-
from .utils.random import random_choice_csc
17+
from .utils.random import _random_choice_csc
1818
from .utils.stats import _weighted_percentile
1919
from .utils.multiclass import class_distribution
2020
from .utils import deprecated
@@ -211,7 +211,7 @@ def predict(self, X):
211211
elif self.strategy == "constant":
212212
classes_ = [np.array([c]) for c in constant]
213213

214-
y = random_choice_csc(n_samples, classes_, class_prob,
214+
y = _random_choice_csc(n_samples, classes_, class_prob,
215215
self.random_state)
216216
else:
217217
if self.strategy in ("most_frequent", "prior"):

sklearn/linear_model/logistic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
squared_norm)
2929
from ..utils.extmath import row_norms
3030
from ..utils.fixes import logsumexp
31-
from ..utils.optimize import newton_cg, _check_optimize_result
31+
from ..utils.optimize import _newton_cg, _check_optimize_result
3232
from ..utils.validation import check_X_y
3333
from ..utils.validation import check_is_fitted, _check_sample_weight
3434
from ..utils import deprecated
@@ -933,8 +933,8 @@ def _logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
933933
w0, loss = opt_res.x, opt_res.fun
934934
elif solver == 'newton-cg':
935935
args = (X, target, 1. / C, sample_weight)
936-
w0, n_iter_i = newton_cg(hess, func, grad, w0, args=args,
937-
maxiter=max_iter, tol=tol)
936+
w0, n_iter_i = _newton_cg(hess, func, grad, w0, args=args,
937+
maxiter=max_iter, tol=tol)
938938
elif solver == 'liblinear':
939939
coef_, intercept_, n_iter_i, = _fit_liblinear(
940940
X, target, C, fit_intercept, intercept_scaling, None,

sklearn/utils/optimize.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from scipy.optimize.linesearch import line_search_wolfe2, line_search_wolfe1
1919

2020
from ..exceptions import ConvergenceWarning
21+
from . import deprecated
2122

2223

2324
class _LineSearchError(RuntimeError):
@@ -111,8 +112,16 @@ def _cg(fhess_p, fgrad, maxiter, tol):
111112
return xsupi
112113

113114

115+
@deprecated("newton_cg is deprecated in version "
116+
"0.22 and will be removed in version 0.24.")
114117
def newton_cg(grad_hess, func, grad, x0, args=(), tol=1e-4,
115118
maxiter=100, maxinner=200, line_search=True, warn=True):
119+
return _newton_cg(grad_hess, func, grad, x0, args, tol, maxiter,
120+
maxinner, line_search, warn)
121+
122+
123+
def _newton_cg(grad_hess, func, grad, x0, args=(), tol=1e-4,
124+
maxiter=100, maxinner=200, line_search=True, warn=True):
116125
"""
117126
Minimization of scalar function of one or more variables using the
118127
Newton-CG algorithm.

sklearn/utils/random.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77

88
from . import check_random_state
99
from ._random import sample_without_replacement
10+
from . import deprecated
1011

1112
__all__ = ['sample_without_replacement']
1213

1314

15+
@deprecated("random_choice_csc is deprecated in version "
16+
"0.22 and will be removed in version 0.24.")
1417
def random_choice_csc(n_samples, classes, class_probability=None,
1518
random_state=None):
19+
return _random_choice_csc(n_samples, classes, class_probability,
20+
random_state)
21+
22+
23+
def _random_choice_csc(n_samples, classes, class_probability=None,
24+
random_state=None):
1625
"""Generate a sparse random matrix given column class distributions
1726
1827
Parameters

sklearn/utils/tests/test_deprecated_utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,73 @@
88
from sklearn.utils.estimator_checks import is_public_parameter
99
from sklearn.utils.estimator_checks import pairwise_estimator_convert_X
1010
from sklearn.utils.estimator_checks import set_checking_parameters
11+
from sklearn.utils.optimize import newton_cg
12+
from sklearn.utils.random import random_choice_csc
1113

1214

1315
# This file tests the utils that are deprecated
1416

1517

18+
# TODO: remove in 0.24
1619
def test_choose_check_classifiers_labels_deprecated():
1720
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
1821
choose_check_classifiers_labels(None, None, None)
1922

2023

24+
# TODO: remove in 0.24
2125
def test_enforce_estimator_tags_y():
2226
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
2327
enforce_estimator_tags_y(DummyClassifier(), np.array([0, 1]))
2428

2529

30+
# TODO: remove in 0.24
2631
def test_notanarray():
2732
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
2833
NotAnArray([1, 2])
2934

3035

36+
# TODO: remove in 0.24
3137
def test_is_public_parameter():
3238
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
3339
is_public_parameter('hello')
3440

3541

42+
# TODO: remove in 0.24
3643
def test_pairwise_estimator_convert_X():
3744
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
3845
pairwise_estimator_convert_X([[1, 2]], DummyClassifier())
3946

4047

48+
# TODO: remove in 0.24
4149
def test_set_checking_parameters():
4250
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
4351
set_checking_parameters(DummyClassifier())
52+
53+
54+
# TODO: remove in 0.24
55+
def test_newton_cg():
56+
rng = np.random.RandomState(0)
57+
A = rng.normal(size=(10, 10))
58+
x0 = np.ones(10)
59+
60+
def func(x):
61+
Ax = A.dot(x)
62+
return .5 * (Ax).dot(Ax)
63+
64+
def grad(x):
65+
return A.T.dot(A.dot(x))
66+
67+
def hess(x, p):
68+
return p.dot(A.T.dot(A.dot(x.all())))
69+
70+
def grad_hess(x):
71+
return grad(x), lambda x: A.T.dot(A.dot(x))
72+
73+
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
74+
newton_cg(grad_hess, func, grad, x0)
75+
76+
77+
# TODO: remove in 0.24
78+
def test_random_choice_csc():
79+
with pytest.warns(DeprecationWarning, match="removed in version 0.24"):
80+
random_choice_csc(10, [[2]])

sklearn/utils/tests/test_optimize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from sklearn.utils.optimize import newton_cg
3+
from sklearn.utils.optimize import _newton_cg
44
from scipy.optimize import fmin_ncg
55

66
from sklearn.utils.testing import assert_array_almost_equal
@@ -27,6 +27,6 @@ def grad_hess(x):
2727
return grad(x), lambda x: A.T.dot(A.dot(x))
2828

2929
assert_array_almost_equal(
30-
newton_cg(grad_hess, func, grad, x0, tol=1e-10)[0],
30+
_newton_cg(grad_hess, func, grad, x0, tol=1e-10)[0],
3131
fmin_ncg(f=func, x0=x0, fprime=grad, fhess_p=hess)
3232
)

sklearn/utils/tests/test_random.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from numpy.testing import assert_array_almost_equal
44

55
from sklearn.utils.fixes import comb
6-
from sklearn.utils.random import random_choice_csc, sample_without_replacement
6+
from sklearn.utils.random import _random_choice_csc, sample_without_replacement
77
from sklearn.utils._random import _our_rand_r_py
88
from sklearn.utils.testing import assert_raises
99

@@ -103,7 +103,7 @@ def test_random_choice_csc(n_samples=10000, random_state=24):
103103
classes = [np.array([0, 1]), np.array([0, 1, 2])]
104104
class_probabilities = [np.array([0.5, 0.5]), np.array([0.6, 0.1, 0.3])]
105105

106-
got = random_choice_csc(n_samples, classes, class_probabilities,
106+
got = _random_choice_csc(n_samples, classes, class_probabilities,
107107
random_state)
108108
assert sp.issparse(got)
109109

@@ -115,7 +115,7 @@ def test_random_choice_csc(n_samples=10000, random_state=24):
115115
classes = [[0, 1], [1, 2]] # test for array-like support
116116
class_probabilities = [np.array([0.5, 0.5]), np.array([0, 1/2, 1/2])]
117117

118-
got = random_choice_csc(n_samples=n_samples,
118+
got = _random_choice_csc(n_samples=n_samples,
119119
classes=classes,
120120
random_state=random_state)
121121
assert sp.issparse(got)
@@ -128,7 +128,7 @@ def test_random_choice_csc(n_samples=10000, random_state=24):
128128
classes = [np.array([0, 1]), np.array([0, 1, 2])]
129129
class_probabilities = [np.array([1.0, 0.0]), np.array([0.0, 1.0, 0.0])]
130130

131-
got = random_choice_csc(n_samples, classes, class_probabilities,
131+
got = _random_choice_csc(n_samples, classes, class_probabilities,
132132
random_state)
133133
assert sp.issparse(got)
134134

@@ -141,7 +141,7 @@ def test_random_choice_csc(n_samples=10000, random_state=24):
141141
classes = [[1], [0]] # test for array-like support
142142
class_probabilities = [np.array([0.0, 1.0]), np.array([1.0])]
143143

144-
got = random_choice_csc(n_samples=n_samples,
144+
got = _random_choice_csc(n_samples=n_samples,
145145
classes=classes,
146146
random_state=random_state)
147147
assert sp.issparse(got)
@@ -155,25 +155,25 @@ def test_random_choice_csc_errors():
155155
# the length of an array in classes and class_probabilities is mismatched
156156
classes = [np.array([0, 1]), np.array([0, 1, 2, 3])]
157157
class_probabilities = [np.array([0.5, 0.5]), np.array([0.6, 0.1, 0.3])]
158-
assert_raises(ValueError, random_choice_csc, 4, classes,
158+
assert_raises(ValueError, _random_choice_csc, 4, classes,
159159
class_probabilities, 1)
160160

161161
# the class dtype is not supported
162162
classes = [np.array(["a", "1"]), np.array(["z", "1", "2"])]
163163
class_probabilities = [np.array([0.5, 0.5]), np.array([0.6, 0.1, 0.3])]
164-
assert_raises(ValueError, random_choice_csc, 4, classes,
164+
assert_raises(ValueError, _random_choice_csc, 4, classes,
165165
class_probabilities, 1)
166166

167167
# the class dtype is not supported
168168
classes = [np.array([4.2, 0.1]), np.array([0.1, 0.2, 9.4])]
169169
class_probabilities = [np.array([0.5, 0.5]), np.array([0.6, 0.1, 0.3])]
170-
assert_raises(ValueError, random_choice_csc, 4, classes,
170+
assert_raises(ValueError, _random_choice_csc, 4, classes,
171171
class_probabilities, 1)
172172

173173
# Given probabilities don't sum to 1
174174
classes = [np.array([0, 1]), np.array([0, 1, 2])]
175175
class_probabilities = [np.array([0.5, 0.6]), np.array([0.6, 0.1, 0.3])]
176-
assert_raises(ValueError, random_choice_csc, 4, classes,
176+
assert_raises(ValueError, _random_choice_csc, 4, classes,
177177
class_probabilities, 1)
178178

179179

0 commit comments

Comments
 (0)
0