2011-05-05 04:49:48 +08:00
|
|
|
"""
|
2013-01-08 17:45:13 +08:00
|
|
|
====================================================================
|
2013-03-26 03:36:08 +08:00
|
|
|
Comparison of the K-Means and MiniBatchKMeans clustering algorithms
|
2013-01-08 17:45:13 +08:00
|
|
|
====================================================================
|
2011-05-05 04:49:48 +08:00
|
|
|
|
2011-05-18 04:57:11 +08:00
|
|
|
We want to compare the performance of the MiniBatchKMeans and KMeans:
|
2011-05-19 10:18:40 +08:00
|
|
|
the MiniBatchKMeans is faster, but gives slightly different results (see
|
2011-05-18 04:57:11 +08:00
|
|
|
:ref:`mini_batch_kmeans`).
|
2011-05-15 19:03:24 +08:00
|
|
|
|
|
|
|
|
We will cluster a set of data, first with KMeans and then with
|
|
|
|
|
MiniBatchKMeans, and plot the results.
|
|
|
|
|
We will also plot the points that are labelled differently between the two
|
|
|
|
|
algorithms.
|
2011-05-05 04:49:48 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-05-05 04:49:48 +08:00
|
|
|
|
2011-05-18 03:50:16 +08:00
|
|
|
import time
|
|
|
|
|
|
2011-05-05 04:49:48 +08:00
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-05-18 03:50:16 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.cluster import MiniBatchKMeans, KMeans
|
2013-08-30 21:31:09 +08:00
|
|
|
from sklearn.metrics.pairwise import pairwise_distances_argmin
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.datasets.samples_generator import make_blobs
|
2011-05-05 04:49:48 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-05-05 04:49:48 +08:00
|
|
|
# Generate sample data
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
|
2011-05-15 19:03:24 +08:00
|
|
|
batch_size = 45
|
2011-05-19 10:18:40 +08:00
|
|
|
centers = [[1, 1], [-1, -1], [1, -1]]
|
|
|
|
|
n_clusters = len(centers)
|
2011-12-21 03:18:08 +08:00
|
|
|
X, labels_true = make_blobs(n_samples=3000, centers=centers, cluster_std=0.7)
|
2011-05-15 19:03:24 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-05-10 06:00:11 +08:00
|
|
|
# Compute clustering with Means
|
2011-05-18 08:16:38 +08:00
|
|
|
|
2012-05-16 16:52:52 +08:00
|
|
|
k_means = KMeans(init='k-means++', n_clusters=3, n_init=10)
|
2011-05-18 03:50:16 +08:00
|
|
|
t0 = time.time()
|
2011-05-10 06:00:11 +08:00
|
|
|
k_means.fit(X)
|
2011-05-18 03:50:16 +08:00
|
|
|
t_batch = time.time() - t0
|
2011-05-10 06:00:11 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-05-05 04:49:48 +08:00
|
|
|
# Compute clustering with MiniBatchKMeans
|
2011-05-15 19:03:24 +08:00
|
|
|
|
2012-05-16 16:52:52 +08:00
|
|
|
mbk = MiniBatchKMeans(init='k-means++', n_clusters=3, batch_size=batch_size,
|
2011-12-19 23:28:41 +08:00
|
|
|
n_init=10, max_no_improvement=10, verbose=0)
|
2011-05-18 03:50:16 +08:00
|
|
|
t0 = time.time()
|
2011-05-18 08:16:38 +08:00
|
|
|
mbk.fit(X)
|
2011-05-18 03:50:16 +08:00
|
|
|
t_mini_batch = time.time() - t0
|
2011-05-05 04:49:48 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-05-05 04:49:48 +08:00
|
|
|
# Plot result
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure(figsize=(8, 3))
|
2011-12-20 22:34:17 +08:00
|
|
|
fig.subplots_adjust(left=0.02, right=0.98, bottom=0.05, top=0.9)
|
2011-05-15 19:03:24 +08:00
|
|
|
colors = ['#4EACC5', '#FF9C34', '#4E9A06']
|
|
|
|
|
|
|
|
|
|
# We want to have the same colors for the same cluster from the
|
|
|
|
|
# MiniBatchKMeans and the KMeans algorithm. Let's pair the cluster centers per
|
|
|
|
|
# closest one.
|
2015-12-20 12:59:32 +08:00
|
|
|
k_means_cluster_centers = np.sort(k_means.cluster_centers_, axis=0)
|
|
|
|
|
mbk_means_cluster_centers = np.sort(mbk.cluster_centers_, axis=0)
|
|
|
|
|
k_means_labels = pairwise_distances_argmin(X, k_means_cluster_centers)
|
|
|
|
|
mbk_means_labels = pairwise_distances_argmin(X, mbk_means_cluster_centers)
|
2013-08-30 21:31:09 +08:00
|
|
|
order = pairwise_distances_argmin(k_means_cluster_centers,
|
|
|
|
|
mbk_means_cluster_centers)
|
2011-05-10 06:00:11 +08:00
|
|
|
|
|
|
|
|
# KMeans
|
|
|
|
|
ax = fig.add_subplot(1, 3, 1)
|
|
|
|
|
for k, col in zip(range(n_clusters), colors):
|
|
|
|
|
my_members = k_means_labels == k
|
|
|
|
|
cluster_center = k_means_cluster_centers[k]
|
2011-05-15 19:03:24 +08:00
|
|
|
ax.plot(X[my_members, 0], X[my_members, 1], 'w',
|
|
|
|
|
markerfacecolor=col, marker='.')
|
2011-05-10 06:00:11 +08:00
|
|
|
ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
|
2012-12-25 20:16:05 +08:00
|
|
|
markeredgecolor='k', markersize=6)
|
2011-05-18 02:52:38 +08:00
|
|
|
ax.set_title('KMeans')
|
2011-12-20 18:36:51 +08:00
|
|
|
ax.set_xticks(())
|
|
|
|
|
ax.set_yticks(())
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.text(-3.5, 1.8, 'train time: %.2fs\ninertia: %f' % (
|
2011-12-21 03:18:08 +08:00
|
|
|
t_batch, k_means.inertia_))
|
2011-05-05 04:49:48 +08:00
|
|
|
|
2011-05-10 06:00:11 +08:00
|
|
|
# MiniBatchKMeans
|
|
|
|
|
ax = fig.add_subplot(1, 3, 2)
|
|
|
|
|
for k, col in zip(range(n_clusters), colors):
|
2011-05-15 19:03:24 +08:00
|
|
|
my_members = mbk_means_labels == order[k]
|
|
|
|
|
cluster_center = mbk_means_cluster_centers[order[k]]
|
|
|
|
|
ax.plot(X[my_members, 0], X[my_members, 1], 'w',
|
|
|
|
|
markerfacecolor=col, marker='.')
|
2011-05-10 06:00:11 +08:00
|
|
|
ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
|
2012-12-25 20:16:05 +08:00
|
|
|
markeredgecolor='k', markersize=6)
|
2011-05-18 02:52:38 +08:00
|
|
|
ax.set_title('MiniBatchKMeans')
|
2011-12-20 18:36:51 +08:00
|
|
|
ax.set_xticks(())
|
|
|
|
|
ax.set_yticks(())
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.text(-3.5, 1.8, 'train time: %.2fs\ninertia: %f' %
|
2014-05-15 10:35:13 +08:00
|
|
|
(t_mini_batch, mbk.inertia_))
|
2011-05-10 06:00:11 +08:00
|
|
|
|
2011-05-15 19:03:24 +08:00
|
|
|
# Initialise the different array to all False
|
|
|
|
|
different = (mbk_means_labels == 4)
|
2011-05-10 06:00:11 +08:00
|
|
|
ax = fig.add_subplot(1, 3, 3)
|
|
|
|
|
|
2015-12-16 14:19:31 +08:00
|
|
|
for k in range(n_clusters):
|
2011-05-15 19:03:24 +08:00
|
|
|
different += ((k_means_labels == k) != (mbk_means_labels == order[k]))
|
|
|
|
|
|
|
|
|
|
identic = np.logical_not(different)
|
|
|
|
|
ax.plot(X[identic, 0], X[identic, 1], 'w',
|
|
|
|
|
markerfacecolor='#bbbbbb', marker='.')
|
|
|
|
|
ax.plot(X[different, 0], X[different, 1], 'w',
|
|
|
|
|
markerfacecolor='m', marker='.')
|
|
|
|
|
ax.set_title('Difference')
|
2011-12-20 18:36:51 +08:00
|
|
|
ax.set_xticks(())
|
|
|
|
|
ax.set_yticks(())
|
2011-05-10 06:00:11 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|