我想分層聚類2D numpy數組,使它看起來不錯,當我把它作爲相關矩陣在d3.js
。如何聚類nmapy係數的熱圖
我的數據是這樣的:
[[ 1. 0.091 0.147 ..., -0.239 0.113 -0.012 ]
[ 0.091 1. -0.153 ..., -0.004 -0.244 -0.00520801]
[ 0.147 -0.153 1. ..., -0.157 0.013 0.133]
...,
[-0.239 -0.004 -0.157 ..., -0.265 -0.362 1. ]]
我計算這些-1到1。正如你可以看到之間的Pearson相關係數,有一個1對1的相關性下降對角線從左上角數組右下方。
如果我繪製這些值,我的相關矩陣是這樣的:
集羣后,我希望它是有點類似於此,其中紅色代表陽性的相關性和藍色表示負相關:
使用matplotlib和SciPy的,我可以羣集係數看起來像一個熱圖,但是,值發生改變。我希望我的價值觀保持不變。
I used this answer to graph the heatmap in python, but its not quite what I want since it changes my values.。我需要的只是將數據聚集並輸出到csv/json文件。
from scipy.spatial.distance import pdist, squareform
from scipy.cluster.hierarchy import linkage, dendrogram
data_dist = pdist(final_correlation, 'correlation') # If I use this,
# it gives me an array that is half the size of my original correlation matrix. These are
# the distances. How do I use this to re-order my correlation matrix as a clustered matrix?
Out[1]: # The size is 9730, as opposed to the original size of 19,600
[ 0.612 0.503 1.653 ..., 0.792 1.577
0.829]
UPDATE 如果有人知道R
,我試圖執行可能會看起來像代碼this
一個完整和最小的例子與虛擬數據將是一個很大的幫助 – YXD 2014-12-08 15:07:32