Repository navigation
Conversation
…cted from X
The bug happens when X is sparse and initial cluster centroids are
given. In this case the means of each of X's columns are computed and
subtracted from init for no reason.
To reproduce:
import numpy as np
import scipy
from sklearn.cluster import KMeans
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
'''Get a local optimum'''
centers = KMeans(n_clusters=3).fit(X).cluster_centers_
'''Fit starting from a local optimum shouldn't change the solution'''
np.testing.assert_allclose(
centers,
KMeans(n_clusters=3, init=centers, n_init=1).fit(X).cluster_centers_
)
'''The same should be true when X is sparse, but wasn't before the bug fix'''
X_sparse = scipy.sparse.csr_matrix(X)
np.testing.assert_allclose(
centers,
KMeans(n_clusters=3, init=centers, n_init=1).fit(X_sparse).cluster_centers_
)
Contributor
|
Hi, thanks for the fix. It looks like the bug is now fixed. Can you please add the above example as a regression test? Thanks. |
Member
You mean to say it will be fixed by this PR right? Besides +1 for the NRT like @maniteja123 suggested |
Contributor
|
@tomtung Have you done any update of this PR ? |
Member
|
are you still working on this? I'll put the "needs contributor" tag on this for now. |
Contributor
|
Seems like bug is fixed and the code need some care to merge it? I think I can handle this. |
Member
|
Thanks @kiote. It also needs a test. |
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issue
Fixes #6740
What does this implement/fix? Explain your changes.
Subtract X_means from initial centroids iff it's also subtracted from X
Any other comments?
The bug happens when X is sparse and initial cluster centroids are given. In this case the means of each of X's columns are computed and subtracted from init for no reason.
To reproduce: