@@ -248,20 +248,21 @@ def test_large_features(self, xp):
248
248
data [:x .shape [0 ]] = x
249
249
data [x .shape [0 ]:] = y
250
250
251
- kmeans (xp .asarray (data ), 2 )
251
+ # use `seed` to ensure backwards compatibility after SPEC7
252
+ kmeans (xp .asarray (data ), 2 , seed = 1 )
252
253
253
254
def test_kmeans_simple (self , xp ):
254
- np .random .seed (54321 )
255
+ rng = np .random .default_rng (54321 )
255
256
initc = np .concatenate ([[X [0 ]], [X [1 ]], [X [2 ]]])
256
- code1 = kmeans (xp .asarray (X ), xp .asarray (initc ), iter = 1 )[0 ]
257
+ code1 = kmeans (xp .asarray (X ), xp .asarray (initc ), iter = 1 , rng = rng )[0 ]
257
258
xp_assert_close (code1 , xp .asarray (CODET2 ))
258
259
259
260
@pytest .mark .skipif (SCIPY_ARRAY_API ,
260
261
reason = '`np.matrix` unsupported in array API mode' )
261
262
def test_kmeans_simple_matrix (self , xp ):
262
- np .random .seed (54321 )
263
+ rng = np .random .default_rng (54321 )
263
264
initc = np .concatenate ([[X [0 ]], [X [1 ]], [X [2 ]]])
264
- code1 = kmeans (matrix (X ), matrix (initc ), iter = 1 )[0 ]
265
+ code1 = kmeans (matrix (X ), matrix (initc ), iter = 1 , rng = rng )[0 ]
265
266
xp_assert_close (code1 , CODET2 )
266
267
267
268
def test_kmeans_lost_cluster (self , xp ):
@@ -281,23 +282,23 @@ def test_kmeans_lost_cluster(self, xp):
281
282
assert_raises (ClusterError , kmeans2 , data , initk , missing = 'raise' )
282
283
283
284
def test_kmeans2_simple (self , xp ):
284
- np .random .seed (12345678 )
285
+ rng = np .random .default_rng (12345678 )
285
286
initc = xp .asarray (np .concatenate ([[X [0 ]], [X [1 ]], [X [2 ]]]))
286
287
arrays = [xp .asarray ] if SCIPY_ARRAY_API else [np .asarray , matrix ]
287
288
for tp in arrays :
288
- code1 = kmeans2 (tp (X ), tp (initc ), iter = 1 )[0 ]
289
- code2 = kmeans2 (tp (X ), tp (initc ), iter = 2 )[0 ]
289
+ code1 = kmeans2 (tp (X ), tp (initc ), iter = 1 , rng = rng )[0 ]
290
+ code2 = kmeans2 (tp (X ), tp (initc ), iter = 2 , rng = rng )[0 ]
290
291
291
292
xp_assert_close (code1 , xp .asarray (CODET1 ))
292
293
xp_assert_close (code2 , xp .asarray (CODET2 ))
293
294
294
295
@pytest .mark .skipif (SCIPY_ARRAY_API ,
295
296
reason = '`np.matrix` unsupported in array API mode' )
296
297
def test_kmeans2_simple_matrix (self , xp ):
297
- np .random .seed (12345678 )
298
+ rng = np .random .default_rng (12345678 )
298
299
initc = xp .asarray (np .concatenate ([[X [0 ]], [X [1 ]], [X [2 ]]]))
299
- code1 = kmeans2 (matrix (X ), matrix (initc ), iter = 1 )[0 ]
300
- code2 = kmeans2 (matrix (X ), matrix (initc ), iter = 2 )[0 ]
300
+ code1 = kmeans2 (matrix (X ), matrix (initc ), iter = 1 , rng = rng )[0 ]
301
+ code2 = kmeans2 (matrix (X ), matrix (initc ), iter = 2 , rng = rng )[0 ]
301
302
302
303
xp_assert_close (code1 , CODET1 )
303
304
xp_assert_close (code2 , CODET2 )
@@ -308,7 +309,9 @@ def test_kmeans2_rank1(self, xp):
308
309
309
310
initc = data1 [:3 ]
310
311
code = xp_copy (initc , xp = xp )
311
- kmeans2 (data1 , code , iter = 1 )[0 ]
312
+
313
+ # use `seed` to ensure backwards compatibility after SPEC7
314
+ kmeans2 (data1 , code , iter = 1 , seed = 1 )[0 ]
312
315
kmeans2 (data1 , code , iter = 2 )[0 ]
313
316
314
317
def test_kmeans2_rank1_2 (self , xp ):
@@ -326,21 +329,21 @@ def test_kmeans2_high_dim(self, xp):
326
329
@skip_xp_backends ('jax.numpy' ,
327
330
reason = 'jax arrays do not support item assignment' )
328
331
def test_kmeans2_init (self , xp ):
329
- np .random .seed ( 12345 )
332
+ rng = np .random .default_rng ( 12345678 )
330
333
data = xp .asarray (TESTDATA_2D )
331
334
k = 3
332
335
333
- kmeans2 (data , k , minit = 'points' )
334
- kmeans2 (data [:, 1 ], k , minit = 'points' ) # special case (1-D)
336
+ kmeans2 (data , k , minit = 'points' , rng = rng )
337
+ kmeans2 (data [:, 1 ], k , minit = 'points' , rng = rng ) # special case (1-D)
335
338
336
- kmeans2 (data , k , minit = '++' )
337
- kmeans2 (data [:, 1 ], k , minit = '++' ) # special case (1-D)
339
+ kmeans2 (data , k , minit = '++' , rng = rng )
340
+ kmeans2 (data [:, 1 ], k , minit = '++' , rng = rng ) # special case (1-D)
338
341
339
342
# minit='random' can give warnings, filter those
340
343
with suppress_warnings () as sup :
341
344
sup .filter (message = "One of the clusters is empty. Re-run." )
342
- kmeans2 (data , k , minit = 'random' )
343
- kmeans2 (data [:, 1 ], k , minit = 'random' ) # special case (1-D)
345
+ kmeans2 (data , k , minit = 'random' , rng = rng )
346
+ kmeans2 (data [:, 1 ], k , minit = 'random' , rng = rng ) # special case (1-D)
344
347
345
348
@pytest .mark .skipif (sys .platform == 'win32' ,
346
349
reason = 'Fails with MemoryError in Wine.' )
@@ -377,28 +380,29 @@ def test_kmeans_large_thres(self, xp):
377
380
reason = 'jax arrays do not support item assignment' )
378
381
def test_kmeans2_kpp_low_dim (self , xp ):
379
382
# Regression test for gh-11462
383
+ rng = np .random .default_rng (2358792345678234568 )
380
384
prev_res = xp .asarray ([[- 1.95266667 , 0.898 ],
381
385
[- 3.153375 , 3.3945 ]], dtype = xp .float64 )
382
- np .random .seed (42 )
383
- res , _ = kmeans2 (xp .asarray (TESTDATA_2D ), 2 , minit = '++' )
386
+ res , _ = kmeans2 (xp .asarray (TESTDATA_2D ), 2 , minit = '++' , rng = rng )
384
387
xp_assert_close (res , prev_res )
385
388
386
389
@skip_xp_backends ('jax.numpy' ,
387
390
reason = 'jax arrays do not support item assignment' )
388
391
def test_kmeans2_kpp_high_dim (self , xp ):
389
392
# Regression test for gh-11462
393
+ rng = np .random .default_rng (23587923456834568 )
390
394
n_dim = 100
391
395
size = 10
392
396
centers = np .vstack ([5 * np .ones (n_dim ),
393
397
- 5 * np .ones (n_dim )])
394
- np . random . seed ( 42 )
398
+
395
399
data = np .vstack ([
396
- np . random .multivariate_normal (centers [0 ], np .eye (n_dim ), size = size ),
397
- np . random .multivariate_normal (centers [1 ], np .eye (n_dim ), size = size )
400
+ rng .multivariate_normal (centers [0 ], np .eye (n_dim ), size = size ),
401
+ rng .multivariate_normal (centers [1 ], np .eye (n_dim ), size = size )
398
402
])
399
403
400
404
data = xp .asarray (data )
401
- res , _ = kmeans2 (data , 2 , minit = '++' )
405
+ res , _ = kmeans2 (data , 2 , minit = '++' , rng = rng )
402
406
xp_assert_equal (xp .sign (res ), xp .sign (xp .asarray (centers )))
403
407
404
408
def test_kmeans_diff_convergence (self , xp ):
0 commit comments