SciPy - leaders() 方法



SciPy leaders() 方法返回層次聚類的根節點。這基於用於聚類資料點的無監督機器學習演算法。

語法

以下是 SciPy leaders() 方法的語法:

leaders(Z, clusters)

引數

此方法接受以下引數:-

  • Z:此引數儲存名為 linkage() 的方法。
  • clusters:此引數用於儲存在三個引數 Z、t 和 criterion 上工作的 fcluster() 方法。

返回值

此方法返回 N 維陣列。

示例 1

以下是說明 SciPy leaders() 方法用法的基本示例:

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

inp_data = np.array([[10, 20], [40, 50], [50, 60], [70, 80], [10, 0]])
Z = linkage(inp_data, 'ward')
clusters = fcluster(Z, t=10, criterion='distance')
leader_indices, counts = leaders(Z, clusters)
print("The result of leader indices:", leader_indices)
print("The cluster sizes:", counts)

輸出

以上程式碼生成以下結果:

The result of leader indices: [0 4 1 2 3]
The cluster sizes: [1 2 3 4 5]

輸出的詳細解釋:

此處,預設情況下,前導索引對給定資料輸入的順序進行排序。要了解其群集形式,請檢視以下資料索引:

  • 群集 1 的前導:資料索引 0 位於 [10, 20]。
  • 群集 2 的前導:資料索引 4 位於 [10, 0]。
  • 群集 3 的前導:資料索引 1 位於 [40, 50]。
  • 群集 4 的前導:資料索引 2 位於 [50, 60]。
  • 群集 5 的前導:資料索引 3 位於 [70, 80]。
scipy_reference.htm
廣告
© . All rights reserved.