【问题标题】:How do I create a non-ascii dendrogram with Python?如何使用 Python 创建非 ascii 树状图?
【发布时间】:2011-07-02 14:09:08
【问题描述】:

试图用这个找到的代码块创建一个树状图,它一直工作到调用:

r('mt_dist <- dist(t(mt))')

然后吐出错误:

RPy_RException:dist(t(mt)) 中的错误:(list) 对象不能被强制输入“double”

在那之前它看起来不错......我可能错过了一些非常简单的东西

有什么帮助吗?

#importing modules
from numpy import array
from random import normalvariate,shuffle
from rpy import r

# creating a random matrix
# creating it with different 'samples' in different columns
mt = []
for l in range(20): #20 lines
    line = []
    means = range(1,9)
    for c in range(8): # 8 columns
        #Colum 1: mean 1; Column 2: mean 2.... values normally distributed s.d. = 0.5       
        line.append(normalvariate(means.pop(), 0.5))

    mt.append(line)

# once we have a matrix, transform it in an array
mt_array = array(mt)

# The R work
# Pass the array to 'mt' variable in R
r.assign("mt", mt_array)

# manipulate R via r('command')
r('print(mt)') #print the matrix 'mt' to check values

#The clustering process
#Calculating distances with 'dist'
#'dist' calculates distance among lines, so I am transposing (with t()) in order to have my columns clustered
## I guess 'dist' uses euclidian distance as default

r('mt_dist <- dist(t(mt))')
# hclust does the clustering with upgma as default

r('result = hclust(mt_dist)')

# directs the output to a determinde file
r('png("output_file.png")')

# plot the result
labels = ["sample A", "sample B","sample C", "sample D","sample E", "sample F", "sample G", "sample H"]
r.assign("labels", labels)
r('plot(result, labels=labels, main="My title")')

# 'close' you output
r('dev.off()')

【问题讨论】:

    标签: python r numpy rpy2 dendrogram


    【解决方案1】:

    这不是您的 RPy_... 异常问题的答案。而是为您的标题How do I create a non-ascii dendrogram with Python? 提供答案。你可以试试这个来绘制dendrogram

    【讨论】:

    • 我在 scipy 和 matplotlib 以及这些作品中深陷其中。我已经全部安装好了,并且运行了几个示例。我可能在这里问得更好 - stackoverflow.com/questions/5089030/…
    • @Morvern:所以我的回答帮助你朝着正确的方向前进 ;-)。
    • true ~ 所以你能帮忙提供链接吗?呵呵
    猜你喜欢
    • 2014-02-13
    • 2013-05-02
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多