33
33
assert_almost_equal ,
34
34
assert_array_equal ,
35
35
)
36
+ from sklearn .utils .fixes import CSR_CONTAINERS
36
37
37
38
38
39
class Trans (TransformerMixin , BaseEstimator ):
@@ -58,12 +59,15 @@ def transform(self, X):
58
59
59
60
60
61
class SparseMatrixTrans (BaseEstimator ):
62
+ def __init__ (self , csr_container ):
63
+ self .csr_container = csr_container
64
+
61
65
def fit (self , X , y = None ):
62
66
return self
63
67
64
68
def transform (self , X , y = None ):
65
69
n_samples = len (X )
66
- return sparse .eye (n_samples , n_samples ). tocsr ( )
70
+ return self . csr_container ( sparse .eye (n_samples , n_samples ))
67
71
68
72
69
73
class TransNo2D (BaseEstimator ):
@@ -431,14 +435,15 @@ def test_column_transformer_output_indices_df():
431
435
assert_array_equal (X_trans [:, []], X_trans [:, ct .output_indices_ ["remainder" ]])
432
436
433
437
434
- def test_column_transformer_sparse_array ():
435
- X_sparse = sparse .eye (3 , 2 ).tocsr ()
438
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
439
+ def test_column_transformer_sparse_array (csr_container ):
440
+ X_sparse = csr_container (sparse .eye (3 , 2 ))
436
441
437
442
# no distinction between 1D and 2D
438
- X_res_first = X_sparse [:, 0 ]
443
+ X_res_first = X_sparse [:, [ 0 ] ]
439
444
X_res_both = X_sparse
440
445
441
- for col in [0 , [0 ], slice (0 , 1 )]:
446
+ for col in [( 0 ,) , [0 ], slice (0 , 1 )]:
442
447
for remainder , res in [("drop" , X_res_first ), ("passthrough" , X_res_both )]:
443
448
ct = ColumnTransformer (
444
449
[("trans" , Trans (), col )], remainder = remainder , sparse_threshold = 0.8
@@ -474,10 +479,11 @@ def test_column_transformer_list():
474
479
assert_array_equal (ct .fit (X_list ).transform (X_list ), expected_result )
475
480
476
481
477
- def test_column_transformer_sparse_stacking ():
482
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
483
+ def test_column_transformer_sparse_stacking (csr_container ):
478
484
X_array = np .array ([[0 , 1 , 2 ], [2 , 4 , 6 ]]).T
479
485
col_trans = ColumnTransformer (
480
- [("trans1" , Trans (), [0 ]), ("trans2" , SparseMatrixTrans (), 1 )],
486
+ [("trans1" , Trans (), [0 ]), ("trans2" , SparseMatrixTrans (csr_container ), 1 )],
481
487
sparse_threshold = 0.8 ,
482
488
)
483
489
col_trans .fit (X_array )
@@ -489,7 +495,7 @@ def test_column_transformer_sparse_stacking():
489
495
assert col_trans .transformers_ [- 1 ][0 ] != "remainder"
490
496
491
497
col_trans = ColumnTransformer (
492
- [("trans1" , Trans (), [0 ]), ("trans2" , SparseMatrixTrans (), 1 )],
498
+ [("trans1" , Trans (), [0 ]), ("trans2" , SparseMatrixTrans (csr_container ), 1 )],
493
499
sparse_threshold = 0.1 ,
494
500
)
495
501
col_trans .fit (X_array )
@@ -999,11 +1005,14 @@ def test_column_transformer_drops_all_remainder_transformer():
999
1005
assert_array_equal (ct .transformers_ [- 1 ][2 ], [1 , 2 ])
1000
1006
1001
1007
1002
- def test_column_transformer_sparse_remainder_transformer ():
1008
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
1009
+ def test_column_transformer_sparse_remainder_transformer (csr_container ):
1003
1010
X_array = np .array ([[0 , 1 , 2 ], [2 , 4 , 6 ], [8 , 6 , 4 ]]).T
1004
1011
1005
1012
ct = ColumnTransformer (
1006
- [("trans1" , Trans (), [0 ])], remainder = SparseMatrixTrans (), sparse_threshold = 0.8
1013
+ [("trans1" , Trans (), [0 ])],
1014
+ remainder = SparseMatrixTrans (csr_container ),
1015
+ sparse_threshold = 0.8 ,
1007
1016
)
1008
1017
1009
1018
X_trans = ct .fit_transform (X_array )
@@ -1020,10 +1029,13 @@ def test_column_transformer_sparse_remainder_transformer():
1020
1029
assert_array_equal (ct .transformers_ [- 1 ][2 ], [1 , 2 ])
1021
1030
1022
1031
1023
- def test_column_transformer_drop_all_sparse_remainder_transformer ():
1032
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
1033
+ def test_column_transformer_drop_all_sparse_remainder_transformer (csr_container ):
1024
1034
X_array = np .array ([[0 , 1 , 2 ], [2 , 4 , 6 ], [8 , 6 , 4 ]]).T
1025
1035
ct = ColumnTransformer (
1026
- [("trans1" , "drop" , [0 ])], remainder = SparseMatrixTrans (), sparse_threshold = 0.8
1036
+ [("trans1" , "drop" , [0 ])],
1037
+ remainder = SparseMatrixTrans (csr_container ),
1038
+ sparse_threshold = 0.8 ,
1027
1039
)
1028
1040
1029
1041
X_trans = ct .fit_transform (X_array )
@@ -1231,7 +1243,7 @@ def test_column_transformer_negative_column_indexes():
1231
1243
assert_array_equal (tf_1 .fit_transform (X ), tf_2 .fit_transform (X ))
1232
1244
1233
1245
1234
- @pytest .mark .parametrize ("array_type" , [np .asarray , sparse . csr_matrix ])
1246
+ @pytest .mark .parametrize ("array_type" , [np .asarray , * CSR_CONTAINERS ])
1235
1247
def test_column_transformer_mask_indexing (array_type ):
1236
1248
# Regression test for #14510
1237
1249
# Boolean array-like does not behave as boolean array with sparse matrices.
0 commit comments