Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0512ba8

Browse files
dalmiaSundrique
authored andcommitted
[MRG+2] ENH: used SelectorMixin in BaseRandomizedLinearModel (scikit-learn#8263)
* ENH: used SelectorMixin in BaseRandomizedLinearModel * FIX: added get_support to return _get_support_mask * FIX: added docstring for get_support() * DOC: added bug fix entry to whats_new * FIX: removed redundant get_support()
1 parent f211ffd commit 0512ba8

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

doc/whats_new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ Enhancements
150150
Bug fixes
151151
.........
152152

153+
- Fixed a bug where :class:`sklearn.linear_model.RandomizedLasso` and
154+
:class:`sklearn.linear_model.RandomizedLogisticRegression` breaks for
155+
sparse input.
156+
:issue:`8259` by :user:`Aman Dalmia <dalmia>`.
157+
153158
- Fixed a bug where :func:`sklearn.datasets.make_moons` gives an
154159
incorrect result when ``n_samples`` is odd.
155160
:issue:`8198` by :user:`Josh Levy <levy5674>`.

sklearn/linear_model/randomized_l1.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from scipy.interpolate import interp1d
1717

1818
from .base import _preprocess_data
19-
from ..base import BaseEstimator, TransformerMixin
19+
from ..base import BaseEstimator
2020
from ..externals import six
2121
from ..externals.joblib import Memory, Parallel, delayed
22-
from ..utils import (as_float_array, check_random_state, check_X_y,
23-
check_array, safe_mask)
22+
from ..feature_selection.base import SelectorMixin
23+
from ..utils import (as_float_array, check_random_state, check_X_y, safe_mask)
2424
from ..utils.validation import check_is_fitted
2525
from .least_angle import lars_path, LassoLarsIC
2626
from .logistic import LogisticRegression
@@ -59,7 +59,7 @@ def _resample_model(estimator_func, X, y, scaling=.5, n_resampling=200,
5959

6060

6161
class BaseRandomizedLinearModel(six.with_metaclass(ABCMeta, BaseEstimator,
62-
TransformerMixin)):
62+
SelectorMixin)):
6363
"""Base class to implement randomized linear models for feature selection
6464
6565
This implements the strategy by Meinshausen and Buhlman:
@@ -87,7 +87,7 @@ def fit(self, X, y):
8787
Returns
8888
-------
8989
self : object
90-
Returns an instance of self.
90+
Returns an instance of self.
9191
"""
9292
X, y = check_X_y(X, y, ['csr', 'csc'], y_numeric=True,
9393
ensure_min_samples=2, estimator=self)
@@ -121,31 +121,17 @@ def _make_estimator_and_params(self, X, y):
121121
"""Return the parameters passed to the estimator"""
122122
raise NotImplementedError
123123

124-
def get_support(self, indices=False):
125-
"""Return a mask, or list, of the features/indices selected."""
126-
check_is_fitted(self, 'scores_')
124+
def _get_support_mask(self):
125+
"""Get the boolean mask indicating which features are selected.
127126
128-
mask = self.scores_ > self.selection_threshold
129-
return mask if not indices else np.where(mask)[0]
130-
131-
# XXX: the two function below are copy/pasted from feature_selection,
132-
# Should we add an intermediate base class?
133-
def transform(self, X):
134-
"""Transform a new matrix using the selected features"""
135-
mask = self.get_support()
136-
X = check_array(X)
137-
if len(mask) != X.shape[1]:
138-
raise ValueError("X has a different shape than during fitting.")
139-
return check_array(X)[:, safe_mask(X, mask)]
140-
141-
def inverse_transform(self, X):
142-
"""Transform a new matrix using the selected features"""
143-
support = self.get_support()
144-
if X.ndim == 1:
145-
X = X[None, :]
146-
Xt = np.zeros((X.shape[0], support.size))
147-
Xt[:, support] = X
148-
return Xt
127+
Returns
128+
-------
129+
support : boolean array of shape [# input features]
130+
An element is True iff its corresponding feature is selected
131+
for retention.
132+
"""
133+
check_is_fitted(self, 'scores_')
134+
return self.scores_ > self.selection_threshold
149135

150136

151137
###############################################################################

0 commit comments

Comments
 (0)