階層式分群中的群集標籤

Unsupervised Learning in Python

Benjamin Wilson

Director of Research at lateral.io

階層式分群中的群集標籤

  • 不只是視覺化工具!
  • 可在任一中間階段取回群集標籤
  • 可用於例如交叉列表

Eurovision hierarchical clustering

Unsupervised Learning in Python

中間分群與系統樹圖的高度

  • 例如高度 15:
    • Bulgaria、Cyprus、Greece 為同一群
    • Russia 與 Moldova 為另一群
    • Armenia 自成一群

放大群集,水平線於高度 15

Unsupervised Learning in Python

系統樹圖呈現群集距離

  • 系統樹圖的高度=合併群集之間的距離
  • 例如僅含 Cyprus 與 Greece 的群集距離約為 6

放大群集,標示 Cyprus/Greece 群集

Unsupervised Learning in Python

系統樹圖呈現群集距離

  • 系統樹圖的高度=合併群集之間的距離
  • 例如僅含 Cyprus 與 Greece 的群集距離約為 6
  • 此新群集與僅含 Bulgaria 的群集距離約為 12

放大群集,標示 Cyprus/Greece 及 Cyprus/Greece/Bulgaria 群集

Unsupervised Learning in Python

中間分群與系統樹圖的高度

  • 系統樹圖的高度指定要合併群集的最大距離
  • 別合併距離超過此值(例如 15)的群集

放大群集,水平線於高度 15

Unsupervised Learning in Python

群集之間的距離

  • 由「連結方法」定義
  • 在「complete」連結中:群集間距離為其樣本間的最大距離
  • 透過參數指定,例如 linkage(samples, method="complete")
  • 連結方法不同,階層式分群也不同!
Unsupervised Learning in Python

擷取群集標籤

  • 使用 fcluster() 函式
  • 傳回含群集標籤的 NumPy 陣列

放大群集,水平線於高度 15

Unsupervised Learning in Python

使用 fcluster 擷取群集標籤

from scipy.cluster.hierarchy import linkage
mergings = linkage(samples, method='complete')
from scipy.cluster.hierarchy import fcluster

labels = fcluster(mergings, 15, criterion='distance') print(labels)
[ 9  8 11 20  2  1 17 14 ... ]
Unsupervised Learning in Python

將群集標籤對齊國家名稱

給定字串清單 country_names

import pandas as pd
pairs = pd.DataFrame({'labels': labels, 'countries': country_names})
print(pairs.sort_values('labels'))
               countries  labels
5                Belarus       1
40               Ukraine       1
...
36                 Spain       5
8               Bulgaria       6
19                Greece       6
10                Cyprus       6
28               Moldova       7
...
Unsupervised Learning in Python

一起來練習吧!

Unsupervised Learning in Python

Preparing Video For Download...