Skip to content

Commit c838875

Browse files
authored
ENH inplace addition in newton_cg (scikit-learn#27417)
1 parent 87cfae2 commit c838875

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

doc/whats_new/v1.4.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ Changelog
196196
:class:`scipy.sparse.sparray` subclasses.
197197
:pr:`27301` by :user:`Lohit SundaramahaLingam <lohitslohit>`.
198198

199+
:mod:`sklearn.linear_model`
200+
...........................
201+
202+
- |Enhancement| Solver `"newton-cg"` in :class:`LogisticRegression` and
203+
:class:`LogisticRegressionCV` uses a little less memory. The effect is proportional
204+
to the number of coefficients (`n_features * n_classes`).
205+
:pr:`27417` by :user:`Christian Lorentzen <lorentzenchr>`.
206+
199207
:mod:`sklearn.metrics`
200208
......................
201209

sklearn/utils/optimize.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _cg(fhess_p, fgrad, maxiter, tol):
7777
Estimated solution.
7878
"""
7979
xsupi = np.zeros(len(fgrad), dtype=fgrad.dtype)
80-
ri = fgrad
80+
ri = np.copy(fgrad)
8181
psupi = -ri
8282
i = 0
8383
dri0 = np.dot(ri, ri)
@@ -100,7 +100,7 @@ def _cg(fhess_p, fgrad, maxiter, tol):
100100
break
101101
alphai = dri0 / curv
102102
xsupi += alphai * psupi
103-
ri = ri + alphai * Ap
103+
ri += alphai * Ap
104104
dri1 = np.dot(ri, ri)
105105
betai = dri1 / dri0
106106
psupi = -ri + betai * psupi
@@ -168,7 +168,7 @@ def _newton_cg(
168168
Estimated minimum.
169169
"""
170170
x0 = np.asarray(x0).flatten()
171-
xk = x0
171+
xk = np.copy(x0)
172172
k = 0
173173

174174
if line_search:
@@ -204,7 +204,7 @@ def _newton_cg(
204204
warnings.warn("Line Search failed")
205205
break
206206

207-
xk = xk + alphak * xsupi # upcast if necessary
207+
xk += alphak * xsupi # upcast if necessary
208208
k += 1
209209

210210
if warn and k >= maxiter:

0 commit comments

Comments
 (0)