當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy hierarchy.from_mlab_linkage用法及代碼示例

本文簡要介紹 python 語言中 scipy.cluster.hierarchy.from_mlab_linkage 的用法。

用法:

scipy.cluster.hierarchy.from_mlab_linkage(Z)#

將 MATLAB(TM) 生成的鏈接矩陣轉換為與該模塊兼容的新鏈接矩陣。

轉換做了兩件事:

  • the indices are converted from 1..N to 0..(N-1) form, and

  • a fourth column Z[:,3] is added where Z[i,3] represents the number of original observations (leaves) in the non-singleton cluster i.

當從 MATLAB 生成的舊數據文件加載鏈接時,此函數很有用。

參數

Z ndarray

由 MATLAB(TM) 生成的鏈接矩陣。

返回

ZS ndarray

scipy.cluster.hierarchy 兼容的鏈接矩陣。

例子

>>> import numpy as np
>>> from scipy.cluster.hierarchy import ward, from_mlab_linkage

給定 MATLAB 格式 mZ 的鏈接矩陣,我們可以使用 scipy.cluster.hierarchy.from_mlab_linkage 將其導入為 SciPy 格式:

>>> mZ = np.array([[1, 2, 1], [4, 5, 1], [7, 8, 1],
...                [10, 11, 1], [3, 13, 1.29099445],
...                [6, 14, 1.29099445],
...                [9, 15, 1.29099445],
...                [12, 16, 1.29099445],
...                [17, 18, 5.77350269],
...                [19, 20, 5.77350269],
...                [21, 22,  8.16496581]])
>>> Z = from_mlab_linkage(mZ)
>>> Z
array([[  0.        ,   1.        ,   1.        ,   2.        ],
       [  3.        ,   4.        ,   1.        ,   2.        ],
       [  6.        ,   7.        ,   1.        ,   2.        ],
       [  9.        ,  10.        ,   1.        ,   2.        ],
       [  2.        ,  12.        ,   1.29099445,   3.        ],
       [  5.        ,  13.        ,   1.29099445,   3.        ],
       [  8.        ,  14.        ,   1.29099445,   3.        ],
       [ 11.        ,  15.        ,   1.29099445,   3.        ],
       [ 16.        ,  17.        ,   5.77350269,   6.        ],
       [ 18.        ,  19.        ,   5.77350269,   6.        ],
       [ 20.        ,  21.        ,   8.16496581,  12.        ]])

正如預期的那樣,返回的鏈接矩陣Z 包括一個額外的列,用於計算每個集群中原始樣本的數量。此外,所有集群索引都減 1(MATLAB 格式使用 1 索引,而 SciPy 使用 0 索引)。

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.cluster.hierarchy.from_mlab_linkage。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。