scikit-learn/examples/cluster/plot_ward_structured_vs_uns...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.5 KiB
Python
Raw Normal View History

2011-04-03 02:57:38 +08:00
"""
===========================================================
Hierarchical clustering: structured vs unstructured ward
===========================================================
Example builds a swiss roll dataset and runs
hierarchical clustering on their position.
2011-04-03 02:57:38 +08:00
For more information, see :ref:`hierarchical_clustering`.
In a first step, the hierarchical clustering is performed without connectivity
constraints on the structure and is solely based on distance, whereas in
a second step the clustering is restricted to the k-Nearest Neighbors
graph: it's a hierarchical clustering with structure prior.
2011-04-03 02:57:38 +08:00
Some of the clusters learned without connectivity constraints do not
2011-05-04 23:49:17 +08:00
respect the structure of the swiss roll and extend across different folds of
2011-04-03 02:57:38 +08:00
the manifolds. On the opposite, when opposing connectivity constraints,
the clusters form a nice parcellation of the swiss roll.
2011-04-03 02:57:38 +08:00
"""
# Authors : Vincent Michel, 2010
# Alexandre Gramfort, 2010
# Gael Varoquaux, 2010
# License: BSD 3 clause
2011-04-03 02:57:38 +08:00
import time as time
2022-03-02 17:24:02 +08:00
import matplotlib.pyplot as plt
2022-03-02 17:24:02 +08:00
# The following import is required
# for 3D projection to work with matplotlib < 3.2
import mpl_toolkits.mplot3d # noqa: F401
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_swiss_roll
2011-04-03 02:57:38 +08:00
# #############################################################################
2011-04-03 02:57:38 +08:00
# Generate data (swiss roll dataset)
n_samples = 1500
2011-04-03 02:57:38 +08:00
noise = 0.05
X, _ = make_swiss_roll(n_samples, noise=noise)
2011-04-03 02:57:38 +08:00
# Make it thinner
X[:, 1] *= 0.5
# #############################################################################
2011-04-03 02:57:38 +08:00
# Compute clustering
print("Compute unstructured hierarchical clustering...")
2011-04-03 02:57:38 +08:00
st = time.time()
ward = AgglomerativeClustering(n_clusters=6, linkage="ward").fit(X)
elapsed_time = time.time() - st
2011-04-03 02:57:38 +08:00
label = ward.labels_
print("Elapsed time: %.2fs" % elapsed_time)
print("Number of points: %i" % label.size)
2011-04-03 02:57:38 +08:00
# #############################################################################
2011-04-03 02:57:38 +08:00
# Plot result
fig = plt.figure()
2022-03-02 17:24:02 +08:00
ax = fig.add_subplot(111, projection="3d", elev=7, azim=-80)
ax.set_position([0, 0, 0.95, 1])
2011-04-03 02:57:38 +08:00
for l in np.unique(label):
ax.scatter(
X[label == l, 0],
X[label == l, 1],
X[label == l, 2],
color=plt.cm.jet(float(l) / np.max(label + 1)),
s=20,
edgecolor="k",
)
plt.title("Without connectivity constraints (time %.2fs)" % elapsed_time)
2011-04-03 02:57:38 +08:00
# #############################################################################
2011-04-03 02:57:38 +08:00
# Define the structure A of the data. Here a 10 nearest neighbors
from sklearn.neighbors import kneighbors_graph
connectivity = kneighbors_graph(X, n_neighbors=10, include_self=False)
2011-04-03 02:57:38 +08:00
# #############################################################################
2011-04-03 02:57:38 +08:00
# Compute clustering
print("Compute structured hierarchical clustering...")
2011-04-03 02:57:38 +08:00
st = time.time()
ward = AgglomerativeClustering(
n_clusters=6, connectivity=connectivity, linkage="ward"
).fit(X)
elapsed_time = time.time() - st
2011-04-03 02:57:38 +08:00
label = ward.labels_
print("Elapsed time: %.2fs" % elapsed_time)
print("Number of points: %i" % label.size)
2011-04-03 02:57:38 +08:00
# #############################################################################
2011-04-03 02:57:38 +08:00
# Plot result
fig = plt.figure()
2022-03-02 17:24:02 +08:00
ax = fig.add_subplot(111, projection="3d", elev=7, azim=-80)
ax.set_position([0, 0, 0.95, 1])
2011-04-03 02:57:38 +08:00
for l in np.unique(label):
ax.scatter(
X[label == l, 0],
X[label == l, 1],
X[label == l, 2],
color=plt.cm.jet(float(l) / np.max(label + 1)),
s=20,
edgecolor="k",
)
plt.title("With connectivity constraints (time %.2fs)" % elapsed_time)
2011-04-03 02:57:38 +08:00
plt.show()