scipy.cluster.hierarchy.

complete#

scipy.cluster.hierarchy.complete(y)[source]#

在壓縮距離矩陣上執行 complete/max/farthest point linkage。

參數:
yndarray

距離矩陣的上三角部分。pdist 的結果會以此形式傳回。

回傳值:
Zndarray

包含階層式分群的 linkage 矩陣。 有關其結構的更多資訊,請參閱 linkage 函數文件。

參見

linkage

用於進階建立階層式分群。

scipy.spatial.distance.pdist

成對距離度量

範例

>>> from scipy.cluster.hierarchy import complete, 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 = complete(y)
>>> Z
array([[ 0.        ,  1.        ,  1.        ,  2.        ],
       [ 3.        ,  4.        ,  1.        ,  2.        ],
       [ 6.        ,  7.        ,  1.        ,  2.        ],
       [ 9.        , 10.        ,  1.        ,  2.        ],
       [ 2.        , 12.        ,  1.41421356,  3.        ],
       [ 5.        , 13.        ,  1.41421356,  3.        ],
       [ 8.        , 14.        ,  1.41421356,  3.        ],
       [11.        , 15.        ,  1.41421356,  3.        ],
       [16.        , 17.        ,  4.12310563,  6.        ],
       [18.        , 19.        ,  4.12310563,  6.        ],
       [20.        , 21.        ,  5.65685425, 12.        ]])

linkage 矩陣 Z 代表一個樹狀圖 - 有關其內容的詳細說明,請參閱 scipy.cluster.hierarchy.linkage

我們可以利用 scipy.cluster.hierarchy.fcluster 來查看在給定距離閾值下,每個初始點將屬於哪個群集

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

此外,scipy.cluster.hierarchy.dendrogram 可用於產生樹狀圖的繪圖。