Skip to content

test_k_means_fit_predict failing on some MacPython runs #12644

Closed

Description

@jnothman

KMeans fit_predict(X) != fit(X).predict(X) in several cases in

https://travis-ci.org/MacPython/scikit-learn-wheels/jobs/458223437
https://travis-ci.org/MacPython/scikit-learn-wheels/jobs/458223439

_________ test_k_means_fit_predict[0-2-1e-07-csr_matrix-float64-full] __________
algo = 'full', dtype = <class 'numpy.float64'>
constructor = <class 'scipy.sparse.csr.csr_matrix'>, seed = 0, max_iter = 2
tol = 1e-07
    @pytest.mark.parametrize('algo', ['full', 'elkan'])
    @pytest.mark.parametrize('dtype', [np.float32, np.float64])
    @pytest.mark.parametrize('constructor', [np.asarray, sp.csr_matrix])
    @pytest.mark.parametrize('seed, max_iter, tol', [
        (0, 2, 1e-7),    # strict non-convergence
        (1, 2, 1e-1),    # loose non-convergence
        (3, 300, 1e-7),  # strict convergence
        (4, 300, 1e-1),  # loose convergence
    ])
    def test_k_means_fit_predict(algo, dtype, constructor, seed, max_iter, tol):
        # check that fit.predict gives same result as fit_predict
        # There's a very small chance of failure with elkan on unstructured dataset
        # because predict method uses fast euclidean distances computation which
        # may cause small numerical instabilities.
        if not (algo == 'elkan' and constructor is sp.csr_matrix):
            rng = np.random.RandomState(seed)
    
            X = make_blobs(n_samples=1000, n_features=10, centers=10,
                           random_state=rng)[0].astype(dtype, copy=False)
            X = constructor(X)
    
            kmeans = KMeans(algorithm=algo, n_clusters=10, random_state=seed,
                            tol=tol, max_iter=max_iter, n_jobs=1)
    
            labels_1 = kmeans.fit(X).predict(X)
            labels_2 = kmeans.fit_predict(X)
    
