python - 从 Rpy2 到 R 聚类的馈送距离矩阵

标签 python r numpy cluster-analysis rpy2

我在 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

最佳答案

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

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

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

关于python - 从 Rpy2 到 R 聚类的馈送距离矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931261/

相关文章:

r - 带循环的多个数据帧上的 Ggplot

r - 在 R 中值 >0 的两列之间插入 0 的行值

r - 如何循环遍历 R 中的 CSV 文件文件夹

python - 获取numpy数组的多个轴的平均值

python - 如何在 Tensorflow 中进行列求和?

python - numpy:反转上三角矩阵

python - 在 matplotlib 中对 x 轴进行排序

python - 使用 pandas 将字符串替换为其自身的较短版本

python - 更高效的matplotlib堆积条形图——如何计算底值

python - 查找(并记录)numpy 数组切片的最大值