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

© . All rights reserved.