7
7
import numpy as np
8
8
import pytest
9
9
from numpy .testing import assert_allclose
10
- from scipy .sparse import coo_matrix , csc_matrix , csr_matrix
11
10
12
11
from sklearn import datasets
13
12
from sklearn .base import clone
31
30
assert_array_equal ,
32
31
skip_if_32bit ,
33
32
)
33
+ from sklearn .utils .fixes import COO_CONTAINERS , CSC_CONTAINERS , CSR_CONTAINERS
34
34
35
35
GRADIENT_BOOSTING_ESTIMATORS = [GradientBoostingClassifier , GradientBoostingRegressor ]
36
36
@@ -288,11 +288,12 @@ def test_single_class_with_sample_weight():
288
288
clf .fit (X , y , sample_weight = sample_weight )
289
289
290
290
291
- def test_check_inputs_predict_stages ():
291
+ @pytest .mark .parametrize ("csc_container" , CSC_CONTAINERS )
292
+ def test_check_inputs_predict_stages (csc_container ):
292
293
# check that predict_stages through an error if the type of X is not
293
294
# supported
294
295
x , y = datasets .make_hastie_10_2 (n_samples = 100 , random_state = 1 )
295
- x_sparse_csc = csc_matrix (x )
296
+ x_sparse_csc = csc_container (x )
296
297
clf = GradientBoostingClassifier (n_estimators = 100 , random_state = 1 )
297
298
clf .fit (x , y )
298
299
score = np .zeros ((y .shape )).reshape (- 1 , 1 )
@@ -913,10 +914,12 @@ def test_warm_start_oob(Cls):
913
914
914
915
915
916
@pytest .mark .parametrize ("Cls" , GRADIENT_BOOSTING_ESTIMATORS )
916
- def test_warm_start_sparse (Cls ):
917
+ @pytest .mark .parametrize (
918
+ "sparse_container" , COO_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
919
+ )
920
+ def test_warm_start_sparse (Cls , sparse_container ):
917
921
# Test that all sparse matrix types are supported
918
922
X , y = datasets .make_hastie_10_2 (n_samples = 100 , random_state = 1 )
919
- sparse_matrix_type = [csr_matrix , csc_matrix , coo_matrix ]
920
923
est_dense = Cls (
921
924
n_estimators = 100 , max_depth = 1 , subsample = 0.5 , random_state = 1 , warm_start = True
922
925
)
@@ -926,31 +929,28 @@ def test_warm_start_sparse(Cls):
926
929
est_dense .fit (X , y )
927
930
y_pred_dense = est_dense .predict (X )
928
931
929
- for sparse_constructor in sparse_matrix_type :
930
- X_sparse = sparse_constructor (X )
932
+ X_sparse = sparse_container (X )
931
933
932
- est_sparse = Cls (
933
- n_estimators = 100 ,
934
- max_depth = 1 ,
935
- subsample = 0.5 ,
936
- random_state = 1 ,
937
- warm_start = True ,
938
- )
939
- est_sparse .fit (X_sparse , y )
940
- est_sparse .predict (X )
941
- est_sparse .set_params (n_estimators = 200 )
942
- est_sparse .fit (X_sparse , y )
943
- y_pred_sparse = est_sparse .predict (X )
934
+ est_sparse = Cls (
935
+ n_estimators = 100 ,
936
+ max_depth = 1 ,
937
+ subsample = 0.5 ,
938
+ random_state = 1 ,
939
+ warm_start = True ,
940
+ )
941
+ est_sparse .fit (X_sparse , y )
942
+ est_sparse .predict (X )
943
+ est_sparse .set_params (n_estimators = 200 )
944
+ est_sparse .fit (X_sparse , y )
945
+ y_pred_sparse = est_sparse .predict (X )
944
946
945
- assert_array_almost_equal (
946
- est_dense .oob_improvement_ [:100 ], est_sparse .oob_improvement_ [:100 ]
947
- )
948
- assert est_dense .oob_scores_ [- 1 ] == pytest .approx (est_dense .oob_score_ )
949
- assert_array_almost_equal (
950
- est_dense .oob_scores_ [:100 ], est_sparse .oob_scores_ [:100 ]
951
- )
952
- assert est_sparse .oob_scores_ [- 1 ] == pytest .approx (est_sparse .oob_score_ )
953
- assert_array_almost_equal (y_pred_dense , y_pred_sparse )
947
+ assert_array_almost_equal (
948
+ est_dense .oob_improvement_ [:100 ], est_sparse .oob_improvement_ [:100 ]
949
+ )
950
+ assert est_dense .oob_scores_ [- 1 ] == pytest .approx (est_dense .oob_score_ )
951
+ assert_array_almost_equal (est_dense .oob_scores_ [:100 ], est_sparse .oob_scores_ [:100 ])
952
+ assert est_sparse .oob_scores_ [- 1 ] == pytest .approx (est_sparse .oob_score_ )
953
+ assert_array_almost_equal (y_pred_dense , y_pred_sparse )
954
954
955
955
956
956
@pytest .mark .parametrize ("Cls" , GRADIENT_BOOSTING_ESTIMATORS )
@@ -1173,13 +1173,15 @@ def test_non_uniform_weights_toy_edge_case_clf():
1173
1173
@pytest .mark .parametrize (
1174
1174
"EstimatorClass" , (GradientBoostingClassifier , GradientBoostingRegressor )
1175
1175
)
1176
-@pytest .mark .parametrize ("sparse_matrix" , (csr_matrix , csc_matrix , coo_matrix ))
1177
- def test_sparse_input (EstimatorClass , sparse_matrix ):
1176
+ @pytest .mark .parametrize (
1177
+ "sparse_container" , COO_CONTAINERS + CSC_CONTAINERS + CSR_CONTAINERS
1178
+ )
1179
+ def test_sparse_input (EstimatorClass , sparse_container ):
1178
1180
y , X = datasets .make_multilabel_classification (
1179
1181
random_state = 0 , n_samples = 50 , n_features = 1 , n_classes = 20
1180
1182
)
1181
1183
y = y [:, 0 ]
1182
- X_sparse = sparse_matrix (X )
1184
+ X_sparse = sparse_container (X )
1183
1185
1184
1186
dense = EstimatorClass (
1185
1187
n_estimators = 10 , random_state = 0 , max_depth = 2 , min_impurity_decrease = 1e-7
0 commit comments