OPTICS 聚类算法

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

本实验展示了如何使用 scikit-learn 库中的 OPTICS 聚类算法。OPTICS 是 Ordering Points To Identify the Clustering Structure(排序点以识别聚类结构)的首字母缩写。它是一种基于密度的聚类算法,可找到高密度的核心样本并从这些样本扩展聚类。在本实验中,我们将生成样本数据、绘制可达性图,并使用 DBSCAN 对数据进行聚类。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们会及时为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/cluster("Clustering") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/cluster -.-> lab-49234{{"OPTICS 聚类算法"}} ml/sklearn -.-> lab-49234{{"OPTICS 聚类算法"}} end

导入库

我们将首先为本实验导入必要的库——numpy、matplotlib.pyplot,以及来自 scikit-learn 的 OPTICS 和 cluster_optics_dbscan。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import OPTICS, cluster_optics_dbscan

生成样本数据

我们将生成用于聚类的样本数据。在这个例子中,我们使用 numpy 生成六个密度不同的聚类。

np.random.seed(0)
n_points_per_cluster = 250

C1 = [-5, -2] + 0.8 * np.random.randn(n_points_per_cluster, 2)
C2 = [4, -1] + 0.1 * np.random.randn(n_points_per_cluster, 2)
C3 = [1, -2] + 0.2 * np.random.randn(n_points_per_cluster, 2)
C4 = [-2, 3] + 0.3 * np.random.randn(n_points_per_cluster, 2)
C5 = [3, -2] + 1.6 * np.random.randn(n_points_per_cluster, 2)
C6 = [5, 6] + 2 * np.random.randn(n_points_per_cluster, 2)
X = np.vstack((C1, C2, C3, C4, C5, C6))

运行 OPTICS 聚类算法

现在我们将对生成的数据运行 OPTICS 聚类算法。在这个例子中,我们设置 min_samples=50xi=0.05min_cluster_size=0.05

clust = OPTICS(min_samples=50, xi=0.05, min_cluster_size=0.05)
clust.fit(X)

使用 DBSCAN 对数据进行聚类

我们将使用不同的 epsilon 值,通过 DBSCAN 对数据进行聚类。在这个例子中,我们将 epsilon 设置为 0.5 和 2。

labels_050 = cluster_optics_dbscan(
    reachability=clust.reachability_,
    core_distances=clust.core_distances_,
    ordering=clust.ordering_,
    eps=0.5,
)
labels_200 = cluster_optics_dbscan(
    reachability=clust.reachability_,
    core_distances=clust.core_distances_,
    ordering=clust.ordering_,
    eps=2,
)

绘制可达性图和聚类结果

我们将绘制不同 epsilon 值下 OPTICS 和 DBSCAN 的可达性图以及聚类结果。

space = np.arange(len(X))
reachability = clust.reachability_[clust.ordering_]
labels = clust.labels_[clust.ordering_]

plt.figure(figsize=(10, 7))
G = gridspec.GridSpec(2, 3)
ax1 = plt.subplot(G[0, :])
ax2 = plt.subplot(G[1, 0])
ax3 = plt.subplot(G[1, 1])
ax4 = plt.subplot(G[1, 2])

## 可达性图
colors = ["g.", "r.", "b.", "y.", "c."]
for klass, color in zip(range(0, 5), colors):
    Xk = space[labels == klass]
    Rk = reachability[labels == klass]
    ax1.plot(Xk, Rk, color, alpha=0.3)
ax1.plot(space[labels == -1], reachability[labels == -1], "k.", alpha=0.3)
ax1.plot(space, np.full_like(space, 2.0, dtype=float), "k-", alpha=0.5)
ax1.plot(space, np.full_like(space, 0.5, dtype=float), "k-.", alpha=0.5)
ax1.set_ylabel("可达性 (epsilon 距离)")
ax1.set_title("可达性图")

## OPTICS
colors = ["g.", "r.", "b.", "y.", "c."]
for klass, color in zip(range(0, 5), colors):
    Xk = X[clust.labels_ == klass]
    ax2.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
ax2.plot(X[clust.labels_ == -1, 0], X[clust.labels_ == -1, 1], "k+", alpha=0.1)
ax2.set_title("自动聚类\nOPTICS")

## DBSCAN 在 0.5 时
colors = ["g.", "r.", "b.", "c."]
for klass, color in zip(range(0, 4), colors):
    Xk = X[labels_050 == klass]
    ax3.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
ax3.plot(X[labels_050 == -1, 0], X[labels_050 == -1, 1], "k+", alpha=0.1)
ax3.set_title("在 0.5 epsilon 截断时的聚类\nDBSCAN")

## DBSCAN 在 2. 时
colors = ["g.", "m.", "y.", "c."]
for klass, color in zip(range(0, 4), colors):
    Xk = X[labels_200 == klass]
    ax4.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
ax4.plot(X[labels_200 == -1, 0], X[labels_200 == -1, 1], "k+", alpha=0.1)
ax4.set_title("在 2.0 epsilon 截断时的聚类\nDBSCAN")

plt.tight_layout()
plt.show()

总结

在这个实验中,我们学习了如何使用 OPTICS 聚类算法对数据进行聚类,以及如何使用 DBSCAN 在不同的 epsilon 值下对数据进行聚类。我们还学习了如何绘制可达性图和聚类结果。