层次聚类:结构化与非结构化 Ward#
示例构建了一个瑞士卷数据集,并根据其位置运行层次聚类。
有关更多信息,请参阅 层次聚类。
第一步,在结构上没有任何连接约束的情况下执行层次聚类,并且仅基于距离,而在第二步中,聚类被限制在 k 最近邻图中:它是具有结构先验的层次聚类。
一些在没有连接约束的情况下学习到的簇不尊重瑞士卷的结构,并且跨越流形的不同折叠延伸。相反,当反对连接约束时,簇形成瑞士卷的良好分割。
# Authors : Vincent Michel, 2010
# Alexandre Gramfort, 2010
# Gael Varoquaux, 2010
# License: BSD 3 clause
import time as time
# 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.datasets import make_swiss_roll
n_samples = 1500
noise = 0.05
X, _ = make_swiss_roll(n_samples, noise=noise)
# Make it thinner
X[:, 1] *= 0.5
计算聚类#
我们执行没有任何连接约束的层次聚类下的 AgglomerativeClustering。
from sklearn.cluster import AgglomerativeClustering
print("Compute unstructured hierarchical clustering...")
st = time.time()
ward = AgglomerativeClustering(n_clusters=6, linkage="ward").fit(X)
elapsed_time = time.time() - st
label = ward.labels_
print(f"Elapsed time: {elapsed_time:.2f}s")
print(f"Number of points: {label.size}")
Compute unstructured hierarchical clustering...
Elapsed time: 0.04s
Number of points: 1500
绘制结果#
绘制非结构化层次聚类。
import matplotlib.pyplot as plt
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, projection="3d", elev=7, azim=-80)
ax1.set_position([0, 0, 0.95, 1])
for l in np.unique(label):
ax1.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",
)
_ = fig1.suptitle(f"Without connectivity constraints (time {elapsed_time:.2f}s)")
我们正在定义具有 10 个邻居的 k 最近邻#
from sklearn.neighbors import kneighbors_graph
connectivity = kneighbors_graph(X, n_neighbors=10, include_self=False)
计算聚类#
我们再次执行具有连接约束的 AgglomerativeClustering。
print("Compute structured hierarchical clustering...")
st = time.time()
ward = AgglomerativeClustering(
n_clusters=6, connectivity=connectivity, linkage="ward"
).fit(X)
elapsed_time = time.time() - st
label = ward.labels_
print(f"Elapsed time: {elapsed_time:.2f}s")
print(f"Number of points: {label.size}")
Compute structured hierarchical clustering...
Elapsed time: 0.07s
Number of points: 1500
绘制结果#
绘制结构化层次聚类。
fig2 = plt.figure()
ax2 = fig2.add_subplot(121, projection="3d", elev=7, azim=-80)
ax2.set_position([0, 0, 0.95, 1])
for l in np.unique(label):
ax2.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",
)
fig2.suptitle(f"With connectivity constraints (time {elapsed_time:.2f}s)")
plt.show()
脚本总运行时间:(0 分 0.402 秒)
相关示例
硬币图像上结构化 Ward 层次聚类的演示
有结构和无结构的凝聚聚类
绘制层次聚类树状图
在玩具数据集上比较不同的聚类算法