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
广告