8
8
9
9
from sklearn .base import clone
10
10
11
+ from sklearn .preprocessing import maxabs_scale
11
12
from sklearn .preprocessing import minmax_scale
12
13
from sklearn .preprocessing import scale
13
14
from sklearn .preprocessing import power_transform
14
15
from sklearn .preprocessing import quantile_transform
15
16
17
+ from sklearn .preprocessing import MaxAbsScaler
16
18
from sklearn .preprocessing import MinMaxScaler
17
19
from sklearn .preprocessing import StandardScaler
18
20
from sklearn .preprocessing import PowerTransformer
@@ -31,7 +33,8 @@ def _get_valid_samples_by_column(X, col):
31
33
32
34
@pytest .mark .parametrize (
33
35
"est, func, support_sparse, strictly_positive" ,
34
- [(MinMaxScaler (), minmax_scale , False , False ),
36
+ [(MaxAbsScaler (), maxabs_scale , True , False ),
37
+ (MinMaxScaler (), minmax_scale , False , False ),
35
38
(StandardScaler (), scale , False , False ),
36
39
(StandardScaler (with_mean = False ), scale , True , False ),
37
40
(PowerTransformer (), power_transform , False , True ),
@@ -53,12 +56,17 @@ def test_missing_value_handling(est, func, support_sparse, strictly_positive):
53
56
assert np .any (np .isnan (X_test ), axis = 0 ).all ()
54
57
X_test [:, 0 ] = np .nan # make sure this boundary case is tested
55
58
56
- Xt = est .fit (X_train ).transform (X_test )
59
+ with pytest .warns (None ) as records :
60
+ Xt = est .fit (X_train ).transform (X_test )
61
+ # ensure no warnings are raised
62
+ assert len (records ) == 0
57
63
# missing values should still be missing, and only them
58
64
assert_array_equal (np .isnan (Xt ), np .isnan (X_test ))
59
65
60
66
# check that the function leads to the same results as the class
61
- Xt_class = est .transform (X_train )
67
+ with pytest .warns (None ) as records :
68
+ Xt_class = est .transform (X_train )
69
+ assert len (records ) == 0
62
70
Xt_func = func (X_train , ** est .get_params ())
63
71
assert_array_equal (np .isnan (Xt_func ), np .isnan (Xt_class ))
64
72
assert_allclose (Xt_func [~ np .isnan (Xt_func )], Xt_class [~ np .isnan (Xt_class )])
@@ -74,7 +82,9 @@ def test_missing_value_handling(est, func, support_sparse, strictly_positive):
74
82
# train only on non-NaN
75
83
est .fit (_get_valid_samples_by_column (X_train , i ))
76
84
# check transforming with NaN works even when training without NaN
77
- Xt_col = est .transform (X_test [:, [i ]])
85
+ with pytest .warns (None ) as records :
86
+ Xt_col = est .transform (X_test [:, [i ]])
87
+ assert len (records ) == 0
78
88
assert_allclose (Xt_col , Xt [:, [i ]])
79
89
# check non-NaN is handled as before - the 1st column is all nan
80
90
if not np .isnan (X_test [:, i ]).all ():
@@ -87,15 +97,23 @@ def test_missing_value_handling(est, func, support_sparse, strictly_positive):
87
97
est_dense = clone (est )
88
98
est_sparse = clone (est )
89
99
90
- Xt_dense = est_dense .fit (X_train ).transform (X_test )
91
- Xt_inv_dense = est_dense .inverse_transform (Xt_dense )
100
+ with pytest .warns (None ) as records :
101
+ Xt_dense = est_dense .fit (X_train ).transform (X_test )
102
+ Xt_inv_dense = est_dense .inverse_transform (Xt_dense )
103
+ assert len (records ) == 0
92
104
for sparse_constructor in (sparse .csr_matrix , sparse .csc_matrix ,
93
105
sparse .bsr_matrix , sparse .coo_matrix ,
94
106
sparse .dia_matrix , sparse .dok_matrix ,
95
107
sparse .lil_matrix ):
96
108
# check that the dense and sparse inputs lead to the same results
97
- Xt_sparse = (est_sparse .fit (sparse_constructor (X_train ))
98
- .transform (sparse_constructor (X_test )))
99
- assert_allclose (Xt_sparse .A , Xt_dense )
100
- Xt_inv_sparse = est_sparse .inverse_transform (Xt_sparse )
101
- assert_allclose (Xt_inv_sparse .A , Xt_inv_dense )
109
+ # precompute the matrix to avoid catching side warnings
110
+ X_train_sp = sparse_constructor (X_train )
111
+ X_test_sp = sparse_constructor (X_test )
112
+ with pytest .warns (None ) as records :
113
+ Xt_sp = est_sparse .fit (X_train_sp ).transform (X_test_sp )
114
+ assert len (records ) == 0
115
+ assert_allclose (Xt_sp .A , Xt_dense )
116
+ with pytest .warns (None ) as records :
117
+ Xt_inv_sp = est_sparse .inverse_transform (Xt_sp )
118
+ assert len (records ) == 0
119
+ assert_allclose (Xt_inv_sp .A , Xt_inv_dense )
0 commit comments