Skip to content

Commit ad758d2

Browse files
committed
FIX: Cluster centers after fir in KMeans and MBKMeans need not be ordered in the same way
1 parent c2cee91 commit ad758d2

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

examples/cluster/plot_mini_batch_kmeans.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
t0 = time.time()
4040
k_means.fit(X)
4141
t_batch = time.time() - t0
42-
k_means_labels = k_means.labels_
43-
k_means_cluster_centers = k_means.cluster_centers_
44-
k_means_labels_unique = np.unique(k_means_labels)
4542

4643
##############################################################################
4744
# Compute clustering with MiniBatchKMeans
@@ -51,9 +48,6 @@
5148
t0 = time.time()
5249
mbk.fit(X)
5350
t_mini_batch = time.time() - t0
54-
mbk_means_labels = mbk.labels_
55-
mbk_means_cluster_centers = mbk.cluster_centers_
56-
mbk_means_labels_unique = np.unique(mbk_means_labels)
5751

5852
##############################################################################
5953
# Plot result
@@ -65,7 +59,10 @@
6559
# We want to have the same colors for the same cluster from the
6660
# MiniBatchKMeans and the KMeans algorithm. Let's pair the cluster centers per
6761
# closest one.
68-
62+
k_means_cluster_centers = np.sort(k_means.cluster_centers_, axis=0)
63+
mbk_means_cluster_centers = np.sort(mbk.cluster_centers_, axis=0)
64+
k_means_labels = pairwise_distances_argmin(X, k_means_cluster_centers)
65+
mbk_means_labels = pairwise_distances_argmin(X, mbk_means_cluster_centers)
6966
order = pairwise_distances_argmin(k_means_cluster_centers,
7067
mbk_means_cluster_centers)
7168

0 commit comments

Comments
 (0)