some new features
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,153 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
from numpy.testing import assert_array_equal
|
||||
|
||||
from sklearn.base import BaseEstimator
|
||||
from sklearn.feature_selection._base import SelectorMixin
|
||||
from sklearn.utils.fixes import CSC_CONTAINERS
|
||||
|
||||
|
||||
class StepSelector(SelectorMixin, BaseEstimator):
|
||||
"""Retain every `step` features (beginning with 0).
|
||||
|
||||
If `step < 1`, then no features are selected.
|
||||
"""
|
||||
|
||||
def __init__(self, step=2):
|
||||
self.step = step
|
||||
|
||||
def fit(self, X, y=None):
|
||||
X = self._validate_data(X, accept_sparse="csc")
|
||||
return self
|
||||
|
||||
def _get_support_mask(self):
|
||||
mask = np.zeros(self.n_features_in_, dtype=bool)
|
||||
if self.step >= 1:
|
||||
mask[:: self.step] = True
|
||||
return mask
|
||||
|
||||
|
||||
support = [True, False] * 5
|
||||
support_inds = [0, 2, 4, 6, 8]
|
||||
X = np.arange(20).reshape(2, 10)
|
||||
Xt = np.arange(0, 20, 2).reshape(2, 5)
|
||||
Xinv = X.copy()
|
||||
Xinv[:, 1::2] = 0
|
||||
y = [0, 1]
|
||||
feature_names = list("ABCDEFGHIJ")
|
||||
feature_names_t = feature_names[::2]
|
||||
feature_names_inv = np.array(feature_names)
|
||||
feature_names_inv[1::2] = ""
|
||||
|
||||
|
||||
def test_transform_dense():
|
||||
sel = StepSelector()
|
||||
Xt_actual = sel.fit(X, y).transform(X)
|
||||
Xt_actual2 = StepSelector().fit_transform(X, y)
|
||||
assert_array_equal(Xt, Xt_actual)
|
||||
assert_array_equal(Xt, Xt_actual2)
|
||||
|
||||
# Check dtype matches
|
||||
assert np.int32 == sel.transform(X.astype(np.int32)).dtype
|
||||
assert np.float32 == sel.transform(X.astype(np.float32)).dtype
|
||||
|
||||
# Check 1d list and other dtype:
|
||||
names_t_actual = sel.transform([feature_names])
|
||||
assert_array_equal(feature_names_t, names_t_actual.ravel())
|
||||
|
||||
# Check wrong shape raises error
|
||||
with pytest.raises(ValueError):
|
||||
sel.transform(np.array([[1], [2]]))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csc_container", CSC_CONTAINERS)
|
||||
def test_transform_sparse(csc_container):
|
||||
X_sp = csc_container(X)
|
||||
sel = StepSelector()
|
||||
Xt_actual = sel.fit(X_sp).transform(X_sp)
|
||||
Xt_actual2 = sel.fit_transform(X_sp)
|
||||
assert_array_equal(Xt, Xt_actual.toarray())
|
||||
assert_array_equal(Xt, Xt_actual2.toarray())
|
||||
|
||||
# Check dtype matches
|
||||
assert np.int32 == sel.transform(X_sp.astype(np.int32)).dtype
|
||||
assert np.float32 == sel.transform(X_sp.astype(np.float32)).dtype
|
||||
|
||||
# Check wrong shape raises error
|
||||
with pytest.raises(ValueError):
|
||||
sel.transform(np.array([[1], [2]]))
|
||||
|
||||
|
||||
def test_inverse_transform_dense():
|
||||
sel = StepSelector()
|
||||
Xinv_actual = sel.fit(X, y).inverse_transform(Xt)
|
||||
assert_array_equal(Xinv, Xinv_actual)
|
||||
|
||||
# Check dtype matches
|
||||
assert np.int32 == sel.inverse_transform(Xt.astype(np.int32)).dtype
|
||||
assert np.float32 == sel.inverse_transform(Xt.astype(np.float32)).dtype
|
||||
|
||||
# Check 1d list and other dtype:
|
||||
names_inv_actual = sel.inverse_transform([feature_names_t])
|
||||
assert_array_equal(feature_names_inv, names_inv_actual.ravel())
|
||||
|
||||
# Check wrong shape raises error
|
||||
with pytest.raises(ValueError):
|
||||
sel.inverse_transform(np.array([[1], [2]]))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csc_container", CSC_CONTAINERS)
|
||||
def test_inverse_transform_sparse(csc_container):
|
||||
X_sp = csc_container(X)
|
||||
Xt_sp = csc_container(Xt)
|
||||
sel = StepSelector()
|
||||
Xinv_actual = sel.fit(X_sp).inverse_transform(Xt_sp)
|
||||
assert_array_equal(Xinv, Xinv_actual.toarray())
|
||||
|
||||
# Check dtype matches
|
||||
assert np.int32 == sel.inverse_transform(Xt_sp.astype(np.int32)).dtype
|
||||
assert np.float32 == sel.inverse_transform(Xt_sp.astype(np.float32)).dtype
|
||||
|
||||
# Check wrong shape raises error
|
||||
with pytest.raises(ValueError):
|
||||
sel.inverse_transform(np.array([[1], [2]]))
|
||||
|
||||
|
||||
def test_get_support():
|
||||
sel = StepSelector()
|
||||
sel.fit(X, y)
|
||||
assert_array_equal(support, sel.get_support())
|
||||
assert_array_equal(support_inds, sel.get_support(indices=True))
|
||||
|
||||
|
||||
def test_output_dataframe():
|
||||
"""Check output dtypes for dataframes is consistent with the input dtypes."""
|
||||
pd = pytest.importorskip("pandas")
|
||||
|
||||
X = pd.DataFrame(
|
||||
{
|
||||
"a": pd.Series([1.0, 2.4, 4.5], dtype=np.float32),
|
||||
"b": pd.Series(["a", "b", "a"], dtype="category"),
|
||||
"c": pd.Series(["j", "b", "b"], dtype="category"),
|
||||
"d": pd.Series([3.0, 2.4, 1.2], dtype=np.float64),
|
||||
}
|
||||
)
|
||||
|
||||
for step in [2, 3]:
|
||||
sel = StepSelector(step=step).set_output(transform="pandas")
|
||||
sel.fit(X)
|
||||
|
||||
output = sel.transform(X)
|
||||
for name, dtype in output.dtypes.items():
|
||||
assert dtype == X.dtypes[name]
|
||||
|
||||
# step=0 will select nothing
|
||||
sel0 = StepSelector(step=0).set_output(transform="pandas")
|
||||
sel0.fit(X, y)
|
||||
|
||||
msg = "No features were selected"
|
||||
with pytest.warns(UserWarning, match=msg):
|
||||
output0 = sel0.transform(X)
|
||||
|
||||
assert_array_equal(output0.index, X.index)
|
||||
assert output0.shape == (X.shape[0], 0)
|
||||
@ -0,0 +1,93 @@
|
||||
"""
|
||||
Tests for chi2, currently the only feature selection function designed
|
||||
specifically to work with sparse matrices.
|
||||
"""
|
||||
|
||||
import warnings
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
import scipy.stats
|
||||
|
||||
from sklearn.feature_selection import SelectKBest, chi2
|
||||
from sklearn.feature_selection._univariate_selection import _chisquare
|
||||
from sklearn.utils._testing import assert_array_almost_equal, assert_array_equal
|
||||
from sklearn.utils.fixes import COO_CONTAINERS, CSR_CONTAINERS
|
||||
|
||||
# Feature 0 is highly informative for class 1;
|
||||
# feature 1 is the same everywhere;
|
||||
# feature 2 is a bit informative for class 2.
|
||||
X = [[2, 1, 2], [9, 1, 1], [6, 1, 2], [0, 1, 2]]
|
||||
y = [0, 1, 2, 2]
|
||||
|
||||
|
||||
def mkchi2(k):
|
||||
"""Make k-best chi2 selector"""
|
||||
return SelectKBest(chi2, k=k)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
|
||||
def test_chi2(csr_container):
|
||||
# Test Chi2 feature extraction
|
||||
|
||||
chi2 = mkchi2(k=1).fit(X, y)
|
||||
chi2 = mkchi2(k=1).fit(X, y)
|
||||
assert_array_equal(chi2.get_support(indices=True), [0])
|
||||
assert_array_equal(chi2.transform(X), np.array(X)[:, [0]])
|
||||
|
||||
chi2 = mkchi2(k=2).fit(X, y)
|
||||
assert_array_equal(sorted(chi2.get_support(indices=True)), [0, 2])
|
||||
|
||||
Xsp = csr_container(X, dtype=np.float64)
|
||||
chi2 = mkchi2(k=2).fit(Xsp, y)
|
||||
assert_array_equal(sorted(chi2.get_support(indices=True)), [0, 2])
|
||||
Xtrans = chi2.transform(Xsp)
|
||||
assert_array_equal(Xtrans.shape, [Xsp.shape[0], 2])
|
||||
|
||||
# == doesn't work on scipy.sparse matrices
|
||||
Xtrans = Xtrans.toarray()
|
||||
Xtrans2 = mkchi2(k=2).fit_transform(Xsp, y).toarray()
|
||||
assert_array_almost_equal(Xtrans, Xtrans2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("coo_container", COO_CONTAINERS)
|
||||
def test_chi2_coo(coo_container):
|
||||
# Check that chi2 works with a COO matrix
|
||||
# (as returned by CountVectorizer, DictVectorizer)
|
||||
Xcoo = coo_container(X)
|
||||
mkchi2(k=2).fit_transform(Xcoo, y)
|
||||
# if we got here without an exception, we're safe
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
|
||||
def test_chi2_negative(csr_container):
|
||||
# Check for proper error on negative numbers in the input X.
|
||||
X, y = [[0, 1], [-1e-20, 1]], [0, 1]
|
||||
for X in (X, np.array(X), csr_container(X)):
|
||||
with pytest.raises(ValueError):
|
||||
chi2(X, y)
|
||||
|
||||
|
||||
def test_chi2_unused_feature():
|
||||
# Unused feature should evaluate to NaN
|
||||
# and should issue no runtime warning
|
||||
with warnings.catch_warnings(record=True) as warned:
|
||||
warnings.simplefilter("always")
|
||||
chi, p = chi2([[1, 0], [0, 0]], [1, 0])
|
||||
for w in warned:
|
||||
if "divide by zero" in repr(w):
|
||||
raise AssertionError("Found unexpected warning %s" % w)
|
||||
assert_array_equal(chi, [1, np.nan])
|
||||
assert_array_equal(p[1], np.nan)
|
||||
|
||||
|
||||
def test_chisquare():
|
||||
# Test replacement for scipy.stats.chisquare against the original.
|
||||
obs = np.array([[2.0, 2.0], [1.0, 1.0]])
|
||||
exp = np.array([[1.5, 1.5], [1.5, 1.5]])
|
||||
# call SciPy first because our version overwrites obs
|
||||
chi_scp, p_scp = scipy.stats.chisquare(obs, exp)
|
||||
chi_our, p_our = _chisquare(obs, exp)
|
||||
|
||||
assert_array_almost_equal(chi_scp, chi_our)
|
||||
assert_array_almost_equal(p_scp, p_our)
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,684 @@
|
||||
import re
|
||||
import warnings
|
||||
from unittest.mock import Mock
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from sklearn import datasets
|
||||
from sklearn.base import BaseEstimator
|
||||
from sklearn.cross_decomposition import CCA, PLSCanonical, PLSRegression
|
||||
from sklearn.datasets import make_friedman1
|
||||
from sklearn.decomposition import PCA
|
||||
from sklearn.ensemble import HistGradientBoostingClassifier, RandomForestClassifier
|
||||
from sklearn.exceptions import NotFittedError
|
||||
from sklearn.feature_selection import SelectFromModel
|
||||
from sklearn.linear_model import (
|
||||
ElasticNet,
|
||||
ElasticNetCV,
|
||||
Lasso,
|
||||
LassoCV,
|
||||
LinearRegression,
|
||||
LogisticRegression,
|
||||
PassiveAggressiveClassifier,
|
||||
SGDClassifier,
|
||||
)
|
||||
from sklearn.pipeline import make_pipeline
|
||||
from sklearn.svm import LinearSVC
|
||||
from sklearn.utils._testing import (
|
||||
MinimalClassifier,
|
||||
assert_allclose,
|
||||
assert_array_almost_equal,
|
||||
assert_array_equal,
|
||||
skip_if_32bit,
|
||||
)
|
||||
|
||||
|
||||
class NaNTag(BaseEstimator):
|
||||
def _more_tags(self):
|
||||
return {"allow_nan": True}
|
||||
|
||||
|
||||
class NoNaNTag(BaseEstimator):
|
||||
def _more_tags(self):
|
||||
return {"allow_nan": False}
|
||||
|
||||
|
||||
class NaNTagRandomForest(RandomForestClassifier):
|
||||
def _more_tags(self):
|
||||
return {"allow_nan": True}
|
||||
|
||||
|
||||
iris = datasets.load_iris()
|
||||
data, y = iris.data, iris.target
|
||||
rng = np.random.RandomState(0)
|
||||
|
||||
|
||||
def test_invalid_input():
|
||||
clf = SGDClassifier(
|
||||
alpha=0.1, max_iter=10, shuffle=True, random_state=None, tol=None
|
||||
)
|
||||
for threshold in ["gobbledigook", ".5 * gobbledigook"]:
|
||||
model = SelectFromModel(clf, threshold=threshold)
|
||||
model.fit(data, y)
|
||||
with pytest.raises(ValueError):
|
||||
model.transform(data)
|
||||
|
||||
|
||||
def test_input_estimator_unchanged():
|
||||
# Test that SelectFromModel fits on a clone of the estimator.
|
||||
est = RandomForestClassifier()
|
||||
transformer = SelectFromModel(estimator=est)
|
||||
transformer.fit(data, y)
|
||||
assert transformer.estimator is est
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"max_features, err_type, err_msg",
|
||||
[
|
||||
(
|
||||
data.shape[1] + 1,
|
||||
ValueError,
|
||||
"max_features ==",
|
||||
),
|
||||
(
|
||||
lambda X: 1.5,
|
||||
TypeError,
|
||||
"max_features must be an instance of int, not float.",
|
||||
),
|
||||
(
|
||||
lambda X: data.shape[1] + 1,
|
||||
ValueError,
|
||||
"max_features ==",
|
||||
),
|
||||
(
|
||||
lambda X: -1,
|
||||
ValueError,
|
||||
"max_features ==",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_max_features_error(max_features, err_type, err_msg):
|
||||
err_msg = re.escape(err_msg)
|
||||
clf = RandomForestClassifier(n_estimators=5, random_state=0)
|
||||
|
||||
transformer = SelectFromModel(
|
||||
estimator=clf, max_features=max_features, threshold=-np.inf
|
||||
)
|
||||
with pytest.raises(err_type, match=err_msg):
|
||||
transformer.fit(data, y)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("max_features", [0, 2, data.shape[1], None])
|
||||
def test_inferred_max_features_integer(max_features):
|
||||
"""Check max_features_ and output shape for integer max_features."""
|
||||
clf = RandomForestClassifier(n_estimators=5, random_state=0)
|
||||
transformer = SelectFromModel(
|
||||
estimator=clf, max_features=max_features, threshold=-np.inf
|
||||
)
|
||||
X_trans = transformer.fit_transform(data, y)
|
||||
if max_features is not None:
|
||||
assert transformer.max_features_ == max_features
|
||||
assert X_trans.shape[1] == transformer.max_features_
|
||||
else:
|
||||
assert not hasattr(transformer, "max_features_")
|
||||
assert X_trans.shape[1] == data.shape[1]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"max_features",
|
||||
[lambda X: 1, lambda X: X.shape[1], lambda X: min(X.shape[1], 10000)],
|
||||
)
|
||||
def test_inferred_max_features_callable(max_features):
|
||||
"""Check max_features_ and output shape for callable max_features."""
|
||||
clf = RandomForestClassifier(n_estimators=5, random_state=0)
|
||||
transformer = SelectFromModel(
|
||||
estimator=clf, max_features=max_features, threshold=-np.inf
|
||||
)
|
||||
X_trans = transformer.fit_transform(data, y)
|
||||
assert transformer.max_features_ == max_features(data)
|
||||
assert X_trans.shape[1] == transformer.max_features_
|
||||
|
||||
|
||||
@pytest.mark.parametrize("max_features", [lambda X: round(len(X[0]) / 2), 2])
|
||||
def test_max_features_array_like(max_features):
|
||||
X = [
|
||||
[0.87, -1.34, 0.31],
|
||||
[-2.79, -0.02, -0.85],
|
||||
[-1.34, -0.48, -2.55],
|
||||
[1.92, 1.48, 0.65],
|
||||
]
|
||||
y = [0, 1, 0, 1]
|
||||
|
||||
clf = RandomForestClassifier(n_estimators=5, random_state=0)
|
||||
transformer = SelectFromModel(
|
||||
estimator=clf, max_features=max_features, threshold=-np.inf
|
||||
)
|
||||
X_trans = transformer.fit_transform(X, y)
|
||||
assert X_trans.shape[1] == transformer.max_features_
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"max_features",
|
||||
[lambda X: min(X.shape[1], 10000), lambda X: X.shape[1], lambda X: 1],
|
||||
)
|
||||
def test_max_features_callable_data(max_features):
|
||||
"""Tests that the callable passed to `fit` is called on X."""
|
||||
clf = RandomForestClassifier(n_estimators=50, random_state=0)
|
||||
m = Mock(side_effect=max_features)
|
||||
transformer = SelectFromModel(estimator=clf, max_features=m, threshold=-np.inf)
|
||||
transformer.fit_transform(data, y)
|
||||
m.assert_called_with(data)
|
||||
|
||||
|
||||
class FixedImportanceEstimator(BaseEstimator):
|
||||
def __init__(self, importances):
|
||||
self.importances = importances
|
||||
|
||||
def fit(self, X, y=None):
|
||||
self.feature_importances_ = np.array(self.importances)
|
||||
|
||||
|
||||
def test_max_features():
|
||||
# Test max_features parameter using various values
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=1000,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
)
|
||||
max_features = X.shape[1]
|
||||
est = RandomForestClassifier(n_estimators=50, random_state=0)
|
||||
|
||||
transformer1 = SelectFromModel(estimator=est, threshold=-np.inf)
|
||||
transformer2 = SelectFromModel(
|
||||
estimator=est, max_features=max_features, threshold=-np.inf
|
||||
)
|
||||
X_new1 = transformer1.fit_transform(X, y)
|
||||
X_new2 = transformer2.fit_transform(X, y)
|
||||
assert_allclose(X_new1, X_new2)
|
||||
|
||||
# Test max_features against actual model.
|
||||
transformer1 = SelectFromModel(estimator=Lasso(alpha=0.025, random_state=42))
|
||||
X_new1 = transformer1.fit_transform(X, y)
|
||||
scores1 = np.abs(transformer1.estimator_.coef_)
|
||||
candidate_indices1 = np.argsort(-scores1, kind="mergesort")
|
||||
|
||||
for n_features in range(1, X_new1.shape[1] + 1):
|
||||
transformer2 = SelectFromModel(
|
||||
estimator=Lasso(alpha=0.025, random_state=42),
|
||||
max_features=n_features,
|
||||
threshold=-np.inf,
|
||||
)
|
||||
X_new2 = transformer2.fit_transform(X, y)
|
||||
scores2 = np.abs(transformer2.estimator_.coef_)
|
||||
candidate_indices2 = np.argsort(-scores2, kind="mergesort")
|
||||
assert_allclose(
|
||||
X[:, candidate_indices1[:n_features]], X[:, candidate_indices2[:n_features]]
|
||||
)
|
||||
assert_allclose(transformer1.estimator_.coef_, transformer2.estimator_.coef_)
|
||||
|
||||
|
||||
def test_max_features_tiebreak():
|
||||
# Test if max_features can break tie among feature importance
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=1000,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
)
|
||||
max_features = X.shape[1]
|
||||
|
||||
feature_importances = np.array([4, 4, 4, 4, 3, 3, 3, 2, 2, 1])
|
||||
for n_features in range(1, max_features + 1):
|
||||
transformer = SelectFromModel(
|
||||
FixedImportanceEstimator(feature_importances),
|
||||
max_features=n_features,
|
||||
threshold=-np.inf,
|
||||
)
|
||||
X_new = transformer.fit_transform(X, y)
|
||||
selected_feature_indices = np.where(transformer._get_support_mask())[0]
|
||||
assert_array_equal(selected_feature_indices, np.arange(n_features))
|
||||
assert X_new.shape[1] == n_features
|
||||
|
||||
|
||||
def test_threshold_and_max_features():
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=1000,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
)
|
||||
est = RandomForestClassifier(n_estimators=50, random_state=0)
|
||||
|
||||
transformer1 = SelectFromModel(estimator=est, max_features=3, threshold=-np.inf)
|
||||
X_new1 = transformer1.fit_transform(X, y)
|
||||
|
||||
transformer2 = SelectFromModel(estimator=est, threshold=0.04)
|
||||
X_new2 = transformer2.fit_transform(X, y)
|
||||
|
||||
transformer3 = SelectFromModel(estimator=est, max_features=3, threshold=0.04)
|
||||
X_new3 = transformer3.fit_transform(X, y)
|
||||
assert X_new3.shape[1] == min(X_new1.shape[1], X_new2.shape[1])
|
||||
selected_indices = transformer3.transform(np.arange(X.shape[1])[np.newaxis, :])
|
||||
assert_allclose(X_new3, X[:, selected_indices[0]])
|
||||
|
||||
|
||||
@skip_if_32bit
|
||||
def test_feature_importances():
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=1000,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
)
|
||||
|
||||
est = RandomForestClassifier(n_estimators=50, random_state=0)
|
||||
for threshold, func in zip(["mean", "median"], [np.mean, np.median]):
|
||||
transformer = SelectFromModel(estimator=est, threshold=threshold)
|
||||
transformer.fit(X, y)
|
||||
assert hasattr(transformer.estimator_, "feature_importances_")
|
||||
|
||||
X_new = transformer.transform(X)
|
||||
assert X_new.shape[1] < X.shape[1]
|
||||
importances = transformer.estimator_.feature_importances_
|
||||
|
||||
feature_mask = np.abs(importances) > func(importances)
|
||||
assert_array_almost_equal(X_new, X[:, feature_mask])
|
||||
|
||||
|
||||
def test_sample_weight():
|
||||
# Ensure sample weights are passed to underlying estimator
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=100,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
)
|
||||
|
||||
# Check with sample weights
|
||||
sample_weight = np.ones(y.shape)
|
||||
sample_weight[y == 1] *= 100
|
||||
|
||||
est = LogisticRegression(random_state=0, fit_intercept=False)
|
||||
transformer = SelectFromModel(estimator=est)
|
||||
transformer.fit(X, y, sample_weight=None)
|
||||
mask = transformer._get_support_mask()
|
||||
transformer.fit(X, y, sample_weight=sample_weight)
|
||||
weighted_mask = transformer._get_support_mask()
|
||||
assert not np.all(weighted_mask == mask)
|
||||
transformer.fit(X, y, sample_weight=3 * sample_weight)
|
||||
reweighted_mask = transformer._get_support_mask()
|
||||
assert np.all(weighted_mask == reweighted_mask)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"estimator",
|
||||
[
|
||||
Lasso(alpha=0.1, random_state=42),
|
||||
LassoCV(random_state=42),
|
||||
ElasticNet(l1_ratio=1, random_state=42),
|
||||
ElasticNetCV(l1_ratio=[1], random_state=42),
|
||||
],
|
||||
)
|
||||
def test_coef_default_threshold(estimator):
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=100,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
)
|
||||
|
||||
# For the Lasso and related models, the threshold defaults to 1e-5
|
||||
transformer = SelectFromModel(estimator=estimator)
|
||||
transformer.fit(X, y)
|
||||
X_new = transformer.transform(X)
|
||||
mask = np.abs(transformer.estimator_.coef_) > 1e-5
|
||||
assert_array_almost_equal(X_new, X[:, mask])
|
||||
|
||||
|
||||
@skip_if_32bit
|
||||
def test_2d_coef():
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=1000,
|
||||
n_features=10,
|
||||
n_informative=3,
|
||||
n_redundant=0,
|
||||
n_repeated=0,
|
||||
shuffle=False,
|
||||
random_state=0,
|
||||
n_classes=4,
|
||||
)
|
||||
|
||||
est = LogisticRegression()
|
||||
for threshold, func in zip(["mean", "median"], [np.mean, np.median]):
|
||||
for order in [1, 2, np.inf]:
|
||||
# Fit SelectFromModel a multi-class problem
|
||||
transformer = SelectFromModel(
|
||||
estimator=LogisticRegression(), threshold=threshold, norm_order=order
|
||||
)
|
||||
transformer.fit(X, y)
|
||||
assert hasattr(transformer.estimator_, "coef_")
|
||||
X_new = transformer.transform(X)
|
||||
assert X_new.shape[1] < X.shape[1]
|
||||
|
||||
# Manually check that the norm is correctly performed
|
||||
est.fit(X, y)
|
||||
importances = np.linalg.norm(est.coef_, axis=0, ord=order)
|
||||
feature_mask = importances > func(importances)
|
||||
assert_array_almost_equal(X_new, X[:, feature_mask])
|
||||
|
||||
|
||||
def test_partial_fit():
|
||||
est = PassiveAggressiveClassifier(
|
||||
random_state=0, shuffle=False, max_iter=5, tol=None
|
||||
)
|
||||
transformer = SelectFromModel(estimator=est)
|
||||
transformer.partial_fit(data, y, classes=np.unique(y))
|
||||
old_model = transformer.estimator_
|
||||
transformer.partial_fit(data, y, classes=np.unique(y))
|
||||
new_model = transformer.estimator_
|
||||
assert old_model is new_model
|
||||
|
||||
X_transform = transformer.transform(data)
|
||||
transformer.fit(np.vstack((data, data)), np.concatenate((y, y)))
|
||||
assert_array_almost_equal(X_transform, transformer.transform(data))
|
||||
|
||||
# check that if est doesn't have partial_fit, neither does SelectFromModel
|
||||
transformer = SelectFromModel(estimator=RandomForestClassifier())
|
||||
assert not hasattr(transformer, "partial_fit")
|
||||
|
||||
|
||||
def test_calling_fit_reinitializes():
|
||||
est = LinearSVC(random_state=0)
|
||||
transformer = SelectFromModel(estimator=est)
|
||||
transformer.fit(data, y)
|
||||
transformer.set_params(estimator__C=100)
|
||||
transformer.fit(data, y)
|
||||
assert transformer.estimator_.C == 100
|
||||
|
||||
|
||||
def test_prefit():
|
||||
# Test all possible combinations of the prefit parameter.
|
||||
|
||||
# Passing a prefit parameter with the selected model
|
||||
# and fitting a unfit model with prefit=False should give same results.
|
||||
clf = SGDClassifier(alpha=0.1, max_iter=10, shuffle=True, random_state=0, tol=None)
|
||||
model = SelectFromModel(clf)
|
||||
model.fit(data, y)
|
||||
X_transform = model.transform(data)
|
||||
clf.fit(data, y)
|
||||
model = SelectFromModel(clf, prefit=True)
|
||||
assert_array_almost_equal(model.transform(data), X_transform)
|
||||
model.fit(data, y)
|
||||
assert model.estimator_ is not clf
|
||||
|
||||
# Check that the model is rewritten if prefit=False and a fitted model is
|
||||
# passed
|
||||
model = SelectFromModel(clf, prefit=False)
|
||||
model.fit(data, y)
|
||||
assert_array_almost_equal(model.transform(data), X_transform)
|
||||
|
||||
# Check that passing an unfitted estimator with `prefit=True` raises a
|
||||
# `ValueError`
|
||||
clf = SGDClassifier(alpha=0.1, max_iter=10, shuffle=True, random_state=0, tol=None)
|
||||
model = SelectFromModel(clf, prefit=True)
|
||||
err_msg = "When `prefit=True`, `estimator` is expected to be a fitted estimator."
|
||||
with pytest.raises(NotFittedError, match=err_msg):
|
||||
model.fit(data, y)
|
||||
with pytest.raises(NotFittedError, match=err_msg):
|
||||
model.partial_fit(data, y)
|
||||
with pytest.raises(NotFittedError, match=err_msg):
|
||||
model.transform(data)
|
||||
|
||||
# Check that the internal parameters of prefitted model are not changed
|
||||
# when calling `fit` or `partial_fit` with `prefit=True`
|
||||
clf = SGDClassifier(alpha=0.1, max_iter=10, shuffle=True, tol=None).fit(data, y)
|
||||
model = SelectFromModel(clf, prefit=True)
|
||||
model.fit(data, y)
|
||||
assert_allclose(model.estimator_.coef_, clf.coef_)
|
||||
model.partial_fit(data, y)
|
||||
assert_allclose(model.estimator_.coef_, clf.coef_)
|
||||
|
||||
|
||||
def test_prefit_max_features():
|
||||
"""Check the interaction between `prefit` and `max_features`."""
|
||||
# case 1: an error should be raised at `transform` if `fit` was not called to
|
||||
# validate the attributes
|
||||
estimator = RandomForestClassifier(n_estimators=5, random_state=0)
|
||||
estimator.fit(data, y)
|
||||
model = SelectFromModel(estimator, prefit=True, max_features=lambda X: X.shape[1])
|
||||
|
||||
err_msg = (
|
||||
"When `prefit=True` and `max_features` is a callable, call `fit` "
|
||||
"before calling `transform`."
|
||||
)
|
||||
with pytest.raises(NotFittedError, match=err_msg):
|
||||
model.transform(data)
|
||||
|
||||
# case 2: `max_features` is not validated and different from an integer
|
||||
# FIXME: we cannot validate the upper bound of the attribute at transform
|
||||
# and we should force calling `fit` if we intend to force the attribute
|
||||
# to have such an upper bound.
|
||||
max_features = 2.5
|
||||
model.set_params(max_features=max_features)
|
||||
with pytest.raises(ValueError, match="`max_features` must be an integer"):
|
||||
model.transform(data)
|
||||
|
||||
|
||||
def test_prefit_get_feature_names_out():
|
||||
"""Check the interaction between prefit and the feature names."""
|
||||
clf = RandomForestClassifier(n_estimators=2, random_state=0)
|
||||
clf.fit(data, y)
|
||||
model = SelectFromModel(clf, prefit=True, max_features=1)
|
||||
|
||||
name = type(model).__name__
|
||||
err_msg = (
|
||||
f"This {name} instance is not fitted yet. Call 'fit' with "
|
||||
"appropriate arguments before using this estimator."
|
||||
)
|
||||
with pytest.raises(NotFittedError, match=err_msg):
|
||||
model.get_feature_names_out()
|
||||
|
||||
model.fit(data, y)
|
||||
feature_names = model.get_feature_names_out()
|
||||
assert feature_names == ["x3"]
|
||||
|
||||
|
||||
def test_threshold_string():
|
||||
est = RandomForestClassifier(n_estimators=50, random_state=0)
|
||||
model = SelectFromModel(est, threshold="0.5*mean")
|
||||
model.fit(data, y)
|
||||
X_transform = model.transform(data)
|
||||
|
||||
# Calculate the threshold from the estimator directly.
|
||||
est.fit(data, y)
|
||||
threshold = 0.5 * np.mean(est.feature_importances_)
|
||||
mask = est.feature_importances_ > threshold
|
||||
assert_array_almost_equal(X_transform, data[:, mask])
|
||||
|
||||
|
||||
def test_threshold_without_refitting():
|
||||
# Test that the threshold can be set without refitting the model.
|
||||
clf = SGDClassifier(alpha=0.1, max_iter=10, shuffle=True, random_state=0, tol=None)
|
||||
model = SelectFromModel(clf, threshold="0.1 * mean")
|
||||
model.fit(data, y)
|
||||
X_transform = model.transform(data)
|
||||
|
||||
# Set a higher threshold to filter out more features.
|
||||
model.threshold = "1.0 * mean"
|
||||
assert X_transform.shape[1] > model.transform(data).shape[1]
|
||||
|
||||
|
||||
def test_fit_accepts_nan_inf():
|
||||
# Test that fit doesn't check for np.inf and np.nan values.
|
||||
clf = HistGradientBoostingClassifier(random_state=0)
|
||||
|
||||
model = SelectFromModel(estimator=clf)
|
||||
|
||||
nan_data = data.copy()
|
||||
nan_data[0] = np.nan
|
||||
nan_data[1] = np.inf
|
||||
|
||||
model.fit(data, y)
|
||||
|
||||
|
||||
def test_transform_accepts_nan_inf():
|
||||
# Test that transform doesn't check for np.inf and np.nan values.
|
||||
clf = NaNTagRandomForest(n_estimators=100, random_state=0)
|
||||
nan_data = data.copy()
|
||||
|
||||
model = SelectFromModel(estimator=clf)
|
||||
model.fit(nan_data, y)
|
||||
|
||||
nan_data[0] = np.nan
|
||||
nan_data[1] = np.inf
|
||||
|
||||
model.transform(nan_data)
|
||||
|
||||
|
||||
def test_allow_nan_tag_comes_from_estimator():
|
||||
allow_nan_est = NaNTag()
|
||||
model = SelectFromModel(estimator=allow_nan_est)
|
||||
assert model._get_tags()["allow_nan"] is True
|
||||
|
||||
no_nan_est = NoNaNTag()
|
||||
model = SelectFromModel(estimator=no_nan_est)
|
||||
assert model._get_tags()["allow_nan"] is False
|
||||
|
||||
|
||||
def _pca_importances(pca_estimator):
|
||||
return np.abs(pca_estimator.explained_variance_)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"estimator, importance_getter",
|
||||
[
|
||||
(
|
||||
make_pipeline(PCA(random_state=0), LogisticRegression()),
|
||||
"named_steps.logisticregression.coef_",
|
||||
),
|
||||
(PCA(random_state=0), _pca_importances),
|
||||
],
|
||||
)
|
||||
def test_importance_getter(estimator, importance_getter):
|
||||
selector = SelectFromModel(
|
||||
estimator, threshold="mean", importance_getter=importance_getter
|
||||
)
|
||||
selector.fit(data, y)
|
||||
assert selector.transform(data).shape[1] == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("PLSEstimator", [CCA, PLSCanonical, PLSRegression])
|
||||
def test_select_from_model_pls(PLSEstimator):
|
||||
"""Check the behaviour of SelectFromModel with PLS estimators.
|
||||
|
||||
Non-regression test for:
|
||||
https://github.com/scikit-learn/scikit-learn/issues/12410
|
||||
"""
|
||||
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
|
||||
estimator = PLSEstimator(n_components=1)
|
||||
model = make_pipeline(SelectFromModel(estimator), estimator).fit(X, y)
|
||||
assert model.score(X, y) > 0.5
|
||||
|
||||
|
||||
def test_estimator_does_not_support_feature_names():
|
||||
"""SelectFromModel works with estimators that do not support feature_names_in_.
|
||||
|
||||
Non-regression test for #21949.
|
||||
"""
|
||||
pytest.importorskip("pandas")
|
||||
X, y = datasets.load_iris(as_frame=True, return_X_y=True)
|
||||
all_feature_names = set(X.columns)
|
||||
|
||||
def importance_getter(estimator):
|
||||
return np.arange(X.shape[1])
|
||||
|
||||
selector = SelectFromModel(
|
||||
MinimalClassifier(), importance_getter=importance_getter
|
||||
).fit(X, y)
|
||||
|
||||
# selector learns the feature names itself
|
||||
assert_array_equal(selector.feature_names_in_, X.columns)
|
||||
|
||||
feature_names_out = set(selector.get_feature_names_out())
|
||||
assert feature_names_out < all_feature_names
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("error", UserWarning)
|
||||
|
||||
selector.transform(X.iloc[1:3])
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error, err_msg, max_features",
|
||||
(
|
||||
[ValueError, "max_features == 10, must be <= 4", 10],
|
||||
[ValueError, "max_features == 5, must be <= 4", lambda x: x.shape[1] + 1],
|
||||
),
|
||||
)
|
||||
def test_partial_fit_validate_max_features(error, err_msg, max_features):
|
||||
"""Test that partial_fit from SelectFromModel validates `max_features`."""
|
||||
X, y = datasets.make_classification(
|
||||
n_samples=100,
|
||||
n_features=4,
|
||||
random_state=0,
|
||||
)
|
||||
|
||||
with pytest.raises(error, match=err_msg):
|
||||
SelectFromModel(
|
||||
estimator=SGDClassifier(), max_features=max_features
|
||||
).partial_fit(X, y, classes=[0, 1])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("as_frame", [True, False])
|
||||
def test_partial_fit_validate_feature_names(as_frame):
|
||||
"""Test that partial_fit from SelectFromModel validates `feature_names_in_`."""
|
||||
pytest.importorskip("pandas")
|
||||
X, y = datasets.load_iris(as_frame=as_frame, return_X_y=True)
|
||||
|
||||
selector = SelectFromModel(estimator=SGDClassifier(), max_features=4).partial_fit(
|
||||
X, y, classes=[0, 1, 2]
|
||||
)
|
||||
if as_frame:
|
||||
assert_array_equal(selector.feature_names_in_, X.columns)
|
||||
else:
|
||||
assert not hasattr(selector, "feature_names_in_")
|
||||
|
||||
|
||||
def test_from_model_estimator_attribute_error():
|
||||
"""Check that we raise the proper AttributeError when the estimator
|
||||
does not implement the `partial_fit` method, which is decorated with
|
||||
`available_if`.
|
||||
|
||||
Non-regression test for:
|
||||
https://github.com/scikit-learn/scikit-learn/issues/28108
|
||||
"""
|
||||
# `LinearRegression` does not implement 'partial_fit' and should raise an
|
||||
# AttributeError
|
||||
from_model = SelectFromModel(estimator=LinearRegression())
|
||||
|
||||
outer_msg = "This 'SelectFromModel' has no attribute 'partial_fit'"
|
||||
inner_msg = "'LinearRegression' object has no attribute 'partial_fit'"
|
||||
with pytest.raises(AttributeError, match=outer_msg) as exec_info:
|
||||
from_model.fit(data, y).partial_fit(data)
|
||||
assert isinstance(exec_info.value.__cause__, AttributeError)
|
||||
assert inner_msg in str(exec_info.value.__cause__)
|
||||
@ -0,0 +1,270 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from sklearn.datasets import make_classification, make_regression
|
||||
from sklearn.feature_selection import mutual_info_classif, mutual_info_regression
|
||||
from sklearn.feature_selection._mutual_info import _compute_mi
|
||||
from sklearn.utils import check_random_state
|
||||
from sklearn.utils._testing import (
|
||||
assert_allclose,
|
||||
assert_array_equal,
|
||||
)
|
||||
from sklearn.utils.fixes import CSR_CONTAINERS
|
||||
|
||||
|
||||
def test_compute_mi_dd():
|
||||
# In discrete case computations are straightforward and can be done
|
||||
# by hand on given vectors.
|
||||
x = np.array([0, 1, 1, 0, 0])
|
||||
y = np.array([1, 0, 0, 0, 1])
|
||||
|
||||
H_x = H_y = -(3 / 5) * np.log(3 / 5) - (2 / 5) * np.log(2 / 5)
|
||||
H_xy = -1 / 5 * np.log(1 / 5) - 2 / 5 * np.log(2 / 5) - 2 / 5 * np.log(2 / 5)
|
||||
I_xy = H_x + H_y - H_xy
|
||||
|
||||
assert_allclose(_compute_mi(x, y, x_discrete=True, y_discrete=True), I_xy)
|
||||
|
||||
|
||||
def test_compute_mi_cc(global_dtype):
|
||||
# For two continuous variables a good approach is to test on bivariate
|
||||
# normal distribution, where mutual information is known.
|
||||
|
||||
# Mean of the distribution, irrelevant for mutual information.
|
||||
mean = np.zeros(2)
|
||||
|
||||
# Setup covariance matrix with correlation coeff. equal 0.5.
|
||||
sigma_1 = 1
|
||||
sigma_2 = 10
|
||||
corr = 0.5
|
||||
cov = np.array(
|
||||
[
|
||||
[sigma_1**2, corr * sigma_1 * sigma_2],
|
||||
[corr * sigma_1 * sigma_2, sigma_2**2],
|
||||
]
|
||||
)
|
||||
|
||||
# True theoretical mutual information.
|
||||
I_theory = np.log(sigma_1) + np.log(sigma_2) - 0.5 * np.log(np.linalg.det(cov))
|
||||
|
||||
rng = check_random_state(0)
|
||||
Z = rng.multivariate_normal(mean, cov, size=1000).astype(global_dtype, copy=False)
|
||||
|
||||
x, y = Z[:, 0], Z[:, 1]
|
||||
|
||||
# Theory and computed values won't be very close
|
||||
# We here check with a large relative tolerance
|
||||
for n_neighbors in [3, 5, 7]:
|
||||
I_computed = _compute_mi(
|
||||
x, y, x_discrete=False, y_discrete=False, n_neighbors=n_neighbors
|
||||
)
|
||||
assert_allclose(I_computed, I_theory, rtol=1e-1)
|
||||
|
||||
|
||||
def test_compute_mi_cd(global_dtype):
|
||||
# To test define a joint distribution as follows:
|
||||
# p(x, y) = p(x) p(y | x)
|
||||
# X ~ Bernoulli(p)
|
||||
# (Y | x = 0) ~ Uniform(-1, 1)
|
||||
# (Y | x = 1) ~ Uniform(0, 2)
|
||||
|
||||
# Use the following formula for mutual information:
|
||||
# I(X; Y) = H(Y) - H(Y | X)
|
||||
# Two entropies can be computed by hand:
|
||||
# H(Y) = -(1-p)/2 * ln((1-p)/2) - p/2*log(p/2) - 1/2*log(1/2)
|
||||
# H(Y | X) = ln(2)
|
||||
|
||||
# Now we need to implement sampling from out distribution, which is
|
||||
# done easily using conditional distribution logic.
|
||||
|
||||
n_samples = 1000
|
||||
rng = check_random_state(0)
|
||||
|
||||
for p in [0.3, 0.5, 0.7]:
|
||||
x = rng.uniform(size=n_samples) > p
|
||||
|
||||
y = np.empty(n_samples, global_dtype)
|
||||
mask = x == 0
|
||||
y[mask] = rng.uniform(-1, 1, size=np.sum(mask))
|
||||
y[~mask] = rng.uniform(0, 2, size=np.sum(~mask))
|
||||
|
||||
I_theory = -0.5 * (
|
||||
(1 - p) * np.log(0.5 * (1 - p)) + p * np.log(0.5 * p) + np.log(0.5)
|
||||
) - np.log(2)
|
||||
|
||||
# Assert the same tolerance.
|
||||
for n_neighbors in [3, 5, 7]:
|
||||
I_computed = _compute_mi(
|
||||
x, y, x_discrete=True, y_discrete=False, n_neighbors=n_neighbors
|
||||
)
|
||||
assert_allclose(I_computed, I_theory, rtol=1e-1)
|
||||
|
||||
|
||||
def test_compute_mi_cd_unique_label(global_dtype):
|
||||
# Test that adding unique label doesn't change MI.
|
||||
n_samples = 100
|
||||
x = np.random.uniform(size=n_samples) > 0.5
|
||||
|
||||
y = np.empty(n_samples, global_dtype)
|
||||
mask = x == 0
|
||||
y[mask] = np.random.uniform(-1, 1, size=np.sum(mask))
|
||||
y[~mask] = np.random.uniform(0, 2, size=np.sum(~mask))
|
||||
|
||||
mi_1 = _compute_mi(x, y, x_discrete=True, y_discrete=False)
|
||||
|
||||
x = np.hstack((x, 2))
|
||||
y = np.hstack((y, 10))
|
||||
mi_2 = _compute_mi(x, y, x_discrete=True, y_discrete=False)
|
||||
|
||||
assert_allclose(mi_1, mi_2)
|
||||
|
||||
|
||||
# We are going test that feature ordering by MI matches our expectations.
|
||||
def test_mutual_info_classif_discrete(global_dtype):
|
||||
X = np.array(
|
||||
[[0, 0, 0], [1, 1, 0], [2, 0, 1], [2, 0, 1], [2, 0, 1]], dtype=global_dtype
|
||||
)
|
||||
y = np.array([0, 1, 2, 2, 1])
|
||||
|
||||
# Here X[:, 0] is the most informative feature, and X[:, 1] is weakly
|
||||
# informative.
|
||||
mi = mutual_info_classif(X, y, discrete_features=True)
|
||||
assert_array_equal(np.argsort(-mi), np.array([0, 2, 1]))
|
||||
|
||||
|
||||
def test_mutual_info_regression(global_dtype):
|
||||
# We generate sample from multivariate normal distribution, using
|
||||
# transformation from initially uncorrelated variables. The zero
|
||||
# variables after transformation is selected as the target vector,
|
||||
# it has the strongest correlation with the variable 2, and
|
||||
# the weakest correlation with the variable 1.
|
||||
T = np.array([[1, 0.5, 2, 1], [0, 1, 0.1, 0.0], [0, 0.1, 1, 0.1], [0, 0.1, 0.1, 1]])
|
||||
cov = T.dot(T.T)
|
||||
mean = np.zeros(4)
|
||||
|
||||
rng = check_random_state(0)
|
||||
Z = rng.multivariate_normal(mean, cov, size=1000).astype(global_dtype, copy=False)
|
||||
X = Z[:, 1:]
|
||||
y = Z[:, 0]
|
||||
|
||||
mi = mutual_info_regression(X, y, random_state=0)
|
||||
assert_array_equal(np.argsort(-mi), np.array([1, 2, 0]))
|
||||
# XXX: should mutual_info_regression be fixed to avoid
|
||||
# up-casting float32 inputs to float64?
|
||||
assert mi.dtype == np.float64
|
||||
|
||||
|
||||
def test_mutual_info_classif_mixed(global_dtype):
|
||||
# Here the target is discrete and there are two continuous and one
|
||||
# discrete feature. The idea of this test is clear from the code.
|
||||
rng = check_random_state(0)
|
||||
X = rng.rand(1000, 3).astype(global_dtype, copy=False)
|
||||
X[:, 1] += X[:, 0]
|
||||
y = ((0.5 * X[:, 0] + X[:, 2]) > 0.5).astype(int)
|
||||
X[:, 2] = X[:, 2] > 0.5
|
||||
|
||||
mi = mutual_info_classif(X, y, discrete_features=[2], n_neighbors=3, random_state=0)
|
||||
assert_array_equal(np.argsort(-mi), [2, 0, 1])
|
||||
for n_neighbors in [5, 7, 9]:
|
||||
mi_nn = mutual_info_classif(
|
||||
X, y, discrete_features=[2], n_neighbors=n_neighbors, random_state=0
|
||||
)
|
||||
# Check that the continuous values have an higher MI with greater
|
||||
# n_neighbors
|
||||
assert mi_nn[0] > mi[0]
|
||||
assert mi_nn[1] > mi[1]
|
||||
# The n_neighbors should not have any effect on the discrete value
|
||||
# The MI should be the same
|
||||
assert mi_nn[2] == mi[2]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
|
||||
def test_mutual_info_options(global_dtype, csr_container):
|
||||
X = np.array(
|
||||
[[0, 0, 0], [1, 1, 0], [2, 0, 1], [2, 0, 1], [2, 0, 1]], dtype=global_dtype
|
||||
)
|
||||
y = np.array([0, 1, 2, 2, 1], dtype=global_dtype)
|
||||
X_csr = csr_container(X)
|
||||
|
||||
for mutual_info in (mutual_info_regression, mutual_info_classif):
|
||||
with pytest.raises(ValueError):
|
||||
mutual_info(X_csr, y, discrete_features=False)
|
||||
with pytest.raises(ValueError):
|
||||
mutual_info(X, y, discrete_features="manual")
|
||||
with pytest.raises(ValueError):
|
||||
mutual_info(X_csr, y, discrete_features=[True, False, True])
|
||||
with pytest.raises(IndexError):
|
||||
mutual_info(X, y, discrete_features=[True, False, True, False])
|
||||
with pytest.raises(IndexError):
|
||||
mutual_info(X, y, discrete_features=[1, 4])
|
||||
|
||||
mi_1 = mutual_info(X, y, discrete_features="auto", random_state=0)
|
||||
mi_2 = mutual_info(X, y, discrete_features=False, random_state=0)
|
||||
mi_3 = mutual_info(X_csr, y, discrete_features="auto", random_state=0)
|
||||
mi_4 = mutual_info(X_csr, y, discrete_features=True, random_state=0)
|
||||
mi_5 = mutual_info(X, y, discrete_features=[True, False, True], random_state=0)
|
||||
mi_6 = mutual_info(X, y, discrete_features=[0, 2], random_state=0)
|
||||
|
||||
assert_allclose(mi_1, mi_2)
|
||||
assert_allclose(mi_3, mi_4)
|
||||
assert_allclose(mi_5, mi_6)
|
||||
|
||||
assert not np.allclose(mi_1, mi_3)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("correlated", [True, False])
|
||||
def test_mutual_information_symmetry_classif_regression(correlated, global_random_seed):
|
||||
"""Check that `mutual_info_classif` and `mutual_info_regression` are
|
||||
symmetric by switching the target `y` as `feature` in `X` and vice
|
||||
versa.
|
||||
|
||||
Non-regression test for:
|
||||
https://github.com/scikit-learn/scikit-learn/issues/23720
|
||||
"""
|
||||
rng = np.random.RandomState(global_random_seed)
|
||||
n = 100
|
||||
d = rng.randint(10, size=n)
|
||||
|
||||
if correlated:
|
||||
c = d.astype(np.float64)
|
||||
else:
|
||||
c = rng.normal(0, 1, size=n)
|
||||
|
||||
mi_classif = mutual_info_classif(
|
||||
c[:, None], d, discrete_features=[False], random_state=global_random_seed
|
||||
)
|
||||
|
||||
mi_regression = mutual_info_regression(
|
||||
d[:, None], c, discrete_features=[True], random_state=global_random_seed
|
||||
)
|
||||
|
||||
assert mi_classif == pytest.approx(mi_regression)
|
||||
|
||||
|
||||
def test_mutual_info_regression_X_int_dtype(global_random_seed):
|
||||
"""Check that results agree when X is integer dtype and float dtype.
|
||||
|
||||
Non-regression test for Issue #26696.
|
||||
"""
|
||||
rng = np.random.RandomState(global_random_seed)
|
||||
X = rng.randint(100, size=(100, 10))
|
||||
X_float = X.astype(np.float64, copy=True)
|
||||
y = rng.randint(100, size=100)
|
||||
|
||||
expected = mutual_info_regression(X_float, y, random_state=global_random_seed)
|
||||
result = mutual_info_regression(X, y, random_state=global_random_seed)
|
||||
assert_allclose(result, expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mutual_info_func, data_generator",
|
||||
[
|
||||
(mutual_info_regression, make_regression),
|
||||
(mutual_info_classif, make_classification),
|
||||
],
|
||||
)
|
||||
def test_mutual_info_n_jobs(global_random_seed, mutual_info_func, data_generator):
|
||||
"""Check that results are consistent with different `n_jobs`."""
|
||||
X, y = data_generator(random_state=global_random_seed)
|
||||
single_job = mutual_info_func(X, y, random_state=global_random_seed, n_jobs=1)
|
||||
multi_job = mutual_info_func(X, y, random_state=global_random_seed, n_jobs=2)
|
||||
assert_allclose(single_job, multi_job)
|
||||
@ -0,0 +1,668 @@
|
||||
"""
|
||||
Testing Recursive feature elimination
|
||||
"""
|
||||
|
||||
from operator import attrgetter
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal
|
||||
|
||||
from sklearn.base import BaseEstimator, ClassifierMixin
|
||||
from sklearn.compose import TransformedTargetRegressor
|
||||
from sklearn.cross_decomposition import CCA, PLSCanonical, PLSRegression
|
||||
from sklearn.datasets import load_iris, make_classification, make_friedman1
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.feature_selection import RFE, RFECV
|
||||
from sklearn.impute import SimpleImputer
|
||||
from sklearn.linear_model import LinearRegression, LogisticRegression
|
||||
from sklearn.metrics import get_scorer, make_scorer, zero_one_loss
|
||||
from sklearn.model_selection import GroupKFold, cross_val_score
|
||||
from sklearn.pipeline import make_pipeline
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.svm import SVC, SVR, LinearSVR
|
||||
from sklearn.utils import check_random_state
|
||||
from sklearn.utils._testing import ignore_warnings
|
||||
from sklearn.utils.fixes import CSR_CONTAINERS
|
||||
|
||||
|
||||
class MockClassifier:
|
||||
"""
|
||||
Dummy classifier to test recursive feature elimination
|
||||
"""
|
||||
|
||||
def __init__(self, foo_param=0):
|
||||
self.foo_param = foo_param
|
||||
|
||||
def fit(self, X, y):
|
||||
assert len(X) == len(y)
|
||||
self.coef_ = np.ones(X.shape[1], dtype=np.float64)
|
||||
return self
|
||||
|
||||
def predict(self, T):
|
||||
return T.shape[0]
|
||||
|
||||
predict_proba = predict
|
||||
decision_function = predict
|
||||
transform = predict
|
||||
|
||||
def score(self, X=None, y=None):
|
||||
return 0.0
|
||||
|
||||
def get_params(self, deep=True):
|
||||
return {"foo_param": self.foo_param}
|
||||
|
||||
def set_params(self, **params):
|
||||
return self
|
||||
|
||||
def _more_tags(self):
|
||||
return {"allow_nan": True}
|
||||
|
||||
|
||||
def test_rfe_features_importance():
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
# Add some irrelevant features. Random seed is set to make sure that
|
||||
# irrelevant features are always irrelevant.
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = iris.target
|
||||
|
||||
clf = RandomForestClassifier(n_estimators=20, random_state=generator, max_depth=2)
|
||||
rfe = RFE(estimator=clf, n_features_to_select=4, step=0.1)
|
||||
rfe.fit(X, y)
|
||||
assert len(rfe.ranking_) == X.shape[1]
|
||||
|
||||
clf_svc = SVC(kernel="linear")
|
||||
rfe_svc = RFE(estimator=clf_svc, n_features_to_select=4, step=0.1)
|
||||
rfe_svc.fit(X, y)
|
||||
|
||||
# Check if the supports are equal
|
||||
assert_array_equal(rfe.get_support(), rfe_svc.get_support())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
|
||||
def test_rfe(csr_container):
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
# Add some irrelevant features. Random seed is set to make sure that
|
||||
# irrelevant features are always irrelevant.
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
X_sparse = csr_container(X)
|
||||
y = iris.target
|
||||
|
||||
# dense model
|
||||
clf = SVC(kernel="linear")
|
||||
rfe = RFE(estimator=clf, n_features_to_select=4, step=0.1)
|
||||
rfe.fit(X, y)
|
||||
X_r = rfe.transform(X)
|
||||
clf.fit(X_r, y)
|
||||
assert len(rfe.ranking_) == X.shape[1]
|
||||
|
||||
# sparse model
|
||||
clf_sparse = SVC(kernel="linear")
|
||||
rfe_sparse = RFE(estimator=clf_sparse, n_features_to_select=4, step=0.1)
|
||||
rfe_sparse.fit(X_sparse, y)
|
||||
X_r_sparse = rfe_sparse.transform(X_sparse)
|
||||
|
||||
assert X_r.shape == iris.data.shape
|
||||
assert_array_almost_equal(X_r[:10], iris.data[:10])
|
||||
|
||||
assert_array_almost_equal(rfe.predict(X), clf.predict(iris.data))
|
||||
assert rfe.score(X, y) == clf.score(iris.data, iris.target)
|
||||
assert_array_almost_equal(X_r, X_r_sparse.toarray())
|
||||
|
||||
|
||||
def test_RFE_fit_score_params():
|
||||
# Make sure RFE passes the metadata down to fit and score methods of the
|
||||
# underlying estimator
|
||||
class TestEstimator(BaseEstimator, ClassifierMixin):
|
||||
def fit(self, X, y, prop=None):
|
||||
if prop is None:
|
||||
raise ValueError("fit: prop cannot be None")
|
||||
self.svc_ = SVC(kernel="linear").fit(X, y)
|
||||
self.coef_ = self.svc_.coef_
|
||||
return self
|
||||
|
||||
def score(self, X, y, prop=None):
|
||||
if prop is None:
|
||||
raise ValueError("score: prop cannot be None")
|
||||
return self.svc_.score(X, y)
|
||||
|
||||
X, y = load_iris(return_X_y=True)
|
||||
with pytest.raises(ValueError, match="fit: prop cannot be None"):
|
||||
RFE(estimator=TestEstimator()).fit(X, y)
|
||||
with pytest.raises(ValueError, match="score: prop cannot be None"):
|
||||
RFE(estimator=TestEstimator()).fit(X, y, prop="foo").score(X, y)
|
||||
|
||||
RFE(estimator=TestEstimator()).fit(X, y, prop="foo").score(X, y, prop="foo")
|
||||
|
||||
|
||||
def test_rfe_percent_n_features():
|
||||
# test that the results are the same
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
# Add some irrelevant features. Random seed is set to make sure that
|
||||
# irrelevant features are always irrelevant.
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = iris.target
|
||||
# there are 10 features in the data. We select 40%.
|
||||
clf = SVC(kernel="linear")
|
||||
rfe_num = RFE(estimator=clf, n_features_to_select=4, step=0.1)
|
||||
rfe_num.fit(X, y)
|
||||
|
||||
rfe_perc = RFE(estimator=clf, n_features_to_select=0.4, step=0.1)
|
||||
rfe_perc.fit(X, y)
|
||||
|
||||
assert_array_equal(rfe_perc.ranking_, rfe_num.ranking_)
|
||||
assert_array_equal(rfe_perc.support_, rfe_num.support_)
|
||||
|
||||
|
||||
def test_rfe_mockclassifier():
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
# Add some irrelevant features. Random seed is set to make sure that
|
||||
# irrelevant features are always irrelevant.
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = iris.target
|
||||
|
||||
# dense model
|
||||
clf = MockClassifier()
|
||||
rfe = RFE(estimator=clf, n_features_to_select=4, step=0.1)
|
||||
rfe.fit(X, y)
|
||||
X_r = rfe.transform(X)
|
||||
clf.fit(X_r, y)
|
||||
assert len(rfe.ranking_) == X.shape[1]
|
||||
assert X_r.shape == iris.data.shape
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
|
||||
def test_rfecv(csr_container):
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
# Add some irrelevant features. Random seed is set to make sure that
|
||||
# irrelevant features are always irrelevant.
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = list(iris.target) # regression test: list should be supported
|
||||
|
||||
# Test using the score function
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=1)
|
||||
rfecv.fit(X, y)
|
||||
# non-regression test for missing worst feature:
|
||||
|
||||
for key in rfecv.cv_results_.keys():
|
||||
assert len(rfecv.cv_results_[key]) == X.shape[1]
|
||||
|
||||
assert len(rfecv.ranking_) == X.shape[1]
|
||||
X_r = rfecv.transform(X)
|
||||
|
||||
# All the noisy variable were filtered out
|
||||
assert_array_equal(X_r, iris.data)
|
||||
|
||||
# same in sparse
|
||||
rfecv_sparse = RFECV(estimator=SVC(kernel="linear"), step=1)
|
||||
X_sparse = csr_container(X)
|
||||
rfecv_sparse.fit(X_sparse, y)
|
||||
X_r_sparse = rfecv_sparse.transform(X_sparse)
|
||||
assert_array_equal(X_r_sparse.toarray(), iris.data)
|
||||
|
||||
# Test using a customized loss function
|
||||
scoring = make_scorer(zero_one_loss, greater_is_better=False)
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, scoring=scoring)
|
||||
ignore_warnings(rfecv.fit)(X, y)
|
||||
X_r = rfecv.transform(X)
|
||||
assert_array_equal(X_r, iris.data)
|
||||
|
||||
# Test using a scorer
|
||||
scorer = get_scorer("accuracy")
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, scoring=scorer)
|
||||
rfecv.fit(X, y)
|
||||
X_r = rfecv.transform(X)
|
||||
assert_array_equal(X_r, iris.data)
|
||||
|
||||
# Test fix on cv_results_
|
||||
def test_scorer(estimator, X, y):
|
||||
return 1.0
|
||||
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, scoring=test_scorer)
|
||||
rfecv.fit(X, y)
|
||||
|
||||
# In the event of cross validation score ties, the expected behavior of
|
||||
# RFECV is to return the FEWEST features that maximize the CV score.
|
||||
# Because test_scorer always returns 1.0 in this example, RFECV should
|
||||
# reduce the dimensionality to a single feature (i.e. n_features_ = 1)
|
||||
assert rfecv.n_features_ == 1
|
||||
|
||||
# Same as the first two tests, but with step=2
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=2)
|
||||
rfecv.fit(X, y)
|
||||
|
||||
for key in rfecv.cv_results_.keys():
|
||||
assert len(rfecv.cv_results_[key]) == 6
|
||||
|
||||
assert len(rfecv.ranking_) == X.shape[1]
|
||||
X_r = rfecv.transform(X)
|
||||
assert_array_equal(X_r, iris.data)
|
||||
|
||||
rfecv_sparse = RFECV(estimator=SVC(kernel="linear"), step=2)
|
||||
X_sparse = csr_container(X)
|
||||
rfecv_sparse.fit(X_sparse, y)
|
||||
X_r_sparse = rfecv_sparse.transform(X_sparse)
|
||||
assert_array_equal(X_r_sparse.toarray(), iris.data)
|
||||
|
||||
# Verifying that steps < 1 don't blow up.
|
||||
rfecv_sparse = RFECV(estimator=SVC(kernel="linear"), step=0.2)
|
||||
X_sparse = csr_container(X)
|
||||
rfecv_sparse.fit(X_sparse, y)
|
||||
X_r_sparse = rfecv_sparse.transform(X_sparse)
|
||||
assert_array_equal(X_r_sparse.toarray(), iris.data)
|
||||
|
||||
|
||||
def test_rfecv_mockclassifier():
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = list(iris.target) # regression test: list should be supported
|
||||
|
||||
# Test using the score function
|
||||
rfecv = RFECV(estimator=MockClassifier(), step=1)
|
||||
rfecv.fit(X, y)
|
||||
# non-regression test for missing worst feature:
|
||||
|
||||
for key in rfecv.cv_results_.keys():
|
||||
assert len(rfecv.cv_results_[key]) == X.shape[1]
|
||||
|
||||
assert len(rfecv.ranking_) == X.shape[1]
|
||||
|
||||
|
||||
def test_rfecv_verbose_output():
|
||||
# Check verbose=1 is producing an output.
|
||||
import sys
|
||||
from io import StringIO
|
||||
|
||||
sys.stdout = StringIO()
|
||||
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = list(iris.target)
|
||||
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=1, verbose=1)
|
||||
rfecv.fit(X, y)
|
||||
|
||||
verbose_output = sys.stdout
|
||||
verbose_output.seek(0)
|
||||
assert len(verbose_output.readline()) > 0
|
||||
|
||||
|
||||
def test_rfecv_cv_results_size(global_random_seed):
|
||||
generator = check_random_state(global_random_seed)
|
||||
iris = load_iris()
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = list(iris.target) # regression test: list should be supported
|
||||
|
||||
# Non-regression test for varying combinations of step and
|
||||
# min_features_to_select.
|
||||
for step, min_features_to_select in [[2, 1], [2, 2], [3, 3]]:
|
||||
rfecv = RFECV(
|
||||
estimator=MockClassifier(),
|
||||
step=step,
|
||||
min_features_to_select=min_features_to_select,
|
||||
)
|
||||
rfecv.fit(X, y)
|
||||
|
||||
score_len = np.ceil((X.shape[1] - min_features_to_select) / step) + 1
|
||||
|
||||
for key in rfecv.cv_results_.keys():
|
||||
assert len(rfecv.cv_results_[key]) == score_len
|
||||
|
||||
assert len(rfecv.ranking_) == X.shape[1]
|
||||
assert rfecv.n_features_ >= min_features_to_select
|
||||
|
||||
|
||||
def test_rfe_estimator_tags():
|
||||
rfe = RFE(SVC(kernel="linear"))
|
||||
assert rfe._estimator_type == "classifier"
|
||||
# make sure that cross-validation is stratified
|
||||
iris = load_iris()
|
||||
score = cross_val_score(rfe, iris.data, iris.target)
|
||||
assert score.min() > 0.7
|
||||
|
||||
|
||||
def test_rfe_min_step(global_random_seed):
|
||||
n_features = 10
|
||||
X, y = make_friedman1(
|
||||
n_samples=50, n_features=n_features, random_state=global_random_seed
|
||||
)
|
||||
n_samples, n_features = X.shape
|
||||
estimator = SVR(kernel="linear")
|
||||
|
||||
# Test when floor(step * n_features) <= 0
|
||||
selector = RFE(estimator, step=0.01)
|
||||
sel = selector.fit(X, y)
|
||||
assert sel.support_.sum() == n_features // 2
|
||||
|
||||
# Test when step is between (0,1) and floor(step * n_features) > 0
|
||||
selector = RFE(estimator, step=0.20)
|
||||
sel = selector.fit(X, y)
|
||||
assert sel.support_.sum() == n_features // 2
|
||||
|
||||
# Test when step is an integer
|
||||
selector = RFE(estimator, step=5)
|
||||
sel = selector.fit(X, y)
|
||||
assert sel.support_.sum() == n_features // 2
|
||||
|
||||
|
||||
def test_number_of_subsets_of_features(global_random_seed):
|
||||
# In RFE, 'number_of_subsets_of_features'
|
||||
# = the number of iterations in '_fit'
|
||||
# = max(ranking_)
|
||||
# = 1 + (n_features + step - n_features_to_select - 1) // step
|
||||
# After optimization #4534, this number
|
||||
# = 1 + np.ceil((n_features - n_features_to_select) / float(step))
|
||||
# This test case is to test their equivalence, refer to #4534 and #3824
|
||||
|
||||
def formula1(n_features, n_features_to_select, step):
|
||||
return 1 + ((n_features + step - n_features_to_select - 1) // step)
|
||||
|
||||
def formula2(n_features, n_features_to_select, step):
|
||||
return 1 + np.ceil((n_features - n_features_to_select) / float(step))
|
||||
|
||||
# RFE
|
||||
# Case 1, n_features - n_features_to_select is divisible by step
|
||||
# Case 2, n_features - n_features_to_select is not divisible by step
|
||||
n_features_list = [11, 11]
|
||||
n_features_to_select_list = [3, 3]
|
||||
step_list = [2, 3]
|
||||
for n_features, n_features_to_select, step in zip(
|
||||
n_features_list, n_features_to_select_list, step_list
|
||||
):
|
||||
generator = check_random_state(global_random_seed)
|
||||
X = generator.normal(size=(100, n_features))
|
||||
y = generator.rand(100).round()
|
||||
rfe = RFE(
|
||||
estimator=SVC(kernel="linear"),
|
||||
n_features_to_select=n_features_to_select,
|
||||
step=step,
|
||||
)
|
||||
rfe.fit(X, y)
|
||||
# this number also equals to the maximum of ranking_
|
||||
assert np.max(rfe.ranking_) == formula1(n_features, n_features_to_select, step)
|
||||
assert np.max(rfe.ranking_) == formula2(n_features, n_features_to_select, step)
|
||||
|
||||
# In RFECV, 'fit' calls 'RFE._fit'
|
||||
# 'number_of_subsets_of_features' of RFE
|
||||
# = the size of each score in 'cv_results_' of RFECV
|
||||
# = the number of iterations of the for loop before optimization #4534
|
||||
|
||||
# RFECV, n_features_to_select = 1
|
||||
# Case 1, n_features - 1 is divisible by step
|
||||
# Case 2, n_features - 1 is not divisible by step
|
||||
|
||||
n_features_to_select = 1
|
||||
n_features_list = [11, 10]
|
||||
step_list = [2, 2]
|
||||
for n_features, step in zip(n_features_list, step_list):
|
||||
generator = check_random_state(global_random_seed)
|
||||
X = generator.normal(size=(100, n_features))
|
||||
y = generator.rand(100).round()
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"), step=step)
|
||||
rfecv.fit(X, y)
|
||||
|
||||
for key in rfecv.cv_results_.keys():
|
||||
assert len(rfecv.cv_results_[key]) == formula1(
|
||||
n_features, n_features_to_select, step
|
||||
)
|
||||
assert len(rfecv.cv_results_[key]) == formula2(
|
||||
n_features, n_features_to_select, step
|
||||
)
|
||||
|
||||
|
||||
def test_rfe_cv_n_jobs(global_random_seed):
|
||||
generator = check_random_state(global_random_seed)
|
||||
iris = load_iris()
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = iris.target
|
||||
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"))
|
||||
rfecv.fit(X, y)
|
||||
rfecv_ranking = rfecv.ranking_
|
||||
|
||||
rfecv_cv_results_ = rfecv.cv_results_
|
||||
|
||||
rfecv.set_params(n_jobs=2)
|
||||
rfecv.fit(X, y)
|
||||
assert_array_almost_equal(rfecv.ranking_, rfecv_ranking)
|
||||
|
||||
assert rfecv_cv_results_.keys() == rfecv.cv_results_.keys()
|
||||
for key in rfecv_cv_results_.keys():
|
||||
assert rfecv_cv_results_[key] == pytest.approx(rfecv.cv_results_[key])
|
||||
|
||||
|
||||
def test_rfe_cv_groups():
|
||||
generator = check_random_state(0)
|
||||
iris = load_iris()
|
||||
number_groups = 4
|
||||
groups = np.floor(np.linspace(0, number_groups, len(iris.target)))
|
||||
X = iris.data
|
||||
y = (iris.target > 0).astype(int)
|
||||
|
||||
est_groups = RFECV(
|
||||
estimator=RandomForestClassifier(random_state=generator),
|
||||
step=1,
|
||||
scoring="accuracy",
|
||||
cv=GroupKFold(n_splits=2),
|
||||
)
|
||||
est_groups.fit(X, y, groups=groups)
|
||||
assert est_groups.n_features_ > 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"importance_getter", [attrgetter("regressor_.coef_"), "regressor_.coef_"]
|
||||
)
|
||||
@pytest.mark.parametrize("selector, expected_n_features", [(RFE, 5), (RFECV, 4)])
|
||||
def test_rfe_wrapped_estimator(importance_getter, selector, expected_n_features):
|
||||
# Non-regression test for
|
||||
# https://github.com/scikit-learn/scikit-learn/issues/15312
|
||||
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
|
||||
estimator = LinearSVR(random_state=0)
|
||||
|
||||
log_estimator = TransformedTargetRegressor(
|
||||
regressor=estimator, func=np.log, inverse_func=np.exp
|
||||
)
|
||||
|
||||
selector = selector(log_estimator, importance_getter=importance_getter)
|
||||
sel = selector.fit(X, y)
|
||||
assert sel.support_.sum() == expected_n_features
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"importance_getter, err_type",
|
||||
[
|
||||
("auto", ValueError),
|
||||
("random", AttributeError),
|
||||
(lambda x: x.importance, AttributeError),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("Selector", [RFE, RFECV])
|
||||
def test_rfe_importance_getter_validation(importance_getter, err_type, Selector):
|
||||
X, y = make_friedman1(n_samples=50, n_features=10, random_state=42)
|
||||
estimator = LinearSVR()
|
||||
log_estimator = TransformedTargetRegressor(
|
||||
regressor=estimator, func=np.log, inverse_func=np.exp
|
||||
)
|
||||
|
||||
with pytest.raises(err_type):
|
||||
model = Selector(log_estimator, importance_getter=importance_getter)
|
||||
model.fit(X, y)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cv", [None, 5])
|
||||
def test_rfe_allow_nan_inf_in_x(cv):
|
||||
iris = load_iris()
|
||||
X = iris.data
|
||||
y = iris.target
|
||||
|
||||
# add nan and inf value to X
|
||||
X[0][0] = np.nan
|
||||
X[0][1] = np.inf
|
||||
|
||||
clf = MockClassifier()
|
||||
if cv is not None:
|
||||
rfe = RFECV(estimator=clf, cv=cv)
|
||||
else:
|
||||
rfe = RFE(estimator=clf)
|
||||
rfe.fit(X, y)
|
||||
rfe.transform(X)
|
||||
|
||||
|
||||
def test_w_pipeline_2d_coef_():
|
||||
pipeline = make_pipeline(StandardScaler(), LogisticRegression())
|
||||
|
||||
data, y = load_iris(return_X_y=True)
|
||||
sfm = RFE(
|
||||
pipeline,
|
||||
n_features_to_select=2,
|
||||
importance_getter="named_steps.logisticregression.coef_",
|
||||
)
|
||||
|
||||
sfm.fit(data, y)
|
||||
assert sfm.transform(data).shape[1] == 2
|
||||
|
||||
|
||||
def test_rfecv_std_and_mean(global_random_seed):
|
||||
generator = check_random_state(global_random_seed)
|
||||
iris = load_iris()
|
||||
X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))]
|
||||
y = iris.target
|
||||
|
||||
rfecv = RFECV(estimator=SVC(kernel="linear"))
|
||||
rfecv.fit(X, y)
|
||||
split_keys = [key for key in rfecv.cv_results_.keys() if "split" in key]
|
||||
cv_scores = np.asarray([rfecv.cv_results_[key] for key in split_keys])
|
||||
expected_mean = np.mean(cv_scores, axis=0)
|
||||
expected_std = np.std(cv_scores, axis=0)
|
||||
|
||||
assert_allclose(rfecv.cv_results_["mean_test_score"], expected_mean)
|
||||
assert_allclose(rfecv.cv_results_["std_test_score"], expected_std)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
["min_features_to_select", "n_features", "step", "cv_results_n_features"],
|
||||
[
|
||||
[1, 4, 1, np.array([1, 2, 3, 4])],
|
||||
[1, 5, 1, np.array([1, 2, 3, 4, 5])],
|
||||
[1, 4, 2, np.array([1, 2, 4])],
|
||||
[1, 5, 2, np.array([1, 3, 5])],
|
||||
[1, 4, 3, np.array([1, 4])],
|
||||
[1, 5, 3, np.array([1, 2, 5])],
|
||||
[1, 4, 4, np.array([1, 4])],
|
||||
[1, 5, 4, np.array([1, 5])],
|
||||
[4, 4, 2, np.array([4])],
|
||||
[4, 5, 1, np.array([4, 5])],
|
||||
[4, 5, 2, np.array([4, 5])],
|
||||
],
|
||||
)
|
||||
def test_rfecv_cv_results_n_features(
|
||||
min_features_to_select,
|
||||
n_features,
|
||||
step,
|
||||
cv_results_n_features,
|
||||
):
|
||||
X, y = make_classification(
|
||||
n_samples=20, n_features=n_features, n_informative=n_features, n_redundant=0
|
||||
)
|
||||
rfecv = RFECV(
|
||||
estimator=SVC(kernel="linear"),
|
||||
step=step,
|
||||
min_features_to_select=min_features_to_select,
|
||||
)
|
||||
rfecv.fit(X, y)
|
||||
assert_array_equal(rfecv.cv_results_["n_features"], cv_results_n_features)
|
||||
assert all(
|
||||
len(value) == len(rfecv.cv_results_["n_features"])
|
||||
for value in rfecv.cv_results_.values()
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ClsRFE", [RFE, RFECV])
|
||||
def test_multioutput(ClsRFE):
|
||||
X = np.random.normal(size=(10, 3))
|
||||
y = np.random.randint(2, size=(10, 2))
|
||||
clf = RandomForestClassifier(n_estimators=5)
|
||||
rfe_test = ClsRFE(clf)
|
||||
rfe_test.fit(X, y)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ClsRFE", [RFE, RFECV])
|
||||
def test_pipeline_with_nans(ClsRFE):
|
||||
"""Check that RFE works with pipeline that accept nans.
|
||||
|
||||
Non-regression test for gh-21743.
|
||||
"""
|
||||
X, y = load_iris(return_X_y=True)
|
||||
X[0, 0] = np.nan
|
||||
|
||||
pipe = make_pipeline(
|
||||
SimpleImputer(),
|
||||
StandardScaler(),
|
||||
LogisticRegression(),
|
||||
)
|
||||
|
||||
fs = ClsRFE(
|
||||
estimator=pipe,
|
||||
importance_getter="named_steps.logisticregression.coef_",
|
||||
)
|
||||
fs.fit(X, y)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ClsRFE", [RFE, RFECV])
|
||||
@pytest.mark.parametrize("PLSEstimator", [CCA, PLSCanonical, PLSRegression])
|
||||
def test_rfe_pls(ClsRFE, PLSEstimator):
|
||||
"""Check the behaviour of RFE with PLS estimators.
|
||||
|
||||
Non-regression test for:
|
||||
https://github.com/scikit-learn/scikit-learn/issues/12410
|
||||
"""
|
||||
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
|
||||
estimator = PLSEstimator(n_components=1)
|
||||
selector = ClsRFE(estimator, step=1).fit(X, y)
|
||||
assert selector.score(X, y) > 0.5
|
||||
|
||||
|
||||
def test_rfe_estimator_attribute_error():
|
||||
"""Check that we raise the proper AttributeError when the estimator
|
||||
does not implement the `decision_function` method, which is decorated with
|
||||
`available_if`.
|
||||
|
||||
Non-regression test for:
|
||||
https://github.com/scikit-learn/scikit-learn/issues/28108
|
||||
"""
|
||||
iris = load_iris()
|
||||
|
||||
# `LinearRegression` does not implement 'decision_function' and should raise an
|
||||
# AttributeError
|
||||
rfe = RFE(estimator=LinearRegression())
|
||||
|
||||
outer_msg = "This 'RFE' has no attribute 'decision_function'"
|
||||
inner_msg = "'LinearRegression' object has no attribute 'decision_function'"
|
||||
with pytest.raises(AttributeError, match=outer_msg) as exec_info:
|
||||
rfe.fit(iris.data, iris.target).decision_function(iris.data)
|
||||
assert isinstance(exec_info.value.__cause__, AttributeError)
|
||||
assert inner_msg in str(exec_info.value.__cause__)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"ClsRFE, param", [(RFE, "n_features_to_select"), (RFECV, "min_features_to_select")]
|
||||
)
|
||||
def test_rfe_n_features_to_select_warning(ClsRFE, param):
|
||||
"""Check if the correct warning is raised when trying to initialize a RFE
|
||||
object with a n_features_to_select attribute larger than the number of
|
||||
features present in the X variable that is passed to the fit method
|
||||
"""
|
||||
X, y = make_classification(n_features=20, random_state=0)
|
||||
|
||||
with pytest.warns(UserWarning, match=f"{param}=21 > n_features=20"):
|
||||
# Create RFE/RFECV with n_features_to_select/min_features_to_select
|
||||
# larger than the number of features present in the X variable
|
||||
clsrfe = ClsRFE(estimator=LogisticRegression(), **{param: 21})
|
||||
clsrfe.fit(X, y)
|
||||
@ -0,0 +1,323 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
from numpy.testing import assert_array_equal
|
||||
|
||||
from sklearn.cluster import KMeans
|
||||
from sklearn.datasets import make_blobs, make_classification, make_regression
|
||||
from sklearn.ensemble import HistGradientBoostingRegressor
|
||||
from sklearn.feature_selection import SequentialFeatureSelector
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.model_selection import LeaveOneGroupOut, cross_val_score
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
from sklearn.pipeline import make_pipeline
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.utils.fixes import CSR_CONTAINERS
|
||||
|
||||
|
||||
def test_bad_n_features_to_select():
|
||||
n_features = 5
|
||||
X, y = make_regression(n_features=n_features)
|
||||
sfs = SequentialFeatureSelector(LinearRegression(), n_features_to_select=n_features)
|
||||
with pytest.raises(ValueError, match="n_features_to_select must be < n_features"):
|
||||
sfs.fit(X, y)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ("forward", "backward"))
|
||||
@pytest.mark.parametrize("n_features_to_select", (1, 5, 9, "auto"))
|
||||
def test_n_features_to_select(direction, n_features_to_select):
|
||||
# Make sure n_features_to_select is respected
|
||||
|
||||
n_features = 10
|
||||
X, y = make_regression(n_features=n_features, random_state=0)
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(),
|
||||
n_features_to_select=n_features_to_select,
|
||||
direction=direction,
|
||||
cv=2,
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
|
||||
if n_features_to_select == "auto":
|
||||
n_features_to_select = n_features // 2
|
||||
|
||||
assert sfs.get_support(indices=True).shape[0] == n_features_to_select
|
||||
assert sfs.n_features_to_select_ == n_features_to_select
|
||||
assert sfs.transform(X).shape[1] == n_features_to_select
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ("forward", "backward"))
|
||||
def test_n_features_to_select_auto(direction):
|
||||
"""Check the behaviour of `n_features_to_select="auto"` with different
|
||||
values for the parameter `tol`.
|
||||
"""
|
||||
|
||||
n_features = 10
|
||||
tol = 1e-3
|
||||
X, y = make_regression(n_features=n_features, random_state=0)
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(),
|
||||
n_features_to_select="auto",
|
||||
tol=tol,
|
||||
direction=direction,
|
||||
cv=2,
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
|
||||
max_features_to_select = n_features - 1
|
||||
|
||||
assert sfs.get_support(indices=True).shape[0] <= max_features_to_select
|
||||
assert sfs.n_features_to_select_ <= max_features_to_select
|
||||
assert sfs.transform(X).shape[1] <= max_features_to_select
|
||||
assert sfs.get_support(indices=True).shape[0] == sfs.n_features_to_select_
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ("forward", "backward"))
|
||||
def test_n_features_to_select_stopping_criterion(direction):
|
||||
"""Check the behaviour stopping criterion for feature selection
|
||||
depending on the values of `n_features_to_select` and `tol`.
|
||||
|
||||
When `direction` is `'forward'`, select a new features at random
|
||||
among those not currently selected in selector.support_,
|
||||
build a new version of the data that includes all the features
|
||||
in selector.support_ + this newly selected feature.
|
||||
And check that the cross-validation score of the model trained on
|
||||
this new dataset variant is lower than the model with
|
||||
the selected forward selected features or at least does not improve
|
||||
by more than the tol margin.
|
||||
|
||||
When `direction` is `'backward'`, instead of adding a new feature
|
||||
to selector.support_, try to remove one of those selected features at random
|
||||
And check that the cross-validation score is either decreasing or
|
||||
not improving by more than the tol margin.
|
||||
"""
|
||||
|
||||
X, y = make_regression(n_features=50, n_informative=10, random_state=0)
|
||||
|
||||
tol = 1e-3
|
||||
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(),
|
||||
n_features_to_select="auto",
|
||||
tol=tol,
|
||||
direction=direction,
|
||||
cv=2,
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
selected_X = sfs.transform(X)
|
||||
|
||||
rng = np.random.RandomState(0)
|
||||
|
||||
added_candidates = list(set(range(X.shape[1])) - set(sfs.get_support(indices=True)))
|
||||
added_X = np.hstack(
|
||||
[
|
||||
selected_X,
|
||||
(X[:, rng.choice(added_candidates)])[:, np.newaxis],
|
||||
]
|
||||
)
|
||||
|
||||
removed_candidate = rng.choice(list(range(sfs.n_features_to_select_)))
|
||||
removed_X = np.delete(selected_X, removed_candidate, axis=1)
|
||||
|
||||
plain_cv_score = cross_val_score(LinearRegression(), X, y, cv=2).mean()
|
||||
sfs_cv_score = cross_val_score(LinearRegression(), selected_X, y, cv=2).mean()
|
||||
added_cv_score = cross_val_score(LinearRegression(), added_X, y, cv=2).mean()
|
||||
removed_cv_score = cross_val_score(LinearRegression(), removed_X, y, cv=2).mean()
|
||||
|
||||
assert sfs_cv_score >= plain_cv_score
|
||||
|
||||
if direction == "forward":
|
||||
assert (sfs_cv_score - added_cv_score) <= tol
|
||||
assert (sfs_cv_score - removed_cv_score) >= tol
|
||||
else:
|
||||
assert (added_cv_score - sfs_cv_score) <= tol
|
||||
assert (removed_cv_score - sfs_cv_score) <= tol
|
||||
|
||||
|
||||
@pytest.mark.parametrize("direction", ("forward", "backward"))
|
||||
@pytest.mark.parametrize(
|
||||
"n_features_to_select, expected",
|
||||
(
|
||||
(0.1, 1),
|
||||
(1.0, 10),
|
||||
(0.5, 5),
|
||||
),
|
||||
)
|
||||
def test_n_features_to_select_float(direction, n_features_to_select, expected):
|
||||
# Test passing a float as n_features_to_select
|
||||
X, y = make_regression(n_features=10)
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(),
|
||||
n_features_to_select=n_features_to_select,
|
||||
direction=direction,
|
||||
cv=2,
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
assert sfs.n_features_to_select_ == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("seed", range(10))
|
||||
@pytest.mark.parametrize("direction", ("forward", "backward"))
|
||||
@pytest.mark.parametrize(
|
||||
"n_features_to_select, expected_selected_features",
|
||||
[
|
||||
(2, [0, 2]), # f1 is dropped since it has no predictive power
|
||||
(1, [2]), # f2 is more predictive than f0 so it's kept
|
||||
],
|
||||
)
|
||||
def test_sanity(seed, direction, n_features_to_select, expected_selected_features):
|
||||
# Basic sanity check: 3 features, only f0 and f2 are correlated with the
|
||||
# target, f2 having a stronger correlation than f0. We expect f1 to be
|
||||
# dropped, and f2 to always be selected.
|
||||
|
||||
rng = np.random.RandomState(seed)
|
||||
n_samples = 100
|
||||
X = rng.randn(n_samples, 3)
|
||||
y = 3 * X[:, 0] - 10 * X[:, 2]
|
||||
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(),
|
||||
n_features_to_select=n_features_to_select,
|
||||
direction=direction,
|
||||
cv=2,
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
assert_array_equal(sfs.get_support(indices=True), expected_selected_features)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
|
||||
def test_sparse_support(csr_container):
|
||||
# Make sure sparse data is supported
|
||||
|
||||
X, y = make_regression(n_features=10)
|
||||
X = csr_container(X)
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(), n_features_to_select="auto", cv=2
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
sfs.transform(X)
|
||||
|
||||
|
||||
def test_nan_support():
|
||||
# Make sure nans are OK if the underlying estimator supports nans
|
||||
|
||||
rng = np.random.RandomState(0)
|
||||
n_samples, n_features = 40, 4
|
||||
X, y = make_regression(n_samples, n_features, random_state=0)
|
||||
nan_mask = rng.randint(0, 2, size=(n_samples, n_features), dtype=bool)
|
||||
X[nan_mask] = np.nan
|
||||
sfs = SequentialFeatureSelector(
|
||||
HistGradientBoostingRegressor(), n_features_to_select="auto", cv=2
|
||||
)
|
||||
sfs.fit(X, y)
|
||||
sfs.transform(X)
|
||||
|
||||
with pytest.raises(ValueError, match="Input X contains NaN"):
|
||||
# LinearRegression does not support nans
|
||||
SequentialFeatureSelector(
|
||||
LinearRegression(), n_features_to_select="auto", cv=2
|
||||
).fit(X, y)
|
||||
|
||||
|
||||
def test_pipeline_support():
|
||||
# Make sure that pipelines can be passed into SFS and that SFS can be
|
||||
# passed into a pipeline
|
||||
|
||||
n_samples, n_features = 50, 3
|
||||
X, y = make_regression(n_samples, n_features, random_state=0)
|
||||
|
||||
# pipeline in SFS
|
||||
pipe = make_pipeline(StandardScaler(), LinearRegression())
|
||||
sfs = SequentialFeatureSelector(pipe, n_features_to_select="auto", cv=2)
|
||||
sfs.fit(X, y)
|
||||
sfs.transform(X)
|
||||
|
||||
# SFS in pipeline
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(), n_features_to_select="auto", cv=2
|
||||
)
|
||||
pipe = make_pipeline(StandardScaler(), sfs)
|
||||
pipe.fit(X, y)
|
||||
pipe.transform(X)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("n_features_to_select", (2, 3))
|
||||
def test_unsupervised_model_fit(n_features_to_select):
|
||||
# Make sure that models without classification labels are not being
|
||||
# validated
|
||||
|
||||
X, y = make_blobs(n_features=4)
|
||||
sfs = SequentialFeatureSelector(
|
||||
KMeans(n_init=1),
|
||||
n_features_to_select=n_features_to_select,
|
||||
)
|
||||
sfs.fit(X)
|
||||
assert sfs.transform(X).shape[1] == n_features_to_select
|
||||
|
||||
|
||||
@pytest.mark.parametrize("y", ("no_validation", 1j, 99.9, np.nan, 3))
|
||||
def test_no_y_validation_model_fit(y):
|
||||
# Make sure that other non-conventional y labels are not accepted
|
||||
|
||||
X, clusters = make_blobs(n_features=6)
|
||||
sfs = SequentialFeatureSelector(
|
||||
KMeans(),
|
||||
n_features_to_select=3,
|
||||
)
|
||||
|
||||
with pytest.raises((TypeError, ValueError)):
|
||||
sfs.fit(X, y)
|
||||
|
||||
|
||||
def test_forward_neg_tol_error():
|
||||
"""Check that we raise an error when tol<0 and direction='forward'"""
|
||||
X, y = make_regression(n_features=10, random_state=0)
|
||||
sfs = SequentialFeatureSelector(
|
||||
LinearRegression(),
|
||||
n_features_to_select="auto",
|
||||
direction="forward",
|
||||
tol=-1e-3,
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="tol must be strictly positive"):
|
||||
sfs.fit(X, y)
|
||||
|
||||
|
||||
def test_backward_neg_tol():
|
||||
"""Check that SequentialFeatureSelector works negative tol
|
||||
|
||||
non-regression test for #25525
|
||||
"""
|
||||
X, y = make_regression(n_features=10, random_state=0)
|
||||
lr = LinearRegression()
|
||||
initial_score = lr.fit(X, y).score(X, y)
|
||||
|
||||
sfs = SequentialFeatureSelector(
|
||||
lr,
|
||||
n_features_to_select="auto",
|
||||
direction="backward",
|
||||
tol=-1e-3,
|
||||
)
|
||||
Xr = sfs.fit_transform(X, y)
|
||||
new_score = lr.fit(Xr, y).score(Xr, y)
|
||||
|
||||
assert 0 < sfs.get_support().sum() < X.shape[1]
|
||||
assert new_score < initial_score
|
||||
|
||||
|
||||
def test_cv_generator_support():
|
||||
"""Check that no exception raised when cv is generator
|
||||
|
||||
non-regression test for #25957
|
||||
"""
|
||||
X, y = make_classification(random_state=0)
|
||||
|
||||
groups = np.zeros_like(y, dtype=int)
|
||||
groups[y.size // 2 :] = 1
|
||||
|
||||
cv = LeaveOneGroupOut()
|
||||
splits = cv.split(X, y, groups=groups)
|
||||
|
||||
knc = KNeighborsClassifier(n_neighbors=5)
|
||||
|
||||
sfs = SequentialFeatureSelector(knc, n_features_to_select=5, cv=splits)
|
||||
sfs.fit(X, y)
|
||||
@ -0,0 +1,72 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from sklearn.feature_selection import VarianceThreshold
|
||||
from sklearn.utils._testing import assert_array_equal
|
||||
from sklearn.utils.fixes import BSR_CONTAINERS, CSC_CONTAINERS, CSR_CONTAINERS
|
||||
|
||||
data = [[0, 1, 2, 3, 4], [0, 2, 2, 3, 5], [1, 1, 2, 4, 0]]
|
||||
|
||||
data2 = [[-0.13725701]] * 10
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"sparse_container", [None] + BSR_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
|
||||
)
|
||||
def test_zero_variance(sparse_container):
|
||||
# Test VarianceThreshold with default setting, zero variance.
|
||||
X = data if sparse_container is None else sparse_container(data)
|
||||
sel = VarianceThreshold().fit(X)
|
||||
assert_array_equal([0, 1, 3, 4], sel.get_support(indices=True))
|
||||
|
||||
|
||||
def test_zero_variance_value_error():
|
||||
# Test VarianceThreshold with default setting, zero variance, error cases.
|
||||
with pytest.raises(ValueError):
|
||||
VarianceThreshold().fit([[0, 1, 2, 3]])
|
||||
with pytest.raises(ValueError):
|
||||
VarianceThreshold().fit([[0, 1], [0, 1]])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("sparse_container", [None] + CSR_CONTAINERS)
|
||||
def test_variance_threshold(sparse_container):
|
||||
# Test VarianceThreshold with custom variance.
|
||||
X = data if sparse_container is None else sparse_container(data)
|
||||
X = VarianceThreshold(threshold=0.4).fit_transform(X)
|
||||
assert (len(data), 1) == X.shape
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
np.var(data2) == 0,
|
||||
reason=(
|
||||
"This test is not valid for this platform, "
|
||||
"as it relies on numerical instabilities."
|
||||
),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"sparse_container", [None] + BSR_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
|
||||
)
|
||||
def test_zero_variance_floating_point_error(sparse_container):
|
||||
# Test that VarianceThreshold(0.0).fit eliminates features that have
|
||||
# the same value in every sample, even when floating point errors
|
||||
# cause np.var not to be 0 for the feature.
|
||||
# See #13691
|
||||
X = data2 if sparse_container is None else sparse_container(data2)
|
||||
msg = "No feature in X meets the variance threshold 0.00000"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
VarianceThreshold().fit(X)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"sparse_container", [None] + BSR_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
|
||||
)
|
||||
def test_variance_nan(sparse_container):
|
||||
arr = np.array(data, dtype=np.float64)
|
||||
# add single NaN and feature should still be included
|
||||
arr[0, 0] = np.nan
|
||||
# make all values in feature NaN and feature should be rejected
|
||||
arr[:, 1] = np.nan
|
||||
|
||||
X = arr if sparse_container is None else sparse_container(arr)
|
||||
sel = VarianceThreshold().fit(X)
|
||||
assert_array_equal([0, 3, 4], sel.get_support(indices=True))
|
||||
Reference in New Issue
Block a user