Skip to content

Commit

Permalink
FIX Ensure coef_ is an ndarray when fitting LassoLars (scikit-learn#8160
Browse files Browse the repository at this point in the history
)

* Fix scikit-learngh-1615: ensure self.coef_ is an ndarray
  • Loading branch information
perimosocordiae authored and maskani-moh committed Nov 15, 2017
1 parent d1fb37b commit 4aff885
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions doc/whats_new.rst
Expand Up @@ -194,6 +194,10 @@ Bug fixes
would be raised on trying to stack matrices with different dimensions.
:issue:`8093` by :user:`Peter Bull <pjbull>`.

- Fix a bug where :func:`sklearn.linear_model.LassoLars.fit` sometimes
left `coef_` as a list, rather than an ndarray.
:issue:`8160` by :user:`CJ Carey <perimosocordiae>`.

API changes summary
-------------------

Expand Down
5 changes: 2 additions & 3 deletions sklearn/linear_model/least_angle.py
Expand Up @@ -665,9 +665,9 @@ def fit(self, X, y, Xy=None):

self.alphas_ = []
self.n_iter_ = []
self.coef_ = np.empty((n_targets, n_features))

if self.fit_path:
self.coef_ = []
self.active_ = []
self.coef_path_ = []
for k in xrange(n_targets):
Expand All @@ -682,15 +682,14 @@ def fit(self, X, y, Xy=None):
self.active_.append(active)
self.n_iter_.append(n_iter_)
self.coef_path_.append(coef_path)
self.coef_.append(coef_path[:, -1])
self.coef_[k] = coef_path[:, -1]

if n_targets == 1:
self.alphas_, self.active_, self.coef_path_, self.coef_ = [
a[0] for a in (self.alphas_, self.active_, self.coef_path_,
self.coef_)]
self.n_iter_ = self.n_iter_[0]
else:
self.coef_ = np.empty((n_targets, n_features))
for k in xrange(n_targets):
this_Xy = None if Xy is None else Xy[:, k]
alphas, _, self.coef_[k], n_iter_ = lars_path(
Expand Down
11 changes: 9 additions & 2 deletions sklearn/linear_model/tests/test_least_angle.py
Expand Up @@ -366,8 +366,15 @@ def test_multitarget():
X = diabetes.data
Y = np.vstack([diabetes.target, diabetes.target ** 2]).T
n_targets = Y.shape[1]

for estimator in (linear_model.LassoLars(), linear_model.Lars()):
estimators = [
linear_model.LassoLars(),
linear_model.Lars(),
# regression test for gh-1615
linear_model.LassoLars(fit_intercept=False),
linear_model.Lars(fit_intercept=False),
]

for estimator in estimators:
estimator.fit(X, Y)
Y_pred = estimator.predict(X)
alphas, active, coef, path = (estimator.alphas_, estimator.active_,
Expand Down

0 comments on commit 4aff885

Please sign in to comment.