如何使用 Python 创建非 ASCII 树形图?

发布于 2024-10-19 07:25:28 字数 1624 浏览 2 评论 0原文

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

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

然后抛出错误:

RPy_RException:dist(t(mt)) 中的错误:无法强制(列表)对象键入“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()')

Trying to create a dendrogram with this found code block, and it works up until the call:

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

then spouts the error:

RPy_RException: Error in dist(t(mt)) : (list) object cannot be coerced to type 'double'

up until that point it was looking good ... I'm probably missing something really simple

Any help?

#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()')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怂人 2024-10-26 07:25:28

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

This is not answer to your RPy_... exception problem. Rather providing answer to your title How do I create a non-ascii dendrogram with Python?. You may try this to plot dendrogram.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文