2012-03-04 07:43:36 +08:00
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
Comparing different clustering algorithms on toy datasets
|
|
|
|
|
=========================================================
|
|
|
|
|
|
|
|
|
|
This example aims at showing characteristics of different
|
|
|
|
|
clustering algorithms on datasets that are "interesting"
|
2012-03-26 01:13:16 +08:00
|
|
|
but still in 2D. The last dataset is an example of a 'null'
|
|
|
|
|
situation for clustering: the data is homogeneous, and
|
|
|
|
|
there is no good clustering.
|
2012-03-04 07:43:36 +08:00
|
|
|
|
|
|
|
|
While these examples give some intuition about the algorithms,
|
|
|
|
|
this intuition might not apply to very high dimensional data.
|
2012-03-26 01:13:16 +08:00
|
|
|
|
|
|
|
|
The results could be improved by tweaking the parameters for
|
|
|
|
|
each clustering strategy, for instance setting the number of
|
|
|
|
|
clusters for the methods that needs this parameter
|
|
|
|
|
specified. Note that affinity propagation has a tendency to
|
|
|
|
|
create many clusters. Thus in this example its two parameters
|
|
|
|
|
(damping and per-point preference) were set to to mitigate this
|
|
|
|
|
behavior.
|
2012-03-04 07:43:36 +08:00
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
2012-03-27 17:01:51 +08:00
|
|
|
import time
|
|
|
|
|
|
2012-03-04 07:43:36 +08:00
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
|
|
|
|
|
2012-03-26 05:43:00 +08:00
|
|
|
from sklearn import cluster, datasets
|
2012-03-26 01:13:16 +08:00
|
|
|
from sklearn.metrics import euclidean_distances
|
2012-03-04 21:30:56 +08:00
|
|
|
from sklearn.neighbors import kneighbors_graph
|
2012-09-23 19:58:12 +08:00
|
|
|
from sklearn.preprocessing import StandardScaler
|
2012-03-04 07:43:36 +08:00
|
|
|
|
2012-03-26 01:13:16 +08:00
|
|
|
np.random.seed(0)
|
|
|
|
|
|
2012-03-27 17:01:51 +08:00
|
|
|
# Generate datasets. We choose the size big enough to see the scalability
|
|
|
|
|
# of the algorithms, but not too big to avoid too long running times
|
|
|
|
|
n_samples = 1500
|
2012-03-26 05:43:00 +08:00
|
|
|
noisy_circles = datasets.make_circles(n_samples=n_samples, factor=.5,
|
|
|
|
|
noise=.05)
|
|
|
|
|
noisy_moons = datasets.make_moons(n_samples=n_samples, noise=.05)
|
|
|
|
|
blobs = datasets.make_blobs(n_samples=n_samples, random_state=8)
|
2012-03-26 01:13:16 +08:00
|
|
|
no_structure = np.random.rand(n_samples, 2), None
|
2012-03-04 07:43:36 +08:00
|
|
|
|
|
|
|
|
colors = np.array([x for x in 'bgrcmykbgrcmykbgrcmykbgrcmyk'])
|
2012-03-26 01:13:16 +08:00
|
|
|
colors = np.hstack([colors] * 20)
|
2012-03-04 07:43:36 +08:00
|
|
|
|
2012-03-26 05:43:00 +08:00
|
|
|
pl.figure(figsize=(14, 9.5))
|
|
|
|
|
pl.subplots_adjust(left=.001, right=.999, bottom=.001, top=.96, wspace=.05,
|
2012-11-21 00:31:06 +08:00
|
|
|
hspace=.01)
|
2012-03-05 00:51:38 +08:00
|
|
|
|
2012-03-05 00:46:38 +08:00
|
|
|
plot_num = 1
|
2012-03-26 01:13:16 +08:00
|
|
|
for i_dataset, dataset in enumerate([noisy_circles, noisy_moons, blobs,
|
2012-11-21 00:31:06 +08:00
|
|
|
no_structure]):
|
2012-03-04 07:43:36 +08:00
|
|
|
X, y = dataset
|
2012-03-05 00:46:38 +08:00
|
|
|
# normalize dataset for easier parameter selection
|
2012-09-23 19:58:12 +08:00
|
|
|
X = StandardScaler().fit_transform(X)
|
2012-03-05 00:46:38 +08:00
|
|
|
|
2012-03-04 07:43:36 +08:00
|
|
|
# estimate bandwidth for mean shift
|
2012-03-26 05:43:00 +08:00
|
|
|
bandwidth = cluster.estimate_bandwidth(X, quantile=0.3)
|
2012-03-04 07:43:36 +08:00
|
|
|
|
2012-03-05 00:46:38 +08:00
|
|
|
# connectivity matrix for structured Ward
|
2012-03-25 23:33:49 +08:00
|
|
|
connectivity = kneighbors_graph(X, n_neighbors=10)
|
2012-03-05 00:51:38 +08:00
|
|
|
# make connectivity symmetric
|
|
|
|
|
connectivity = 0.5 * (connectivity + connectivity.T)
|
2012-03-04 21:30:56 +08:00
|
|
|
|
2012-03-26 01:13:16 +08:00
|
|
|
# Compute distances
|
2012-04-28 20:04:39 +08:00
|
|
|
#distances = np.exp(-euclidean_distances(X))
|
2012-03-26 01:13:16 +08:00
|
|
|
distances = euclidean_distances(X)
|
|
|
|
|
|
2012-03-04 07:43:36 +08:00
|
|
|
# create clustering estimators
|
2012-03-26 05:43:00 +08:00
|
|
|
ms = cluster.MeanShift(bandwidth=bandwidth, bin_seeding=True)
|
2012-05-16 06:09:47 +08:00
|
|
|
two_means = cluster.MiniBatchKMeans(n_clusters=2)
|
2012-03-26 05:43:00 +08:00
|
|
|
ward_five = cluster.Ward(n_clusters=2, connectivity=connectivity)
|
2012-11-21 00:31:06 +08:00
|
|
|
spectral = cluster.SpectralClustering(n_clusters=2,
|
2012-11-22 21:23:59 +08:00
|
|
|
eigen_solver='arpack',
|
2012-11-21 00:31:06 +08:00
|
|
|
affinity="nearest_neighbors")
|
2012-03-27 17:01:51 +08:00
|
|
|
dbscan = cluster.DBSCAN(eps=.2)
|
2012-08-27 15:35:14 +08:00
|
|
|
affinity_propagation = cluster.AffinityPropagation(damping=.9,
|
2012-11-21 00:31:06 +08:00
|
|
|
preference=-200)
|
2012-03-04 07:43:36 +08:00
|
|
|
|
2012-03-26 02:38:06 +08:00
|
|
|
for algorithm in [two_means, affinity_propagation, ms, spectral,
|
|
|
|
|
ward_five, dbscan]:
|
2012-03-04 07:43:36 +08:00
|
|
|
# predict cluster memberships
|
2012-03-27 17:01:51 +08:00
|
|
|
t0 = time.time()
|
2012-04-28 20:04:39 +08:00
|
|
|
algorithm.fit(X)
|
2012-03-27 17:01:51 +08:00
|
|
|
t1 = time.time()
|
2012-03-26 05:43:00 +08:00
|
|
|
if hasattr(algorithm, 'labels_'):
|
|
|
|
|
y_pred = algorithm.labels_.astype(np.int)
|
|
|
|
|
else:
|
|
|
|
|
y_pred = algorithm.predict(X)
|
2012-03-05 00:51:38 +08:00
|
|
|
|
|
|
|
|
# plot
|
2012-03-26 01:13:16 +08:00
|
|
|
pl.subplot(4, 6, plot_num)
|
2012-03-05 00:46:38 +08:00
|
|
|
if i_dataset == 0:
|
2012-03-26 07:59:15 +08:00
|
|
|
pl.title(str(algorithm).split('(')[0], size=18)
|
2012-03-27 17:06:27 +08:00
|
|
|
pl.scatter(X[:, 0], X[:, 1], color=colors[y_pred].tolist(), s=10)
|
2012-03-05 02:53:38 +08:00
|
|
|
|
|
|
|
|
if hasattr(algorithm, 'cluster_centers_'):
|
|
|
|
|
centers = algorithm.cluster_centers_
|
|
|
|
|
center_colors = colors[:len(centers)]
|
2012-03-27 17:06:27 +08:00
|
|
|
pl.scatter(centers[:, 0], centers[:, 1], s=100, c=center_colors)
|
2012-03-05 02:34:25 +08:00
|
|
|
pl.xlim(-2, 2)
|
|
|
|
|
pl.ylim(-2, 2)
|
2012-03-04 07:43:36 +08:00
|
|
|
pl.xticks(())
|
|
|
|
|
pl.yticks(())
|
2012-03-27 17:01:51 +08:00
|
|
|
pl.text(.99, .01, ('%.2fs' % (t1 - t0)).lstrip('0'),
|
|
|
|
|
transform=pl.gca().transAxes, size=15,
|
|
|
|
|
horizontalalignment='right')
|
2012-03-05 00:46:38 +08:00
|
|
|
plot_num += 1
|
2012-03-04 07:43:36 +08:00
|
|
|
|
|
|
|
|
pl.show()
|