scipy.cluster.hierarchy.median

scipy.cluster.hierarchy.median(y)[源代码]

执行中间带/WPGMC连接。

看见 linkage 有关返回结构和算法的详细信息,请执行以下操作。

以下是常见的调用约定:

  1. Z = median(y)

    在压缩距离矩阵上执行中值/WPGMC链接 y 。看见 linkage 有关返回结构和算法的详细信息,请执行以下操作。

  2. Z = median(X)

    在观测矩阵上执行中值/WPGMC链接 X 使用欧几里得距离作为距离度量。看见 linkage 有关返回结构和算法的详细信息,请执行以下操作。

参数
yndarray

凝聚距离矩阵。压缩距离矩阵是包含距离矩阵的上三角形的平面阵列。这是一张表格,上面写着 pdist 返回。或者,n维的m个观测向量的集合可以作为m×n阵列传递。

退货
Zndarray

该分层聚类被编码为链接矩阵。

参见

linkage

用于高级创建分层群集。

scipy.spatial.distance.pdist

成对距离度量

示例

>>> from scipy.cluster.hierarchy import median, fcluster
>>> from scipy.spatial.distance import pdist

首先,我们需要一个玩具数据集来玩::

x x    x x
x        x

x        x
x x    x x
>>> 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]]

然后,我们从这个数据集中得到一个压缩距离矩阵:

>>> y = pdist(X)

最后,我们可以执行群集:

>>> Z = median(y)
>>> Z
array([[ 0.        ,  1.        ,  1.        ,  2.        ],
       [ 3.        ,  4.        ,  1.        ,  2.        ],
       [ 9.        , 10.        ,  1.        ,  2.        ],
       [ 6.        ,  7.        ,  1.        ,  2.        ],
       [ 2.        , 12.        ,  1.11803399,  3.        ],
       [ 5.        , 13.        ,  1.11803399,  3.        ],
       [ 8.        , 15.        ,  1.11803399,  3.        ],
       [11.        , 14.        ,  1.11803399,  3.        ],
       [18.        , 19.        ,  3.        ,  6.        ],
       [16.        , 17.        ,  3.5       ,  6.        ],
       [20.        , 21.        ,  3.25      , 12.        ]])

链接矩阵 Z 表示树状图-请参阅 scipy.cluster.hierarchy.linkage 有关其内容的详细说明,请参阅。

我们可以利用 scipy.cluster.hierarchy.fcluster 要查看在给定距离阈值的情况下每个初始点将属于哪个簇,请执行以下操作:

>>> fcluster(Z, 0.9, criterion='distance')
array([ 7,  8,  9, 10, 11, 12,  1,  2,  3,  4,  5,  6], dtype=int32)
>>> fcluster(Z, 1.1, criterion='distance')
array([5, 5, 6, 7, 7, 8, 1, 1, 2, 3, 3, 4], dtype=int32)
>>> fcluster(Z, 2, criterion='distance')
array([3, 3, 3, 4, 4, 4, 1, 1, 1, 2, 2, 2], dtype=int32)
>>> fcluster(Z, 4, criterion='distance')
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)

另外, scipy.cluster.hierarchy.dendrogram 可以用来生成树状图的曲线图。