SciPy - cophenet() 方法



SciPy cophenet() 方法計算層次聚類中每個觀察值之間的共距離。這些聚類使用連鎖方法定義,此方法顯示了聚類分裂。

共距離計算兩個點之間的距離,並透過樹狀圖進行說明。樹狀圖顯示了物件之間的層次關係。

語法

以下是 SciPy cophenet() 方法的語法 −

cophenet(Z, pdist(data))

引數

此方法接受以下兩個引數 −

  • Z:此引數儲存 linkage() 方法。
  • pdist(data):用於定義資料的成對分佈。

返回值

此方法返回浮點數作為結果。

示例 1

以下是說明 SciPy cophenet() 方法用法的基本程式。

import numpy as np
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.spatial.distance import pdist

# given data for 2D points
data = np.array([[10, 20], [20, 30], [30, 40], [50, 60], [80, 90]])

# hierarchical clustering
Z = linkage(data, method='single')

# cophenetic correlation coefficient
c, d = cophenet(Z, pdist(data))

print(f"The value of cophenetic correlation coefficient: {c}")

輸出

以上程式碼產生以下輸出 −

Cophenetic Correlation Coefficient: 0.8355044182110838

示例 2

此程式顯示使用完全連鎖方法的共距離相關係數的值。

import numpy as np
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.spatial.distance import pdist

# given data for 2d points
data = np.array([[10, 20], [20, 30], [30, 40], [50, 60], [80, 90]])

# perform hierarchical clustering using the 'complete' linkage method
Z_complete = linkage(data, method='complete')

# cophenetic correlation coefficient
c_complete, d_complete = cophenet(Z_complete, pdist(data))

print(f"The value of cophenetic correlation coefficient (using complete method): {c_complete}")

輸出

以上程式碼產生以下輸出 −

The value of cophenetic correlation coefficient (using complete method): 0.7173095078886984

示例 3

以下程式說明使用平均連鎖方法的共距離相關係數的值。

import numpy as np
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.spatial.distance import pdist

# given data for 2D points
data = np.array([[11, 22], [22, 33], [33, 44], [55, 66], [88, 99]])

# given data for five dimensional point
data_high_dim = np.random.rand(10, 5)

# hierarchical clustering
Z_high_dim = linkage(data_high_dim, method='average')

# cophenetic correlation coefficient
c_high_dim, d_high_dim = cophenet(Z_high_dim, pdist(data_high_dim))

print(f"The value of cophenetic correlation coefficient (high-dimensional): {c_high_dim}")

輸出

以上程式碼產生以下輸出 −

The value of cophenetic correlation coefficient (high-dimensional): 0.6727006277242108
scipy_reference.htm
廣告
© . All rights reserved.