Skip to content

Commit 687e84a

Browse files
authored
ENH avoid np.square(X) in enet_coordinate_descent to save memory (scikit-learn#31665)
1 parent 0202fd3 commit 687e84a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- class:`linear_model:ElasticNet` and class:`linear_model:Lasso` with
2+
`precompute=False` uses less memory for dense `X` and is a bit faster.
3+
By :user:`Christian Lorentzen <lorentzenchr>`

sklearn/linear_model/_cd_fast.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def enet_coordinate_descent(
139139
cdef unsigned int n_features = X.shape[1]
140140

141141
# compute norms of the columns of X
142-
cdef floating[::1] norm_cols_X = np.square(X).sum(axis=0)
142+
# same as norm_cols_X = np.square(X).sum(axis=0)
143+
cdef floating[::1] norm_cols_X = np.einsum(
144+
"ij,ij->j", X, X, dtype=dtype, order="C"
145+
)
143146

144147
# initial value of the residuals
145148
cdef floating[::1] R = np.empty(n_samples, dtype=dtype)

0 commit comments

Comments
 (0)