【问题标题】:feeding distance matrix to R clustering from Rpy2从 Rpy2 将距离矩阵馈送到 R 聚类
【发布时间】:2013-04-02 14:43:59
【问题描述】:

我在 numpy/scipy 中有以下定制的 NxN 距离矩阵:

dist_matrix =    array([array([5, 4, 2, 3, 2, 3]),
                        array([4, 5, 2, 3, 2, 2]), 
                        array([2, 2, 5, 2, 2, 1]), 
                        array([3, 3, 2, 5, 4, 2]), 
                        array([2, 2, 2, 4, 5, 1]), 
                        array([3, 2, 1, 2, 1, 5])])

如何使用此矩阵在 R / ggplot2 中进行层次聚类和绘制树状图?如果我尝试通过 rpy2 将此距离矩阵输入 R:

r.hclust(dist_matrix)

我得到错误:

   res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed

【问题讨论】:

  • dist_matrix 是一个 NxN 数组,还是一维数组的形状为 (N,) 和 dtype object 的一维数组?
  • @askewchan:我可以采用任何一种格式...我都尝试过并得到错误...但无论如何我可以在两者之间来回转换

标签: python r numpy cluster-analysis rpy2


【解决方案1】:

R 函数hclust() 正在获取“距离”对象:

from rpy2.robjects.packages import importr
stats = importr("stats")
d = stats.as_dist(m)
hc = r.hclust(d)

[注意:错误消息还暗示了 rpy2 中可能存在的转换错误。您可以提交错误报告吗?谢谢]

【讨论】:

猜你喜欢
  • 2012-09-05
  • 2016-12-29
  • 2015-04-21
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 2013-04-21
相关资源
最近更新 更多