From 6b05f14b352c86694a09c951b722aba5a27bde9b Mon Sep 17 00:00:00 2001 From: Lars Buitinck Date: Mon, 16 Sep 2013 13:09:35 +0200 Subject: [PATCH 1/2] MAINT get rid of decision_function in linear regression This method only existed to support an undocumented feature in sklearn.multiclass. Fixes #1404. --- sklearn/linear_model/base.py | 19 ++----------------- sklearn/linear_model/coordinate_descent.py | 18 ------------------ .../linear_model/tests/test_least_angle.py | 2 -- 3 files changed, 2 insertions(+), 37 deletions(-) diff --git a/sklearn/linear_model/base.py b/sklearn/linear_model/base.py index d9d72d9a9b8f4..592ad7c6ccb9d 100644 --- a/sklearn/linear_model/base.py +++ b/sklearn/linear_model/base.py @@ -126,8 +126,8 @@ class LinearModel(six.with_metaclass(ABCMeta, BaseEstimator)): def fit(self, X, y): """Fit model.""" - def decision_function(self, X): - """Decision function of the linear model. + def predict(self, X): + """Predict using the linear model. Parameters ---------- @@ -143,21 +143,6 @@ def decision_function(self, X): return safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_ - def predict(self, X): - """Predict using the linear model - - Parameters - ---------- - X : {array-like, sparse matrix}, shape = (n_samples, n_features) - Samples. - - Returns - ------- - C : array, shape = (n_samples,) - Returns predicted values. - """ - return self.decision_function(X) - _center_data = staticmethod(center_data) def _set_intercept(self, X_mean, y_mean, X_std): diff --git a/sklearn/linear_model/coordinate_descent.py b/sklearn/linear_model/coordinate_descent.py index 9795ac9de0e85..cb51fe4a5ec14 100644 --- a/sklearn/linear_model/coordinate_descent.py +++ b/sklearn/linear_model/coordinate_descent.py @@ -628,24 +628,6 @@ def sparse_coef_(self): """ sparse representation of the fitted coef """ return sparse.csr_matrix(self.coef_) - def decision_function(self, X): - """Decision function of the linear model - - Parameters - ---------- - X : numpy array or scipy.sparse matrix of shape (n_samples, n_features) - - Returns - ------- - T : array, shape = (n_samples,) - The predicted decision function - """ - if sparse.isspmatrix(X): - return np.ravel(safe_sparse_dot(self.coef_, X.T, dense_output=True) - + self.intercept_) - else: - return super(ElasticNet, self).decision_function(X) - ############################################################################### # Lasso model diff --git a/sklearn/linear_model/tests/test_least_angle.py b/sklearn/linear_model/tests/test_least_angle.py index 8ca816f608a17..1054f0e2defd9 100644 --- a/sklearn/linear_model/tests/test_least_angle.py +++ b/sklearn/linear_model/tests/test_least_angle.py @@ -399,8 +399,6 @@ def test_multitarget(): for estimator in (linear_model.LassoLars(), linear_model.Lars()): estimator.fit(X, Y) Y_pred = estimator.predict(X) - Y_dec = estimator.decision_function(X) - assert_array_almost_equal(Y_pred, Y_dec) alphas, active, coef, path = (estimator.alphas_, estimator.active_, estimator.coef_, estimator.coef_path_) for k in range(n_targets): From 276c75826dadef74ea54a0428de5b323562d5392 Mon Sep 17 00:00:00 2001 From: Lars Buitinck Date: Mon, 16 Sep 2013 13:10:26 +0200 Subject: [PATCH 2/2] ENH sparsify and densify methods for CD models --- sklearn/linear_model/coordinate_descent.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sklearn/linear_model/coordinate_descent.py b/sklearn/linear_model/coordinate_descent.py index cb51fe4a5ec14..f767af0f37ea9 100644 --- a/sklearn/linear_model/coordinate_descent.py +++ b/sklearn/linear_model/coordinate_descent.py @@ -14,7 +14,7 @@ import numpy as np from scipy import sparse -from .base import LinearModel, _pre_fit +from .base import LinearModel, _pre_fit, SparseCoefMixin from ..base import RegressorMixin from .base import center_data from ..utils import array2d, atleast2d_or_csc, deprecated @@ -445,7 +445,7 @@ def enet_path(X, y, l1_ratio=0.5, eps=1e-3, n_alphas=100, alphas=None, # ElasticNet model -class ElasticNet(LinearModel, RegressorMixin): +class ElasticNet(LinearModel, RegressorMixin, SparseCoefMixin): """Linear Model trained with L1 and L2 prior as regularizer Minimizes the objective function:: @@ -625,7 +625,10 @@ def fit(self, X, y, coef_init=None): @property def sparse_coef_(self): - """ sparse representation of the fitted coef """ + """Sparse representation of the fitted coef_.""" + warnings.warn("sparse_coef_ is deprecated and will be removed in 0.17." + " Call the sparsify method instead.", + DeprecationWarning) return sparse.csr_matrix(self.coef_)