树状图y轴标签混淆问题

11

我在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.)
它生成了一个类似于树状图的图形:enter image description here 我的问题涉及y轴。在我看到的所有示例中,Y轴被限制在0和2之间 - 我已经理解它表示为(1-corr)。在我的结果中,边界要高得多。0代表高度相关的项目(1-1=0),而2则是低度相关内容的截止点(1--1=2)
我找到了以下答案,但它与这个答案不一致,以及参考讲义在这里
无论如何 - 希望有人可以澄清哪个来源是正确的,并帮助传播一些关于此主题的知识。
1个回答

6
度量标准,用于linkage()的是欧几里得距离,详情请参见此处,而不是实际值。因此,它可以超过2,这完全取决于我们使用的距离度量类型。
这支持答案中提到的观点。
然后,这些距离用于计算树,使用以下计算在每对聚类之间。 来自文档:

enter image description here

在你提到的示例中,尽管各个值没有超出 (-1, +1) 的范围,但我们将得到以下谱系图。

enter image description here

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

原因是大小为45的距离数组(10 C 2 - 每一对列; 排序在这里中解释)将具有以下值:
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),则树状图会类似于这样!

enter image description here

因此,你的问题的解决方案是,
你需要将相关值转换为某种距离度量形式。
1. 我们可以使用squareform(),这是在其他答案中建议的一种方法。这是一种管道带方法,用于实现距离度量的两个方面。它必须为零[在同两点之间],并且对于任何两个点都不能为负数。这可以通过从1中减去每个corr值来实现。
2. 直接使用distance.pdist函数作为度量标准的相关性即可。实现可在此处找到。请记得转换数据框,因为我们需要每列之间的相关性,而不是行。
理解解决方案的示例:
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)

enter image description here


很高兴我能帮忙 :)。 - Venkatachalam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接