【问题标题】:Getting error while plotting the dendrogram for the spearmanr correlation绘制 spearmanr 相关性的树状图时出错
【发布时间】:2020-05-07 09:11:46
【问题描述】:

我在为 spearmanr 相关性绘制 树状图 时遇到错误。 下面是我正在使用的代码

corr = np.round(scipy.stats.spearmanr(full_data[list_of_continous]).correlation, 4)
corr_condensed = hc.distance.squareform(1-corr)
z = hc.linkage(corr_condensed, method='average')
fig = plt.figure(figsize=(20,20))
dendrogram = hc.dendrogram(z, labels=full_data[list_of_continous].columns, orientation='left', leaf_font_size=30)
plt.show()

以下是我得到的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-9873c0be8dc7> in <module>()
      1 corr = np.round(scipy.stats.spearmanr(full_data[list_of_continous]).correlation, 4)
----> 2 corr_condensed = hc.distance.squareform(1-corr)
      3 z = hc.linkage(corr_condensed, method='average')
      4 fig = plt.figure(figsize=(20,20))
      5 dendrogram = hc.dendrogram(z, labels=full_data[list_of_continous].columns, orientation='left', leaf_font_size=30)

/usr/local/anaconda/lib/python3.6/site-packages/scipy/spatial/distance.py in squareform(X, force, checks)
   1844             raise ValueError('The matrix argument must be square.')
   1845         if checks:
-> 1846             is_valid_dm(X, throw=True, name='X')
   1847 
   1848         # One-side of the dimensions is set here.

/usr/local/anaconda/lib/python3.6/site-packages/scipy/spatial/distance.py in is_valid_dm(D, tol, throw, name, warning)
   1920                 if name:
   1921                     raise ValueError(('Distance matrix \'%s\' must be '
-> 1922                                      'symmetric.') % name)
   1923                 else:
   1924                     raise ValueError('Distance matrix must be symmetric.')

ValueError: Distance matrix 'X' must be symmetric.

【问题讨论】:

  • 什么是corr.shape
  • 形状为 (94,94)

标签: python matplotlib machine-learning scipy data-science


【解决方案1】:

变量 corr 可能具有 nan 值,这可能会使它变形。
试试:

corr = np.nan_to_num(corr)

更新:

跳过

    corr_condensed = hc.distance.squareform(1-corr)

对我来说没有任何错误。

所以

corr = np.round(scipy.stats.spearmanr(full_data[list_of_continous]).correlation, 4)
z = hc.linkage(corr, method='average')
fig = plt.figure(figsize=(20,20))
dendrogram = hc.dendrogram(z, labels=full_data[list_of_continous].columns, orientation='left', leaf_font_size=30)
plt.show()

应该也适合你。

【讨论】:

    猜你喜欢
    • 2020-02-07
    • 2015-05-21
    • 1970-01-01
    • 2015-12-18
    • 2020-06-06
    • 2020-09-03
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多