>           assert_array_equal(labels_1, labels_2)
E           AssertionError: 
E           Arrays are not equal
E           
E           (mismatch 80.0%)
E            x: array([0, 5, 4, 1, 1, 2, 1, 2, 7, 4, 0, 3, 8, 9, 8, 3, 9, 4, 0, 5, 1, 1, 0,
E                  2, 5, 5, 9, 3, 2, 5, 7, 4, 1, 5, 0, 2, 0, 9, 1, 9, 4, 3, 1, 5, 4, 1,
E                  6, 3, 5, 9, 3, 9, 5, 4, 8, 2, 2, 0, 5, 7, 3, 7, 4, 9, 8, 6, 9, 0, 6,...
E            y: array([1, 0, 5, 2, 2, 3, 2, 3, 7, 5, 1, 4, 9, 8, 9, 4, 8, 5, 1, 0, 2, 2, 1,
E                  3, 0, 0, 8, 4, 3, 0, 7, 5, 2, 0, 1, 3, 1, 8, 2, 8, 5, 4, 2, 0, 5, 2,
E                  6, 4, 0, 8, 4, 8, 0, 5, 9, 3, 3, 1, 0, 7, 4, 7, 5, 8, 9, 6, 8, 1, 6,...
X          = <1000x10 sparse matrix of type '<class 'numpy.float64'>'
	with 10000 stored elements in Compressed Sparse Row format>
algo       = 'full'
constructor = <class 'scipy.sparse.csr.csr_matrix'>
dtype      = <class 'numpy.float64'>
kmeans     = KMeans(algorithm='full', copy_x=True, init='k-means++', max_iter=2,
    n_clusters=10, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=0, tol=1e-07, verbose=0)
labels_1   = array([0, 5, 4, 1, 1, 2, 1, 2, 7, 4, 0, 3, 8, 9, 8, 3, 9, 4, 0, 5, 1, 1, 0,
       2, 5, 5, 9, 3, 2, 5, 7, 4, 1, 5, 0,...3, 5, 1, 3, 3, 2, 3, 5, 4, 8, 8, 0, 8, 1, 7, 3, 6, 2, 2, 6, 3, 3,
       3, 3, 8, 3, 7, 9, 8, 9, 5, 4, 2], dtype=int32)
labels_2   = array([1, 0, 5, 2, 2, 3, 2, 3, 7, 5, 1, 4, 9, 8, 9, 4, 8, 5, 1, 0, 2, 2, 1,
       3, 0, 0, 8, 4, 3, 0, 7, 5, 2, 0, 1,...4, 0, 2, 4, 4, 3, 4, 0, 5, 9, 9, 1, 9, 2, 7, 4, 6, 3, 3, 6, 4, 4,
       4, 4, 9, 4, 7, 8, 9, 8, 0, 5, 3], dtype=int32)
max_iter   = 2
rng        = <mtrand.RandomState object at 0x114933ea0>
seed       = 0
tol        = 1e-07
../venv/lib/python3.6/site-packages/sklearn/cluster/tests/test_k_means.py:352: AssertionError
_________ test_k_means_fit_predict[4-300-0.1-csr_matrix-float64-full] __________
algo = 'full', dtype = <class 'numpy.float64'>
constructor = <class 'scipy.sparse.csr.csr_matrix'>, seed = 4, max_iter = 300
tol = 0.1
    @pytest.mark.parametrize('algo', ['full', 'elkan'])
    @pytest.mark.parametrize('dtype', [np.float32, np.float64])
    @pytest.mark.parametrize('constructor', [np.asarray, sp.csr_matrix])
    @pytest.mark.parametrize('seed, max_iter, tol', [
        (0, 2, 1e-7),    # strict non-convergence
        (1, 2, 1e-1),    # loose non-convergence
        (3, 300, 1e-7),  # strict convergence
        (4, 300, 1e-1),  # loose convergence
    ])
    def test_k_means_fit_predict(algo, dtype, constructor, seed, max_iter, tol):
        # check that fit.predict gives same result as fit_predict
        # There's a very small chance of failure with elkan on unstructured dataset
        # because predict method uses fast euclidean distances computation which
        # may cause small numerical instabilities.
        if not (algo == 'elkan' and constructor is sp.csr_matrix):
            rng = np.random.RandomState(seed)
    
            X = make_blobs(n_samples=1000, n_features=10, centers=10,
                           random_state=rng)[0].astype(dtype, copy=False)
            X = constructor(X)
    
            kmeans = KMeans(algorithm=algo, n_clusters=10, random_state=seed,
                            tol=tol, max_iter=max_iter, n_jobs=1)
    
            labels_1 = kmeans.fit(X).predict(X)
            labels_2 = kmeans.fit_predict(X)
    
>           assert_array_equal(labels_1, labels_2)
E           AssertionError: 
E           Arrays are not equal
E           
E           (mismatch 100.0%)
E            x: array([7, 0, 7, 8, 8, 7, 8, 9, 2, 3, 6, 8, 9, 6, 9, 5, 1, 7, 0, 3, 9, 0, 6,
E                  3, 5, 5, 6, 3, 6, 1, 4, 7, 0, 4, 0, 6, 4, 6, 0, 4, 4, 9, 6, 1, 2, 0,
E                  2, 5, 1, 4, 9, 1, 5, 3, 9, 6, 6, 9, 9, 8, 7, 1, 6, 2, 7, 0, 9, 1, 3,...
E            y: array([9, 2, 9, 0, 0, 9, 0, 1, 8, 7, 4, 0, 1, 4, 1, 6, 5, 9, 2, 7, 1, 2, 4,
E                  7, 6, 6, 4, 7, 4, 5, 3, 9, 2, 3, 2, 4, 3, 4, 2, 3, 3, 1, 4, 5, 8, 2,
E                  8, 6, 5, 3, 1, 5, 6, 7, 1, 4, 4, 1, 1, 0, 9, 5, 4, 8, 9, 2, 1, 5, 7,...
X          = <1000x10 sparse matrix of type '<class 'numpy.float64'>'
	with 10000 stored elements in Compressed Sparse Row format>
algo       = 'full'
constructor = <class 'scipy.sparse.csr.csr_matrix'>
dtype      = <class 'numpy.float64'>
kmeans     = KMeans(algorithm='full', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=10, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=4, tol=0.1, verbose=0)
labels_1   = array([7, 0, 7, 8, 8, 7, 8, 9, 2, 3, 6, 8, 9, 6, 9, 5, 1, 7, 0, 3, 9, 0, 6,
       3, 5, 5, 6, 3, 6, 1, 4, 7, 0, 4, 0,...0, 0, 8, 5, 8, 2, 4, 7, 3, 3, 6, 8, 5, 7, 1, 2, 7, 1, 4, 9, 9, 5,
       4, 2, 2, 7, 5, 9, 8, 4, 9, 0, 1], dtype=int32)
labels_2   = array([9, 2, 9, 0, 0, 9, 0, 1, 8, 7, 4, 0, 1, 4, 1, 6, 5, 9, 2, 7, 1, 2, 4,
       7, 6, 6, 4, 7, 4, 5, 3, 9, 2, 3, 2,...2, 2, 0, 6, 0, 8, 3, 9, 7, 7, 4, 0, 6, 9, 5, 8, 9, 5, 3, 1, 1, 6,
       3, 8, 8, 9, 6, 1, 0, 3, 1, 2, 5], dtype=int32)
max_iter   = 300
rng        = <mtrand.RandomState object at 0x1141c9708>
seed       = 4
tol        = 0.1
../venv/lib/python3.6/site-packages/sklearn/cluster/tests/test_k_means.py:352: AssertionError

Activity

  1. added this to the 0.20.1 milestone on Nov 22, 2018
  2. qinhanmin2014 commented on Nov 22, 2018

    @qinhanmin2014
    Member

    Use v_measure_score?

  3. jnothman commented on Nov 22, 2018

    @jnothman
    MemberAuthor

    Does this basically mean that there are some ties during fitting that are being broken non-deterministically??

  4. jnothman commented on Nov 22, 2018

    @jnothman
    MemberAuthor

    I'm okay using a score, if that's not breaking the intention of the test.

  5. jeremiedbb commented on Nov 22, 2018

    @jeremiedbb
    Member

    It appears that the labels are correct in both fit.predict and fit_predict, but somehow permuted, even with a fixed random_state.

    I think the issue comes from n_init. K_means is run 10 times on 10 different seeds. However the problem is relatively simple and among these 10 k_means, it may be that two or more gave the same best inertia, with permuted labels.

    Because they are run in parallel using joblib.Parallel, I guess the ordering in which they are run could change between fit.predict and fit_predict, and we can't control that. I don't know why this happens in python 2 and not in python 3...

    We can change the test to either use v_measure, or just set n_init to 1 (Notice that the issue will disappear when my kmeans PR is merged because we'll run the n_init sequentially, the parallelism being move to a lower level).

    Remark: The breakage of fit_predict vs fit.predict is not new at all. It's just that there was no test for that before :)

  6. ogrisel commented on Nov 22, 2018

    @ogrisel
    Member

    Because they are run in parallel using joblib.Parallel, I guess the ordering in which they are run could change between fit.predict and fit_predict, and we can't control that.

    I don't think this is the case: there is an explicit n_jobs=1.

    But it's worth trying to set n_init=1 to see if the problem is related to inits that yields exactly the same solution up to a permutation.

  7. qinhanmin2014 commented on Nov 22, 2018

    @qinhanmin2014
    Member

    I don't know why this happens in python 2 and not in python 3...

    I think it also happens in python3, see https://travis-ci.org/MacPython/scikit-learn-wheels/jobs/458223439

  8. ogrisel commented on Nov 22, 2018

    @ogrisel
    Member

    It only appears on macOS though.

    Here is a comment I posted on the gitter discussion w.r.t the release:

    """
    The kmeans.predict test failure is weird but it's not a regression: the test is stricter than it used to be. So I would be in favor of skipping this test in the 0.20.X branch for now and open an issue to understand what going on. To avoid this in the future we should run macOS tests on master. It used to be the case that travis macOS workers would tend to be saturated for a significant fraction of the time with a large backlog of pending jobs. However this does not seem to have been a significant problem over the last month: https://www.traviscistatus.com/#month Alternatively we could configure CI on azure pipelines that offers free CI for linux, macOS and windows with the same configuration file.
    """

    The issue is already open :)

  9. qinhanmin2014 commented on Nov 22, 2018

    @qinhanmin2014
    Member

    +1 to skip (maybe on specific platform?), though maybe I'd prefer things like v_measure_score (or n_init=1 if it works). I'll be surprised if the test still fails after using v_measure_score.

  10. ogrisel commented on Nov 22, 2018

    @ogrisel
    Member

    +1 to skip just on macOS.

  11. ogrisel commented on Nov 22, 2018

    @ogrisel
    Member

    It seems v_measure_score would also work but my point is not to delay the release for the correct resolution of this test.

  12. jeremiedbb commented on Nov 22, 2018

    @jeremiedbb
    Member

    I don't think this is the case: there is an explicit n_jobs=1.

    I missed that... Another confirmation it's not this is that the test only fail with algorithm="full" if I saw correctly.

    The difference between "full" and "elkan" which can cause this issue is that "full" uses the fast method to compute euclidean distances, the one with the precision issue. The inertia returned by the current implementation of k-means can strongly differ from the exact inertia. This can lead to different inertia being computed with only permutations of the labels.

  13. removed this from the 0.20.1 milestone on Nov 22, 2018
  14. 14 remaining items

  15. ogrisel commented on May 10, 2019

    @ogrisel
    Member

    I agree however that nightly build on travis should be able to detect all the failures we would observe on the release CI.

  16. modified the milestones: 0.21, 0.22, 0.21.3 on May 25, 2019
  17. modified the milestones: 0.21.3, 0.23 on Oct 31, 2019
  18. adrinjalali commented on Apr 21, 2020

    @adrinjalali
    Member

    removing from the milestone.

  19. modified the milestones: 0.23, 0.24 on Apr 21, 2020
  20. added
    module:test-suite
    on Feb 6, 2022
  21. jeremiedbb commented on Mar 22, 2022

    @jeremiedbb
    Member

    There's been improvements to the stability of the algorithm. The skip has been removed for a while now and we've not seen failures since then. Closing (definitively I hope :) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugmodule:clustermodule:test-suite

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions