|
28 | 28 | from sklearn.datasets import load_boston, load_iris
|
29 | 29 | from sklearn.utils import check_random_state
|
30 | 30 |
|
31 |
| -from scipy.sparse import csc_matrix, csr_matrix |
| 31 | +from scipy.sparse import csc_matrix |
32 | 32 |
|
33 | 33 | rng = check_random_state(0)
|
34 | 34 |
|
@@ -81,19 +81,27 @@ def test_sparse_classification():
|
81 | 81 | "bootstrap": [True, False],
|
82 | 82 | "bootstrap_features": [True, False]})
|
83 | 83 |
|
84 |
| - for base_estimator in [DummyClassifier(), |
85 |
| - Perceptron(), |
86 |
| - KNeighborsClassifier(), |
87 |
| - SVC()]: |
88 |
| - for params in grid: |
89 |
| - for sparse_format in [csc_matrix, csr_matrix]: |
90 |
| - X_train_sparse = sparse_format(X_train) |
91 |
| - X_test_sparse = sparse_format(X_test) |
92 |
| - BaggingClassifier( |
93 |
| - base_estimator=base_estimator, |
94 |
| - random_state=rng, |
95 |
| - **params |
96 |
| - ).fit(X_train_sparse, y_train).predict(X_test_sparse)
10000
|
| 84 | + base_estimator = SVC() |
| 85 | + for params in grid: |
| 86 | + sparse_format = csc_matrix |
| 87 | + X_train_sparse = sparse_format(X_train) |
| 88 | + X_test_sparse = sparse_format(X_test) |
| 89 | + |
| 90 | + # Trained on sparse format |
| 91 | + sparse_results = BaggingClassifier( |
| 92 | + base_estimator=base_estimator, |
| 93 | + random_state=check_random_state(1), |
| 94 | + **params |
| 95 | + ).fit(X_train_sparse, y_train).predict(X_test_sparse) |
| 96 | + |
| 97 | + # Trained on dense format |
| 98 | + dense_results = BaggingClassifier( |
| 99 | + base_estimator=base_estimator, |
| 100 | + random_state=check_random_state(1), |
| 101 | + **params |
| 102 | + ).fit(X_train, y_train).predict(X_test) |
| 103 | + |
| 104 | + assert_array_equal(sparse_results, dense_results) |
97 | 105 |
|
98 | 106 |
|
99 | 107 | def test_regression():
|
@@ -129,18 +137,27 @@ def test_sparse_regression():
|
129 | 137 | "bootstrap": [True, False],
|
130 | 138 | "bootstrap_features": [True, False]})
|
131 | 139 |
|
132 |
| - for base_estimator in [DummyRegressor(), |
133 |
| - KNeighborsRegressor(), |
134 |
| - SVR()]: |
135 |
| - for params in grid: |
136 |
| - for sparse_format in [csc_matrix, csr_matrix]: |
137 |
| - X_train_sparse = sparse_format(X_train) |
138 |
| - X_test_sparse = sparse_format(X_test) |
139 |
| - BaggingRegressor( |
140 |
| - base_estimator=base_estimator, |
141 |
| - random_state=rng, |
142 |
| - **params |
143 |
| - ).fit(X_train_sparse, y_train).predict(X_test_sparse) |
| 140 | + base_estimator = SVR() |
| 141 | + for params in grid: |
| 142 | + sparse_format = csc_matrix |
| 143 | + X_train_sparse = sparse_format(X_train) |
| 144 | + X_test_sparse = sparse_format(X_test) |
| 145 | + |
| 146 | + # Trained on sparse format |
| 147 | + sparse_results = BaggingRegressor( |
| 148 | + base_estimator=base_estimator, |
| 149 | + random_state=check_random_state(1), |
| 150 | + **params |
| 151 | + ).fit(X_train_sparse, y_train).predict(X_test_sparse) |
| 152 | + |
| 153 | + # Trained on dense format |
| 154 | + dense_results = BaggingRegressor( |
| 155 | + base_estimator=base_estimator, |
| 156 | + random_state=check_random_state(1), |
| 157 | + **params |
| 158 | + ).fit(X_train, y_train).predict(X_test) |
| 159 | + |
| 160 | + assert_array_equal(sparse_results, dense_results) |
144 | 161 |
|
145 | 162 |
|
146 | 163 | def test_bootstrap_samples():
|
|
0 commit comments