8000 TST use global_random_seed in sklearn/cluster/tests/test_hierarchical… · Anthony22-dev/scikit-learn@6ce482c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ce482c

Browse files
TST use global_random_seed in sklearn/cluster/tests/test_hierarchical.py (scikit-learn#25281)
1 parent 7d47190 commit 6ce482c

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

sklearn/cluster/tests/test_hierarchical.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def test_agglomerative_clustering_distances(
176176
assert not hasattr(clustering, "distances_")
177177

178178

179-
def test_agglomerative_clustering():
179+
def test_agglomerative_clustering(global_random_seed):
180180
# Check that we obtain the correct number of clusters with
181181
# agglomerative clustering.
182-
rng = np.random.RandomState(0)
182+
rng = np.random.RandomState(global_random_seed)
183183
mask = np.ones([10, 10], dtype=bool)
184184
n_samples = 100
185185
X = rng.randn(n_samples, 50)
@@ -280,9 +280,9 @@ def test_agglomerative_clustering_memory_mapped():
280280
AgglomerativeClustering(metric="euclidean", linkage="single").fit(Xmm)
281281

282282

283-
def test_ward_agglomeration():
283+
def test_ward_agglomeration(global_random_seed):
284284
# Check that we obtain the correct solution in a simplistic case
285-
rng = np.random.RandomState(0)
285+
rng = np.random.RandomState(global_random_seed)
286286
mask = np.ones([10, 10], dtype=bool)
287287
X = rng.randn(50, 100)
288288
connectivity = grid_to_graph(*mask.shape)
@@ -330,10 +330,10 @@ def assess_same_labelling(cut1, cut2):
330330
assert (co_clust[0] == co_clust[1]).all()
331331

332332

333-
def test_sparse_scikit_vs_scipy():
333+
def test_sparse_scikit_vs_scipy(global_random_seed):
334334
# Test scikit linkage with full connectivity (i.e. unstructured) vs scipy
335335
n, p, k = 10, 5, 3
336-
rng = np.random.RandomState(0)
336+
rng = np.random.RandomState(global_random_seed)
337337

338338
# Not using a lil_matrix here, just to check that non sparse
339339
# matrices are well handled
@@ -370,10 +370,9 @@ def test_sparse_scikit_vs_scipy():
370370

371371
# Make sure our custom mst_linkage_core gives
372372
# the same results as scipy's builtin
373-
@pytest.mark.parametrize("seed", range(5))
374-
def test_vector_scikit_single_vs_scipy_single(seed):
373+
def test_vector_scikit_single_vs_scipy_single(global_random_seed):
375374
n_samples, n_features, n_clusters = 10, 5, 3
376-
rng = np.random.RandomState(seed)
375+
rng = np.random.RandomState(global_random_seed)
377376
X = 0.1 * rng.normal(size=(n_samples, n_features))
378377
X -= 4.0 * np.arange(n_samples)[:, np.newaxis]
379378
X -= X.mean(axis=1)[:, np.newaxis]
@@ -468,13 +467,13 @@ def test_connectivity_propagation():
468467
ward.fit(X)
469468

470469

471-
def test_ward_tree_children_order():
470+
def test_ward_tree_children_order(global_random_seed):
472471
# Check that children are ordered in the same way for both structured and
473472
# unstructured versions of ward_tree.
474473

475474
# test on five random datasets
476475
n, p = 10, 5
477-
rng = np.random.RandomState(0)
476+
rng = np.random.RandomState(global_random_seed)
478477

479478
connectivity = np.ones((n, n))
480479
for i in range(5):
@@ -488,13 +487,13 @@ def test_ward_tree_children_order():
488487
assert_array_equal(out_unstructured[0], out_structured[0])
489488

490489

491-
def test_ward_linkage_tree_return_distance():
490+
def test_ward_linkage_tree_return_distance(global_random_seed):
492491
# Test return_distance option on linkage and ward trees
493492

494493
# test that return_distance when set true, gives same
495494
# output on both structured and unstructured clustering.
496495
n, p = 10, 5
497-
rng = np.random.RandomState(0)
496+
rng = np.random.RandomState(global_random_seed)
498497

499498
connectivity = np.ones((n, n))
500499
for i in range(5):
@@ -726,10 +725,10 @@ def increment(self, *args, **kwargs):
726725

727726

728727
@pytest.mark.parametrize("linkage", ["ward", "complete", "average"])
729-
def test_agglomerative_clustering_with_distance_threshold(linkage):
728+
def test_agglomerative_clustering_with_distance_threshold(linkage, global_random_seed):
730729
# Check that we obtain the correct number of clusters with
731730
# agglomerative clustering with distance_threshold.
732-
rng = np.random.RandomState(0)
731+
rng = np.random.RandomState(global_random_seed)
733732
mask = np.ones([10, 10], dtype=bool)
734733
n_samples = 100
735734
X = rng.randn(n_samples, 50)
@@ -764,8 +763,8 @@ def test_agglomerative_clustering_with_distance_threshold(linkage):
764763
assert np.array_equiv(clusters_produced, clusters_at_threshold)
765764

766765

767-
def test_small_distance_threshold():
768-
rng = np.random.RandomState(0)
766+
def test_small_distance_threshold(global_random_seed):
767+
rng = np.random.RandomState(global_random_seed)
769768
n_samples = 10
770769
X = rng.randint(-300, 300, size=(n_samples, 3))
771770
# this should result in all data in their own clusters, given that
@@ -781,8 +780,8 @@ def test_small_distance_threshold():
781780
assert clustering.n_clusters_ == n_samples
782781

783782

784-
def test_cluster_distances_with_distance_threshold():
785-
rng = np.random.RandomState(0)
783+
def test_cluster_distances_with_distance_threshold(global_random_seed):
784+
rng = np.random.RandomState(global_random_seed)
786785
n_samples = 100
787786
X = rng.randint(-10, 10, size=(n_samples, 3))
788787
# check the distances within the clusters and with other clusters

0 commit comments

Comments
 (0)
0