8000 FIX Ensure coef_ is an ndarray when fitting LassoLars (#8160) · sergeyf/scikit-learn@6868707 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6868707

Browse files
perimosocordiaesergeyf
authored andcommitted
FIX Ensure coef_ is an ndarray when fitting LassoLars (scikit-learn#8160)
* Fix scikit-learngh-1615: ensure self.coef_ is an ndarray
1 parent 0414302 commit 6868707

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

doc/whats_new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ Bug fixes
194194
would be raised on trying to stack matrices with different dimensions.
195195
:issue:`8093` by :user:`Peter Bull <pjbull>`.
196196

197+
- Fix a bug where :func:`sklearn.linear_model.LassoLars.fit` sometimes
198+
left `coef_` as a list, rather than an ndarray.
199+
:issue:`8160` by :user:`CJ Carey <perimosocordiae>`.
200+
197201
API changes summary
198202
-------------------
199203

sklearn/linear_model/least_angle.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,9 @@ def fit(self, X, y, Xy=None):
665665

666666
self.alphas_ = []
667667
self.n_iter_ = []
668+
self.coef_ = np.empty((n_targets, n_features))
668669

669670
if self.fit_path:
670-
self.coef_ = []
671671
self.active_ = []
672672
self.coef_path_ = []
673673
for k in xrange(n_targets):
@@ -682,15 +682,14 @@ def fit(self, X, y, Xy=None):
682682
self.active_.append(active)
683683
self.n_iter_.append(n_iter_)
684684
self.coef_path_.append(coef_path)
685-
self.coef_.append(coef_path[:, -1])
685+
self.coef_[k] = coef_path[:, -1]
686686

687687
if n_targets == 1:
688688
self.alphas_, self.active_, self.coef_path_, self.coef_ = [
689689
a[0] for a in (self.alphas_, self.active_, self.coef_path_,
690690
self.coef_)]
691691
self.n_iter_ = self.n_iter_[0]
692692
else:
693-
self.coef_ = np.empty((n_targets, n_features))
694693
for k in xrange(n_targets):
695694
this_Xy = None if Xy is None else Xy[:, k]
696695
alphas, _, self.coef_[k], n_iter_ = lars_path(

sklearn/linear_model/tests/test_least_angle.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,15 @@ def test_multitarget():
366366
X = diabetes.data
367367
Y = np.vstack([diabetes.target, diabetes.target ** 2]).T
368368
n_targets = Y.shape[1]
369-
370-
for estimator in (linear_model.LassoLars(), linear_model.Lars()):
369+
estimators = [
370+
linear_model.LassoLars(),
371+
linear_model.Lars(),
372+
# regression test for gh-1615
373+
linear_model.LassoLars(fit_intercept=False),
374+
linear_model.Lars(fit_intercept=False),
375+
]
376+
377+
for estimator in estimators:
371378
estimator.fit(X, Y)
372379
Y_pred = estimator.predict(X)
373380
alphas, active, coef, path = (estimator.alphas_, estimator.active_,

0 commit comments

Comments
 (0)
0