14
14
from sklearn .random_projection import SparseRandomProjection
15
15
from sklearn .random_projection import GaussianRandomProjection
16
16
17
- from sklearn .utils ._testing import assert_raises
18
- from sklearn .utils ._testing import assert_raise_message
19
17
from sklearn .utils ._testing import assert_array_equal
20
18
from sklearn .utils ._testing import assert_almost_equal
21
19
from sklearn .utils ._testing import assert_array_almost_equal
22
- from sklearn .utils ._testing import assert_warns
23
20
from sklearn .exceptions import DataDimensionalityWarning
24
21
25
22
all_sparse_random_matrix : List [Any ] = [_sparse_random_matrix ]
@@ -59,19 +56,21 @@ def densify(matrix):
59
56
###############################################################################
60
57
# test on JL lemma
61
58
###############################################################################
62
- def test_invalid_jl_domain ():
63
- assert_raises (ValueError , johnson_lindenstrauss_min_dim , 100 , eps = 1.1 )
64
- assert_raises (ValueError , johnson_lindenstrauss_min_dim , 100 , eps = 0.0 )
65
- assert_raises (ValueError , johnson_lindenstrauss_min_dim , 100 , eps = - 0.1 )
66
- assert_raises (ValueError , johnson_lindenstrauss_min_dim , 0 , eps = 0.5 )
67
59
60
+ @pytest .mark .parametrize ("n_samples, eps" , [
61
+ (100 , 1.1 ),
62
+ (100 , 0.0 ),
63
+ (100 , - 0.1 ),
64
+ (0 , 0.5 )
65
+ ])
66
+ def test_invalid_jl_domain (n_samples , eps ):
67
+ with pytest .raises (ValueError ):
68
+ johnson_lindenstrauss_min_dim (n_samples , eps = eps )
68
69
69
- def test_input_size_jl_min_dim ():
70
- assert_raises (ValueError , johnson_lindenstrauss_min_dim ,
71
- 3 * [100 ], eps = 2 * [0.9 ])
72
70
73
- assert_raises (ValueError , johnson_lindenstrauss_min_dim , 3 * [100 ],
74
- eps = 2 * [0.9 ])
71
+ def test_input_size_jl_min_dim ():
72
+ with pytest .raises (ValueError ):
73
+ johnson_lindenstrauss_min_dim (3 * [100 ], eps = 2 * [0.9 ])
75
74
76
75
johnson_lindenstrauss_min_dim (np .random .randint (1 , 10 , size = (10 , 10 )),
77
76
eps = np .full ((10 , 10 ), 0.5 ))
@@ -81,18 +80,17 @@ def test_input_size_jl_min_dim():
81
80
# tests random matrix generation
82
81
###############################################################################
83
82
def check_input_size_random_matrix (random_matrix ):
84
- assert_raises (ValueError , random_matrix , 0 , 0 )
85
- assert_raises (ValueError , random_matrix , - 1 , 1 )
86
- assert_raises (ValueError , random_matrix , 1 , - 1 )
87
- assert_raises (ValueError , random_matrix , 1 , 0 )
88
- assert_raises (ValueError , random_matrix , - 1 , 0 )
83
+ inputs = [(0 , 0 ), (- 1 , 1 ), (1 , - 1 ), (1 , 0 ), (- 1 , 0 )]
84
+ for n_components , n_features in inputs :
85
+ with pytest .raises (ValueError ):
86
+ random_matrix (n_components , n_features )
89
87
90
88
91
89
def check_size_generated (random_matrix ):
92
- assert random_matrix (1 , 5 ). shape == ( 1 , 5 )
93
- assert random_matrix ( 5 , 1 ). shape == ( 5 , 1 )
94
- assert random_matrix (5 , 5 ).shape == (5 , 5 )
95
- assert random_matrix ( 1 , 1 ). shape == ( 1 , 1 )
90
+ inputs = [ (1 , 5 ), ( 5 , 1 ), ( 5 , 5 ), ( 1 , 1 )]
91
+ for n_components , n_features in inputs :
92
+ assert random_matrix (n_components , n_features ).shape == (
93
+ n_components , n_features )
96
94
97
95
98
96
def check_zero_mean_and_unit_norm (random_matrix ):
@@ -109,8 +107,8 @@ def check_input_with_sparse_random_matrix(random_matrix):
109
107
n_components , n_features = 5 , 10
110
108
111
109
for density in [- 1. , 0.0 , 1.1 ]:
112
- assert_raises (ValueError ,
113
- random_matrix , n_components , n_features , density = density )
110
+ with pytest . raises (ValueError ):
111
+ random_matrix ( n_components , n_features , density = density )
114
112
115
113
116
114
@pytest .mark .parametrize ("random_matrix" , all_random_matrix )
@@ -153,9 +151,9 @@ def test_sparse_random_matrix():
153
151
s = 1 / density
154
152
155
153
A = _sparse_random_matrix (n_components ,
156
- n_features ,
157
- density = density ,
158
- random_state = 0 )
154
+ n_features ,
155
+ density = density ,
156
+ random_state = 0 )
159
157
A = densify (A )
160
158
161
159
# Check possible values
@@ -196,31 +194,27 @@ def test_sparse_random_matrix():
196
194
###############################################################################
197
195
# tests on random projection transformer
198
196
###############################################################################
199
- def test_sparse_random_projection_transformer_invalid_density ():
200
- for RandomProjection in all_SparseRandomProjection :
201
- assert_raises (ValueError ,
202
- RandomProjection (density = 1.1 ).fit , data )
203
197
204
- assert_raises ( ValueError ,
205
- RandomProjection (density = 0 ). fit , data )
206
-
207
- assert_raises (ValueError ,
208
- RandomProjection (density = - 0.1 ).fit , data )
198
+ @ pytest . mark . parametrize ( "density" , [ 1.1 , 0 , - 0.1 ])
199
+ def test_sparse_random_projection_transformer_invalid_density (density ):
200
+ for RandomProjection in all_SparseRandomProjection :
201
+ with pytest . raises (ValueError ):
202
+ RandomProjection (density = density ).fit ( data )
209
203
210
204
211
- def test_random_projection_transformer_invalid_input ():
205
+ @pytest .mark .parametrize ("n_components, fit_data" , [
206
+ ('auto' , [[0 , 1 , 2 ]]), (- 10 , data )]
207
+ )
208
+ def test_random_projection_transformer_invalid_input (n_components , fit_data ):
212
209
for RandomProjection in all_RandomProjection :
213
- assert_raises (ValueError ,
214
- RandomProjection (n_components = 'auto' ).fit , [[0 , 1 , 2 ]])
215
-
216
- assert_raises (ValueError ,
217
- RandomProjection (n_components = - 10 ).fit , data )
210
+ with pytest .raises (ValueError ):
211
+ RandomProjection (n_components = n_components ).fit (fit_data )
218
212
219
213
220
214
def test_try_to_transform_before_fit ():
221
215
for RandomProjection in all_RandomProjection :
222
- assert_raises (ValueError ,
223
- RandomProjection (n_components = 'auto' ).transform , data )
216
+ with pytest . raises (ValueError ):
217
+ RandomProjection (n_components = 'auto' ).transform ( data )
224
218
225
219
226
220
def test_too_many_samples_to_find_a_safe_embedding ():
@@ -232,7 +226,8 @@ def test_too_many_samples_to_find_a_safe_embedding():
232
226
'eps=0.100000 and n_samples=1000 lead to a target dimension'
233
227
' of 5920 which is larger than the original space with'
234
228
' n_features=100' )
235
- assert_raise_message (ValueError , expected_msg , rp .fit , data )
229
+ with pytest .raises (ValueError , match = expected_msg ):
230
+ rp .fit (data )
236
231
237
232
238
233
def test_random_projection_embedding_quality ():
@@ -318,7 +313,8 @@ def test_correct_RandomProjection_dimensions_embedding():
318
313
assert_array_equal (projected_1 , projected_3 )
319
314
320
315
# Try to transform with an input X of size different from fitted.
321
- assert_raises (ValueError , rp .transform , data [:, 1 :5 ])
316
+ with pytest .raises (ValueError ):
317
+ rp .transform (data [:, 1 :5 ])
322
318
323
319
# it is also possible to fix the number of components and the density
324
320
# level
@@ -337,8 +333,8 @@ def test_warning_n_components_greater_than_n_features():
337
333
data , _ = make_sparse_random_data (5 , n_features , int (n_features / 4 ))
338
334
339
335
for RandomProjection in all_RandomProjection :
340
- assert_warns (DataDimensionalityWarning ,
341
- RandomProjection (n_components = n_features + 1 ).fit , data )
336
+ with pytest . warns (DataDimensionalityWarning ):
337
+ RandomProjection (n_components = n_features + 1 ).fit ( data )
342
338
343
339
344
340
def test_works_with_sparse_data ():
0 commit comments