2011-02-23 26 views
0

努力與此樹狀圖中發現的代碼塊,它的工作,直到電話:如何用Python創建非ASCII樹狀圖?

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

然後噴出的錯誤:

RPy_RException: Error in dist(t(mt)) : (list) object cannot be coerced to type '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()') 

回答

1

這不是你的RPy _...異常問題的答案。而是提供回答您的標題How do I create a non-ascii dendrogram with Python?。你可以試試這個來繪製dendrogram

+0

即時膝蓋在scipy和matplotlib深處,和作品。 ive得到了所有安裝並運行了幾個示例。 我大概在這裏問了這個問題 - http://stackoverflow.com/questions/5089030/how-do-i-create-a-radial-cluster-like-the-following-code-example-in-python – Morvern 2011-02-23 09:28:46

+0

@Morvern:所以我的回答幫助你移動正確的方向;-)。 – eat 2011-02-23 10:05:50

+0

true〜你能幫助你提供的鏈接嗎?呵呵 – Morvern 2011-02-24 04:38:28