diff --git a/doc/whats_new/v0.20.rst b/doc/whats_new/v0.20.rst index c607174626171..193204002664a 100644 --- a/doc/whats_new/v0.20.rst +++ b/doc/whats_new/v0.20.rst @@ -246,6 +246,10 @@ Classifiers and regressors overridden when using parameter ``copy_X=True`` and ``check_input=False``. :issue:`10581` by :user:`Yacine Mazari `. +- Fixed a bug in :class:`sklearn.linear_model.Lasso` + where the coefficient had wrong shape when ``fit_intercept=False``. + :issue:`10687` by :user:`Martin Hahn `. + Decomposition, manifold learning and clustering - Fix for uninformative error in :class:`decomposition.IncrementalPCA`: diff --git a/sklearn/linear_model/coordinate_descent.py b/sklearn/linear_model/coordinate_descent.py index 32de16e2f8f27..6317da6b8fe2b 100644 --- a/sklearn/linear_model/coordinate_descent.py +++ b/sklearn/linear_model/coordinate_descent.py @@ -762,8 +762,12 @@ def fit(self, X, y, check_input=True): if n_targets == 1: self.n_iter_ = self.n_iter_[0] + self.coef_ = coef_[0] + self.dual_gap_ = dual_gaps_[0] + else: + self.coef_ = coef_ + self.dual_gap_ = dual_gaps_ - self.coef_, self.dual_gap_ = map(np.squeeze, [coef_, dual_gaps_]) self._set_intercept(X_offset, y_offset, X_scale) # workaround since _set_intercept will cast self.coef_ into X.dtype diff --git a/sklearn/linear_model/tests/test_coordinate_descent.py b/sklearn/linear_model/tests/test_coordinate_descent.py index aeeb5d1582288..a3b35f40a88d7 100644 --- a/sklearn/linear_model/tests/test_coordinate_descent.py +++ b/sklearn/linear_model/tests/test_coordinate_descent.py @@ -803,3 +803,9 @@ def test_enet_l1_ratio(): est.fit(X, y[:, None]) est_desired.fit(X, y[:, None]) assert_array_almost_equal(est.coef_, est_desired.coef_, decimal=5) + + +def test_coef_shape_not_zero(): + est_no_intercept = Lasso(fit_intercept=False) + est_no_intercept.fit(np.c_[np.ones(3)], np.ones(3)) + assert est_no_intercept.coef_.shape == (1,)