10
10
11
11
import numpy as np
12
12
import pytest
13
- from scipy .sparse import coo_matrix , csr_matrix , issparse
13
+ from scipy .sparse import issparse
14
14
15
15
from sklearn .base import BaseEstimator , clone
16
16
from sklearn .cluster import KMeans
88
88
assert_array_almost_equal ,
89
89
assert_array_equal ,
90
90
)
91
+ from sklearn .utils .fixes import COO_CONTAINERS , CSR_CONTAINERS
91
92
from sklearn .utils .validation import _num_samples
92
93
93
94
@@ -244,11 +245,11 @@ def fit(
244
245
"MockClassifier extra fit_param sparse_param.shape "
245
246
"is ({0}, {1}), should be ({2}, {3})"
246
247
)
247
- assert sparse_param .shape == P_sparse .shape , fmt .format (
248
+ assert sparse_param .shape == P .shape , fmt .format (
248
249
sparse_param .shape [0 ],
249
250
sparse_param .shape [1 ],
250
- P_sparse .shape [0 ],
251
- P_sparse .shape [1 ],
251
+ P .shape [0 ],
252
+ P .shape [1 ],
252
253
)
253
254
return self
254
255
@@ -270,16 +271,17 @@ def get_params(self, deep=False):
270
271
# XXX: use 2D array, since 1D X is being detected as a single sample in
271
272
# check_consistent_length
272
273
X = np .ones ((10 , 2 ))
273
- X_sparse = coo_matrix (X )
274
274
y = np .array ([0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 ])
275
275
# The number of samples per class needs to be > n_splits,
276
276
# for StratifiedKFold(n_splits=3)
277
277
y2 = np .array ([1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 ])
278
- P_sparse = coo_matrix ( np .eye (5 ) )
278
+ P = np .eye (5 )
279
279
280
280
281
- def test_cross_val_score ():
281
+ @pytest .mark .parametrize ("coo_container" , COO_CONTAINERS )
282
+ def test_cross_val_score (coo_container ):
282
283
clf = MockClassifier ()
284
+ X_sparse = coo_container (X )
283
285
284
286
for a in range (- 10 , 10 ):
285
287
clf .a = a
@@ -399,7 +401,8 @@ def test_cross_validate_nested_estimator():
399
401
400
402
401
403
@pytest .mark .parametrize ("use_sparse" , [False , True ])
402
- def test_cross_validate (use_sparse : bool ):
404
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
405
+ def test_cross_validate (use_sparse : bool , csr_container ):
403
406
# Compute train and test mse/r2 scores
404
407
cv = KFold ()
405
408
@@ -412,8 +415,8 @@ def test_cross_validate(use_sparse: bool):
412
415
clf = SVC (kernel = "linear" , random_state = 0 )
413
416
414
417
if use_sparse :
415
- X_reg = csr_matrix (X_reg )
416
- X_clf = csr_matrix (X_clf )
418
+ X_reg = csr_container (X_reg )
419
+ X_clf = csr_container (X_clf )
417
420
418
421
for X , y , est in ((X_reg , y_reg , reg ), (X_clf , y_clf , clf )):
419
422
# It's okay to evaluate regression metrics on classification too
@@ -681,15 +684,16 @@ def test_cross_val_score_precomputed():
681
684
cross_val_score (svm , linear_kernel .tolist (), y )
682
685
683
686
684
- def test_cross_val_score_fit_params ():
687
+ @pytest .mark .parametrize ("coo_container" , COO_CONTAINERS )
688
+ def test_cross_val_score_fit_params (coo_container ):
685
689
clf = MockClassifier ()
686
690
n_samples = X .shape [0 ]
687
691
n_classes = len (np .unique (y ))
688
692
689
- W_sparse = coo_matrix (
693
+ W_sparse = coo_container (
690
694
(np .array ([1 ]), (np .array ([1 ]), np .array ([0 ]))), shape = (10 , 1 )
691
695
)
692
- P_sparse = coo_matrix (np .eye (5 ))
696
+ P_sparse = coo_container (np .eye (5 ))
693
697
694
698
DUMMY_INT = 42
695
699
DUMMY_STR = "42"
@@ -775,10 +779,11 @@ def test_cross_val_score_with_score_func_regression():
775
779
assert_array_almost_equal (ev_scores , [0.94 , 0.97 , 0.97 , 0.99 , 0.92 ], 2 )
776
780
777
781
778
- def test_permutation_score ():
782
+ @pytest .mark .parametrize ("coo_container" , COO_CONTAINERS )
783
+ def test_permutation_score (coo_container ):
779
784
iris = load_iris ()
780
785
X = iris .data
781
- X_sparse = coo_matrix (X )
786
+ X_sparse = coo_container (X )
782
787
y = iris .target
783
788
svm = SVC (kernel = "linear" )
784
789
cv = StratifiedKFold (2 )
@@ -914,7 +919,8 @@ def test_cross_val_score_multilabel():
914
919
assert_almost_equal (score_samples , [1 , 1 / 2 , 3 / 4 , 1 / 2 , 1 / 4 ])
915
920
916
921
917
- def test_cross_val_predict ():
922
+ @pytest .mark .parametrize ("coo_container" , COO_CONTAINERS )
923
+ def test_cross_val_predict (coo_container ):
918
924
X , y = load_diabetes (return_X_y = True )
919
925
cv = KFold ()
920
926
@@ -938,7 +944,7 @@ def test_cross_val_predict():
938
944
939
945
Xsp = X .copy ()
940
946
Xsp *= Xsp > np .median (Xsp )
941
- Xsp = coo_matrix (Xsp )
947
+ Xsp = coo_container (Xsp )
942
948
preds = cross_val_predict (est , Xsp , y )
943
949
assert_array_almost_equal (len (preds ), len (y ))
944
950
@@ -1054,10 +1060,11 @@ def test_cross_val_predict_predict_log_proba_shape():
1054
1060
assert preds .shape == (150 , 3 )
1055
1061
1056
1062
1057
- def test_cross_val_predict_input_types ():
1063
+ @pytest .mark .parametrize ("coo_container" , COO_CONTAINERS )
1064
+ def test_cross_val_predict_input_types (coo_container ):
1058
1065
iris = load_iris ()
1059
1066
X , y = iris .data , iris .target
1060
- X_sparse = coo_matrix (X )
1067
+ X_sparse = coo_container (X )
1061
1068
multioutput_y = np .column_stack ([y , y [::- 1 ]])
1062
1069
1063
1070
clf = Ridge (fit_intercept = False , random_state = 0 )
@@ -1162,11 +1169,12 @@ def test_cross_val_predict_y_none():
1162
1169
assert_allclose (X , y_hat_proba )
1163
1170
1164
1171
1165
- def test_cross_val_score_sparse_fit_params ():
1172
+ @pytest .mark .parametrize ("coo_container" , COO_CONTAINERS )
1173
+ def test_cross_val_score_sparse_fit_params (coo_container ):
1166
1174
iris = load_iris ()
1167
1175
X , y = iris .data , iris .target
1168
1176
clf = MockClassifier ()
1169
- fit_params = {"sparse_sample_weight" : coo_matrix (np .eye (X .shape [0 ]))}
1177
+ fit_params = {"sparse_sample_weight" : coo_container (np .eye (X .shape [0 ]))}
1170
1178
a = cross_val_score (clf , X , y , params = fit_params , cv = 3 )
1171
1179
assert_array_equal (a , np .ones (3 ))
1172
1180
@@ -1739,7 +1747,8 @@ def test_check_is_permutation():
1739
1747
assert not _check_is_permutation (np .hstack ((p , 0 )), 100 )
1740
1748
1741
1749
1742
- def test_cross_val_predict_sparse_prediction ():
1750
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
1751
+ def test_cross_val_predict_sparse_prediction (csr_container ):
1743
1752
# check that cross_val_predict gives same result for sparse and dense input
1744
1753
X , y = make_multilabel_classification (
1745
1754
n_classes = 2 ,
@@ -1748,8 +1757,8 @@ def test_cross_val_predict_sparse_prediction():
1748
1757
return_indicator = True ,
1749
1758
random_state = 1 ,
1750
1759
)
1751
- X_sparse = csr_matrix (X )
1752
- y_sparse = csr_matrix (y )
1760
+ X_sparse = csr_container (X )
1761
+ y_sparse = csr_container (y )
1753
1762
classif = OneVsRestClassifier (SVC (kernel = "linear" ))
1754
1763
preds = cross_val_predict (classif , X , y , cv = 10 )
1755
1764
preds_sparse = cross_val_predict (classif , X_sparse , y_sparse , cv = 10 )
0 commit comments