scipy.cluster.hierarchy.

leaves_list#

scipy.cluster.hierarchy.leaves_list(Z)[源码]#

返回叶节点 ID 的列表。

返回值对应于从左到右在树中出现的观测向量索引。 Z 是一个连接矩阵。

参数:
Zndarray

层次聚类编码为矩阵。 Z 是一个连接矩阵。 有关详细信息,请参阅 linkage

返回:
leaves_listndarray

叶节点 ID 的列表。

参见

dendrogram

有关树状图结构的信息。

注释

leaves_list 除了 NumPy 之外,还对 Python Array API 标准兼容的后端提供实验性支持。 请考虑通过设置环境变量 SCIPY_ARRAY_API=1 并提供 CuPy、PyTorch、JAX 或 Dask 数组作为数组参数来测试这些功能。 支持以下后端和设备(或其他功能)的组合。

CPU

GPU

NumPy

不适用

CuPy

不适用

PyTorch

JAX

Dask

⚠️ 合并块

不适用

有关详细信息,请参阅 支持数组 API 标准

示例

>>> from scipy.cluster.hierarchy import ward, dendrogram, leaves_list
>>> from scipy.spatial.distance import pdist
>>> from matplotlib import pyplot as plt
>>> X = [[0, 0], [0, 1], [1, 0],
...      [0, 4], [0, 3], [1, 4],
...      [4, 0], [3, 0], [4, 1],
...      [4, 4], [3, 4], [4, 3]]
>>> Z = ward(pdist(X))

连接矩阵 Z 表示一个树状图,即编码所执行聚类结构的树。 scipy.cluster.hierarchy.leaves_list 显示了 X 数据集中的索引与树状图中的叶子之间的映射

>>> leaves_list(Z)
array([ 2,  0,  1,  5,  3,  4,  8,  6,  7, 11,  9, 10], dtype=int32)
>>> fig = plt.figure(figsize=(25, 10))
>>> dn = dendrogram(Z)
>>> plt.show()
../../_images/scipy-cluster-hierarchy-leaves_list-1.png