树状图 y 轴标记混乱

Dendrogram y-axis labeling confusion

我在 pandas 中有一个大的 (106x106) 相关矩阵,其结构如下:

+---+-------------------+------------------+------------------+------------------+------------------+-----------------+------------------+------------------+------------------+-------------------+
|   |         0         |        1         |        2         |        3         |        4         |        5        |        6         |        7         |        8         |         9         |
+---+-------------------+------------------+------------------+------------------+------------------+-----------------+------------------+------------------+------------------+-------------------+
| 0 |               1.0 |   0.465539925807 |   0.736955649673 |   0.733077703346 |  -0.177380436347 | -0.268022641963 |  0.0642473239514 | -0.0136866435594 |  -0.025596700815 | -0.00385065532308 |
| 1 |    0.465539925807 |              1.0 |  -0.173472213691 |   -0.16898620433 | -0.0460674481563 | 0.0994673318696 |   0.137137216943 |   0.061999118034 |  0.0944808695878 |   0.0229095105328 |
| 2 |    0.736955649673 |  -0.173472213691 |              1.0 |   0.996627003263 |  -0.172683935315 |  -0.33319698831 | -0.0562591684255 | -0.0306820050477 | -0.0657065745626 |  -0.0457836647012 |
| 3 |    0.733077703346 |   -0.16898620433 |   0.996627003263 |              1.0 |  -0.153606414649 | -0.321562257834 | -0.0465540370732 | -0.0224318843281 | -0.0586629098513 |  -0.0417237678539 |
| 4 |   -0.177380436347 | -0.0460674481563 |  -0.172683935315 |  -0.153606414649 |              1.0 | 0.0148395123941 |   0.191615549534 |   0.289211355855 |    0.28799868259 |    0.291523969899 |
| 5 |   -0.268022641963 |  0.0994673318696 |   -0.33319698831 |  -0.321562257834 |  0.0148395123941 |             1.0 |   0.205432455075 |   0.445668299971 |   0.454982398693 |    0.427323555674 |
| 6 |   0.0642473239514 |   0.137137216943 | -0.0562591684255 | -0.0465540370732 |   0.191615549534 |  0.205432455075 |              1.0 |   0.674329392219 |   0.727261969241 |     0.67891326835 |
| 7 |  -0.0136866435594 |   0.061999118034 | -0.0306820050477 | -0.0224318843281 |   0.289211355855 |  0.445668299971 |   0.674329392219 |              1.0 |   0.980543049288 |    0.939548790275 |
| 8 |   -0.025596700815 |  0.0944808695878 | -0.0657065745626 | -0.0586629098513 |    0.28799868259 |  0.454982398693 |   0.727261969241 |   0.980543049288 |              1.0 |    0.930281915882 |
| 9 | -0.00385065532308 |  0.0229095105328 | -0.0457836647012 | -0.0417237678539 |   0.291523969899 |  0.427323555674 |    0.67891326835 |   0.939548790275 |   0.930281915882 |               1.0 |
+---+-------------------+------------------+------------------+------------------+------------------+-----------------+------------------+------------------+------------------+-------------------+

为简单起见,此处截断。

如果我计算链接,然后使用以下代码绘制树状图:

from scipy.cluster.hierarchy import dendrogram, linkage
Z = linkage(result_df.corr(),'average')
plt.figure()
fig, axes = plt.subplots(1, 1, figsize=(20, 20))
axes.tick_params(axis='both', which='major', labelsize=15)

dendrogram(Z=Z, labels=result_df_null_cols.columns, 
           leaf_rotation=90., ax=axes, 
           color_threshold=2.)

它生成的树状图如下:

我的问题是关于 y 轴的。在我见过的所有例子中,Y 轴都在 0,2 之间——我读到它解释为 (1-corr)。在我的结果中,边界要高得多。 0 是高度相关的项目 (1-1 = 0),2 是低相关项目的截止值 (1 - -1 = 2)

我找到了 but it does not agree with this answer and the referenced lecture notes here

无论如何 - 希望有人能澄清哪个来源是正确的,并帮助传播有关该主题的一些知识。

用于 linkage() 的度量是欧氏距离,参见 here 而不是实际值。因此,它可以超过 2,这完全取决于我们使用的距离度量的类型。

这支持this回答中提到的观点。

1) The y-axis is a measure of closeness of either individual data points or clusters.

然后,这些距离用于计算树,在每对簇之间使用以下计算。

From Documentation:

在您提到的示例中,

即使单个值不超过(-1, +1),我们也会得到以下树状图。

from scipy.spatial import distance
distance.pdist(df, 'euclidean')

原因是大小为 45 的距离数组(10 C 2 - 每对列;排序已解释 here)将具有以下值:

array([1.546726  , 0.79914141, 0.79426728, 2.24085106, 2.50838998,
       2.22772899, 2.52578923, 2.55978527, 2.51553289, 2.11329023,
       2.10501739, 1.66536963, 1.6303103 , 1.71821177, 2.04386712,
       2.03917033, 2.03614219, 0.0280283 , 2.33440388, 2.68373496,
       2.43771817, 2.68351612, 2.73148741, 2.66843754, 2.31758222,
       2.67031469, 2.4206485 , 2.66539997, 2.7134241 , 2.65058045,
       1.44756593, 1.39699605, 1.55063416, 1.56324546, 1.52001219,
       1.32204039, 1.30206957, 1.29596715, 1.2895916 , 0.65145881,
       0.62242858, 0.6283212 , 0.08642582, 0.11145739, 0.14420816])

如果我们构建一个距离均匀的随机值矩阵。 (-1, 1) 大小 (160, 160),树状图将与此类似!

因此,您的问题的解决方案是,

您需要将相关值转换为某种形式的距离度量。

  1. 我们可以使用相同的 squareform() suggested in the 。这是一种管道胶带方法,可实现距离测量的两个方面。它必须是零 [在相同的两点之间] 并且对于任何两点都是非负数。这可以通过从一个中减去每个 corr 值来实现。

  2. 我们可以直接使用distance.pdist函数,将相关性作为度量。该实现可用 here。请记住转换数据框,因为我们需要每一列而不是行之间的相关性。

理解解决方案的示例:

size = (10000,1)
col1 = np.random.randint(0,100,size) # base column
col2 = col1 * 0.9 + np.random.normal(0,2,size) # huge corr with small noise
col3 = col1 * 0.1 + np.random.normal(0,100,size) # uncorrelated column
col4 = col1 * (-0.5) + np.random.normal(0,1,size) # negatively corr

data = np.hstack((col1,col2,col3,col4))
df = pd.DataFrame(data , columns=list('ABCD'))

df.corr()

    A   B   C   D
A   1.000000    0.997042    0.029078    -0.997614
B   0.997042    1.000000    0.029233    -0.994677
C   0.029078    0.029233    1.000000    -0.028421
D   -0.997614   -0.994677   -0.028421   1.000000

#pdist_values = distance.squareform(1 - df.corr().values )
pdist_values = distance.pdist(df.T, 'correlation')
z = linkage(pdist_values, method='average')
dendrogram(z, labels=df.columns)