You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
K-Means: Should subtract X_means from initial centroids iff it's also subtracted from X #6740
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.
Steps/Code to Reproduce
importnumpyasnpimportscipyfromsklearn.clusterimportKMeansfromsklearnimportdatasetsiris=datasets.load_iris()
X=iris.data# Get a local optimumcenters=KMeans(n_clusters=3).fit(X).cluster_centers_# Fit starting from a local optimum shouldn't change the solutionnp.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 sparseX_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_
)
changed the title [-]K-Means: Should subtract `X_means` from initial centroids iff it's also subtracted from `X`[/-][+]K-Means: Should subtract X_means from initial centroids iff it's also subtracted from X[/+]on May 2, 2016
I think that the center being re-ordered in the previous example is just the symptom of the problem. Since the iris data is low-dimensional, close to (0,0), and we provided the correct n_clusters, it's likely that even the provided init centers are not handled correctly we will converge to the same result anyways.
The root problem is that, when X is sparse, the provided init centers are effectively discarded and replaced with init - X_mean, which are potentially really bad init centers.
To better demonstrate this:
importnumpyasnpimportscipyfromsklearn.clusterimportKMeansfromsklearnimportdatasetsiris=datasets.load_iris()
X=iris.data# Get a local optimum# Here we change from the previous example to n_clusters=4, n_init=1centers=KMeans(n_clusters=4, n_init=1, init='random', random_state=1).fit(X).cluster_centers_# Fit starting from a local optimum shouldn't change the solutionnp.testing.assert_allclose(
centers,
KMeans(n_clusters=4, init=centers, n_init=1).fit(X).cluster_centers_
)
# The same should be true when X is sparse, but wasn't before the bug fixX_sparse=scipy.sparse.csr_matrix(X)
np.testing.assert_allclose(
centers,
KMeans(n_clusters=4, init=centers, n_init=1).fit(X_sparse).cluster_centers_
)
And in the assert error, you can see that the centers are not just reordered but very different.
Activity
[-]K-Means: Should subtract `X_means` from initial centroids iff it's also subtracted from `X`[/-][+]K-Means: Should subtract X_means from initial centroids iff it's also subtracted from X[/+]yupbank commentedon May 4, 2016
I think the problem is because, the order of cluster center changed
you can try to sort the n_center first and then the test would pass.
tomtung commentedon May 4, 2016
I think that the center being re-ordered in the previous example is just the symptom of the problem. Since the iris data is low-dimensional, close to
(0,0), and we provided the correctn_clusters, it's likely that even the provided init centers are not handled correctly we will converge to the same result anyways.The root problem is that, when
Xis sparse, the provided init centers are effectively discarded and replaced withinit - X_mean, which are potentially really bad init centers.To better demonstrate this:
And in the assert error, you can see that the centers are not just reordered but very different.
jkarno commentedon Nov 3, 2016
@amueller I can work on this. Is there anything else that needs to be done besides adding the test case to the current PR?