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

Skip to content

set force_all_finite=False for SelectFromModel.transform #11026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sklearn/feature_selection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def transform(self, X):
X_r : array of shape [n_samples, n_selected_features]
The input samples with only the selected features.
"""
X = check_array(X, accept_sparse='csr')
return self._transform(X)

def _transform(self, X, force_all_finite=True):
X = check_array(X, force_all_finite=force_all_finite,
accept_sparse='csr')
mask = self.get_support()
if not mask.any():
warn("No features were selected: either the data is"
Expand Down
3 changes: 3 additions & 0 deletions sklearn/feature_selection/from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def _get_support_mask(self):
threshold = _calculate_threshold(estimator, scores, self.threshold)
return scores >= threshold

def transform(self, X):
return self._transform(X, False)

def fit(self, X, y=None, **fit_params):
"""Fit the SelectFromModel meta-transformer.

Expand Down
15 changes: 15 additions & 0 deletions sklearn/feature_selection/tests/test_from_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from sklearn.impute import SimpleImputer
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_false
from sklearn.utils.testing import assert_equal
Expand Down Expand Up @@ -198,3 +199,17 @@ def test_threshold_without_refitting():
# Set a higher threshold to filter out more features.
model.threshold = "1.0 * mean"
assert_greater(X_transform.shape[1], model.transform(data).shape[1])


def test_transform_accepts_infinite_data():
# Test that transform doesn't check for np.inf and np.nan values.
est = SimpleImputer(strategy='mean', missing_values='NaN')
model = SelectFromModel(estimator=est)
data[2, :] = np.nan
model.fit(data, y)

X_len = len(model.get_support())
X = np.arange(X_len).reshape(1, X_len)
X.fill(np.inf)

model.transform(X)