备注
Go to the end 下载完整的示例代码。或者通过浏览器中的MysterLite或Binder运行此示例
2D数字嵌入上的各种聚集性聚集#
数字数据集的2D嵌入上聚集集群的各种链接选项的说明。
此示例的目标是直观地显示指标的行为方式,而不是为数字找到良好的集群。这就是为什么该示例适用于2D嵌入。
这个例子向我们展示的是聚集聚类的“富的越来越富”的行为,它往往会创建不均匀的聚类大小。
这种行为对于平均链接策略来说很明显,最终会产生几个数据点很少的集群。
单一连锁的情况甚至更加病态,一个非常大的集群覆盖了大多数数字,一个中等大小的(干净的)集群具有大多数零数字,而所有其他集群都是从边缘周围的噪音点中提取的。
其他链接策略会导致更均匀分布的集群,因此对数据集的随机重新分配可能不太敏感。
Computing embedding
Done.
ward : 0.06s
average : 0.03s
complete : 0.03s
single : 0.01s
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause
from time import time
import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets, manifold
digits = datasets.load_digits()
X, y = digits.data, digits.target
n_samples, n_features = X.shape
np.random.seed(0)
# ----------------------------------------------------------------------
# Visualize the clustering
def plot_clustering(X_red, labels, title=None):
x_min, x_max = np.min(X_red, axis=0), np.max(X_red, axis=0)
X_red = (X_red - x_min) / (x_max - x_min)
plt.figure(figsize=(6, 4))
for digit in digits.target_names:
plt.scatter(
*X_red[y == digit].T,
marker=f"${digit}$",
s=50,
c=plt.cm.nipy_spectral(labels[y == digit] / 10),
alpha=0.5,
)
plt.xticks([])
plt.yticks([])
if title is not None:
plt.title(title, size=17)
plt.axis("off")
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
# ----------------------------------------------------------------------
# 2D embedding of the digits dataset
print("Computing embedding")
X_red = manifold.SpectralEmbedding(n_components=2).fit_transform(X)
print("Done.")
from sklearn.cluster import AgglomerativeClustering
for linkage in ("ward", "average", "complete", "single"):
clustering = AgglomerativeClustering(linkage=linkage, n_clusters=10)
t0 = time()
clustering.fit(X_red)
print("%s :\t%.2fs" % (linkage, time() - t0))
plot_clustering(X_red, clustering.labels_, "%s linkage" % linkage)
plt.show()
Total running time of the script: (0分1.184秒)
相关实例

Manifold learning on handwritten digits: Locally Linear Embedding, Isomap...
Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>
_