2015-02-24 71 views
7

我試圖使用SciPy的dendrogram方法根據閾值將我的數據劃分爲多個簇。但是,一旦我創建樹狀圖並檢索其color_list,列表中的條目少於標籤。通過閾值將SciPy等級樹狀圖切割爲簇

另外,我已經嘗試使用fclusterdendrogram中標識的閾值相同;然而,這不會產生相同的結果 - 它給我一個羣集而不是三個。

這是我的代碼。

import pandas 
data = pandas.DataFrame({'total_runs': {0: 2.489857755536053, 
1: 1.2877651950650333, 2: 0.8898850111727028, 3: 0.77750321282732704, 4: 0.72593099987615461, 5: 0.70064977003207007, 
6: 0.68217502514600825, 7: 0.67963194285399975, 8: 0.64238326692987524, 9: 0.6102581538587678, 10: 0.52588765899448564, 
11: 0.44813665774322564, 12: 0.30434031343774476, 13: 0.26151929543260161, 14: 0.18623657993534984, 15: 0.17494230269731209, 
16: 0.14023670906519603, 17: 0.096817318756050832, 18: 0.085822227670014059, 19: 0.042178447746868117, 20: -0.073494398270518693, 
21: -0.13699665903273103, 22: -0.13733324345373216, 23: -0.31112299949731331, 24: -0.42369178918768974, 25: -0.54826542322710636, 
26: -0.56090603814914863, 27: -0.63252372328438811, 28: -0.68787316140457322, 29: -1.1981351436422796, 30: -1.944118415387774, 
31: -2.1899746357945964, 32: -2.9077222144449961}, 
'total_salaries': {0: 3.5998991340231234, 
1: 1.6158435140488829, 2: 0.87501176080187315, 3: 0.57584734201367749, 4: 0.54559862861592978, 5: 0.85178295446270169, 
6: 0.18345463930386757, 7: 0.81380836410678736, 8: 0.43412670908952178, 9: 0.29560433676606418, 10: 1.0636736398252848, 
11: 0.08930130612600648, 12: -0.20839133305170349, 13: 0.33676911316165403, 14: -0.12404710480916628, 15: 0.82454221267393346, 
16: -0.34510456295395986, 17: -0.17162157282367937, 18: -0.064803261585569982, 19: -0.22807757277294818, 20: -0.61709008778669083, 
21: -0.42506873158089231, 22: -0.42637946918743924, 23: -0.53516500398181921, 24: -0.68219830809296633, 25: -1.0051418692474947, 
26: -1.0900316082184143, 27: -0.82421065378673986, 28: 0.095758053930450004, 29: -0.91540963929213015, 30: -1.3296449323844519, 
31: -1.5512503530547552, 32: -1.6573856443389405}}) 

from scipy.spatial.distance import pdist 
from scipy.cluster.hierarchy import linkage, dendrogram 

distanceMatrix = pdist(data) 
dend = dendrogram(linkage(distanceMatrix, method='complete'), 
      color_threshold=4, 
      leaf_font_size=10, 
      labels = df.teamID.tolist()) 

dendrogram

len(dend['color_list']) 
Out[169]: 32 
len(df.index) 
Out[170]: 33 

爲什麼dendrogram僅指定顏色到32級的標籤,雖然有在數據33個觀測?這是我如何提取標籤及其相應的羣集(上面用藍色,綠色和紅色着色)?如果不是,我還會如何正確地切割樹?

這是我使用fcluster的嘗試。當dend的相同閾值返回3時,它爲什麼只返回一個集羣?

from scipy.cluster.hierarchy import fcluster 
fcluster(linkage(distanceMatrix, method='complete'), 4) 
Out[175]: 
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32) 
+0

關於'color_list'的文檔狀態:'顏色名稱列表。第k個元素表示第k個鏈接的顏色。請參閱:http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.dendrogram.html。我認爲這是一個誤解。 – cel 2015-02-24 07:08:19

+0

考慮到這一點,如果我在4的閾值下砍樹,我怎麼看到聚類結果? – Bryan 2015-02-24 16:52:18

+0

此外,由於這個問題在很多時候都沒有得到很好的答案(請參閱下面的註釋),所以我會爲此付出代價。具體問題是:在SciPy中使用樹狀圖,我如何在給定特定閾值級別的情況下將數據切割成集羣,然後用這些集羣的相應觀察標籤收集這些集羣?見http://stackoverflow.com/questions/9708630/some-questions-on-dendrogram-python-scipy?rq=1,和http://stackoverflow.com/questions/10305111/pruning-dendrogram-in-scipy-等級聚類?rq = 1 – Bryan 2015-02-24 16:58:55

回答

8

下面是答案 - 我沒有添加'距離'作爲fcluster的選項。有了它,我得到了正確的(3)集羣分配。

assignments = fcluster(linkage(distanceMatrix, method='complete'),4,'distance') 

print assignments 
     [3 2 2 2 2 2 2 2 2 2 2 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] 

cluster_output = pandas.DataFrame({'team':df.teamID.tolist() , 'cluster':assignments}) 

print cluster_output 
    cluster team 
0   3 NYA 
1   2 BOS 
2   2 PHI 
3   2 CHA 
4   2 SFN 
5   2 LAN 
6   2 TEX 
7   2 ATL 
8   2 SLN 
9   2 SEA 
10  2 NYN 
11  2 HOU 
12  1 BAL 
13  2 DET 
14  1 ARI 
15  2 CHN 
16  1 CLE 
17  1 CIN 
18  1 TOR 
19  1 COL 
20  1 OAK 
21  1 MIL 
22  1 MIN 
23  1 SDN 
24  1 KCA 
25  1 TBA 
26  1 FLO 
27  1 PIT 
28  1 LAA 
29  1 WAS 
30  1 ANA 
31  1 MON 
32  1 MIA 
+0

這個問題一直困擾着我。我最終得出結論,fcluster的順序與輸入距離矩陣的順序相同,不同於樹狀圖中從左到右的葉子。 – user3479780 2016-07-25 05:44:14