SciPy - fcluster() 方法



SciPy 的 fcluster() 方法是层次聚类的一部分,它借助于无监督机器学习来识别聚类(连接矩阵)的类别。

以下是此方法的一些现实生活中的用途:

  • 它识别客户细分。
  • 最受欢迎的是遗传聚类,它被用于医疗保健领域来定义其基因相似性。
  • 它根据人口统计区域识别图像的相似性。

语法

以下是 SciPy fcluster() 方法的语法:

fcluster(Z, t, criterion)

参数

此方法接受以下参数:

  • Z:这定义了连接矩阵。
  • t:此参数定义了可以请求的聚类数量。
  • criterion:该参数用于定义扁平聚类的类型,例如距离、不一致和最大距离。所有这些聚类都可以以字符串的形式编写。

返回值

此方法返回 n 维数组。

示例 1

以下基本示例说明了fcluster() 方法的使用。

from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [1, 0]])
Z = linkage(data, 'ward')
clusters = fcluster(Z, t=10, criterion='distance')
print(clusters)

输出

以上代码产生以下结果:

[1 1 1 1 1]

示例 2

此程序指定了确切的聚类数量,即t = 3,并将 maxclust 设置为将给定数据划分为正好 3 个聚类的标准。

from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [1, 0]])
Z = linkage(data, 'ward')
res_cluster = fcluster(Z, t=3, criterion='maxclust')
print(res_cluster)

输出

以上代码产生以下结果:

[1 2 2 3 1]

示例 3

在下面的程序中,将值'inconsistent' 设置为 fcluster() 方法中的标准,并且此用法定义了矩阵的不一致性。因此,这识别出与该值具有较低不一致性的聚类。

from scipy.cluster.hierarchy import linkage, fcluster, inconsistent
import numpy as np

data = np.array([[11, 12], [13, 14], [15, 16], [17, 18], [11, 10]])
Z = linkage(data, 'ward')
R = inconsistent(Z)
res_cluster = fcluster(Z, t=1.15, criterion='inconsistent')
print(res_cluster)

输出

以上代码产生以下结果:

[1 1 1 1 1]
scipy_reference.htm
广告