7
7
import numpy as np
8
8
import pytest
9
9
from numpy .testing import assert_allclose , assert_array_almost_equal , assert_array_equal
10
- from scipy import sparse
11
10
12
11
from sklearn .base import BaseEstimator , ClassifierMixin
13
12
from sklearn .compose import TransformedTargetRegressor
23
22
from sklearn .svm import SVC , SVR , LinearSVR
24
23
from sklearn .utils import check_random_state
25
24
from sklearn .utils ._testing import ignore_warnings
25
+ from sklearn .utils .fixes import CSR_CONTAINERS
26
26
27
27
28
28
class MockClassifier :
@@ -79,13 +79,14 @@ def test_rfe_features_importance():
79
79
assert_array_equal (rfe .get_support (), rfe_svc .get_support ())
80
80
81
81
82
- def test_rfe ():
82
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
83
+ def test_rfe (csr_container ):
83
84
generator = check_random_state (0 )
84
85
iris = load_iris ()
85
86
# Add some irrelevant features. Random seed is set to make sure that
86
87
# irrelevant features are always irrelevant.
87
88
X = np .c_ [iris .data , generator .normal (size = (len (iris .data ), 6 ))]
88
- X_sparse = sparse . csr_matrix (X )
89
+ X_sparse = csr_container (X )
89
90
y = iris .target
90
91
91
92
# dense model
@@ -173,7 +174,8 @@ def test_rfe_mockclassifier():
173
174
assert X_r .shape == iris .data .shape
174
175
175
176
176
- def test_rfecv ():
177
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
178
+ def test_rfecv (csr_container ):
177
179
generator = check_random_state (0 )
178
180
iris = load_iris ()
179
181
# Add some irrelevant features. Random seed is set to make sure that
@@ -197,7 +199,7 @@ def test_rfecv():
197
199
198
200
# same in sparse
199
201
rfecv_sparse = RFECV (estimator = SVC (kernel = "linear" ), step = 1 )
200
- X_sparse = sparse . csr_matrix (X )
202
+ X_sparse = csr_container (X )
201
203
rfecv_sparse .fit (X_sparse , y )
202
204
X_r_sparse = rfecv_sparse .transform (X_sparse )
203
205
assert_array_equal (X_r_sparse .toarray (), iris .data )
@@ -241,14 +243,14 @@ def test_scorer(estimator, X, y):
241
243
assert_array_equal (X_r , iris .data )
242
244
243
245
rfecv_sparse = RFECV (estimator = SVC (kernel = "linear" ), step = 2 )
244
- X_sparse = sparse . csr_matrix (X )
246
+ X_sparse = csr_container (X )
245
247
rfecv_sparse .fit (X_sparse , y )
246
248
X_r_sparse = rfecv_sparse .transform (X_sparse )
247
249
assert_array_equal (X_r_sparse .toarray (), iris .data )
248
250
249
251
# Verifying that steps < 1 don't blow up.
250
252
rfecv_sparse = RFECV (estimator = SVC (kernel = "linear" ), step = 0.2 )
251
- X_sparse = sparse . csr_matrix (X )
253
+ X_sparse = csr_container (X )
252
254
rfecv_sparse .fit (X_sparse , y )
253
255
X_r_sparse = rfecv_sparse .transform (X_sparse )
254
256
assert_array_equal (X_r_sparse .toarray (), iris .data )
0 commit comments