Skip to content

BUG/TST: cluster: incorrect test for seed param of {kmeans, kmeans2} #19683

@lucascolley

Description

@lucascolley

Describe your issue.

The test added in gh-13972 by @AtsushiSakai incorrectly compares res1 to itself rather than res2 (twice), so does not test the correctness of the seed parameter.

Once the test is corrected to compare res1 and res2, it actually fails due to the elements of the array being in different orders (see the CI of gh-19682).

Reproducing Code Example

def test_kmeans_and_kmeans2_random_seed(self):

        seed_list = [1234, np.random.RandomState(1234)]

        # check that np.random.Generator can be used (numpy >= 1.17)
        if hasattr(np.random, 'default_rng'):
            seed_list.append(np.random.default_rng(1234))

        for seed in seed_list:
            # test for kmeans
            res1, _ = kmeans(TESTDATA_2D, 2, seed=seed)
            res2, _ = kmeans(TESTDATA_2D, 2, seed=seed)
            assert_allclose(res1, res1)  # should be same results

            # test for kmeans2
            for minit in ["random", "points", "++"]:
                res1, _ = kmeans2(TESTDATA_2D, 2, minit=minit, seed=seed)
                res2, _ = kmeans2(TESTDATA_2D, 2, minit=minit, seed=seed)
                assert_allclose(res1, res1)  # should be same results

Error message (once corrected)

E   AssertionError: 
E   Not equal to tolerance rtol=1e-07, atol=0
E   
E   Mismatched elements: 4 / 4 (100%)
E   Max absolute difference: 2.5019998
E   Max relative difference: 3.09565287
E    x: array([[-3.063333,  3.31023 ],
E          [-1.947611,  0.80823 ]])
E    y: array([[-1.947611,  0.80823 ],
E          [-3.063333,  3.31023 ]])

SciPy/NumPy/Python version and system information

`scipy` @ `main`

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.cluster

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions