SciPy: 连接错误

SciPy: error on linkage

任务 我想对从 CSV 文件检索到的数组使用 运行 链接函数。以下是我正在使用的脚本:

r = []
with open('D:\ResultsFiles\mainforspecial.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile)
    for row in spamreader:
     r.append(row)
vals = [line[1:] for line in r[1:]]
varl2 = np.array(vals,dtype=object)
try:
  linkage_matrix = linkage(varl2, "single")
except ValueError,e:
        print "error",e

然而,当我 运行 这段代码时,它给我一个错误 "could not convert string to float"。我在 Whosebug 上探索了其他问题,发现要解决此错误数据格式需要考虑。

val2的格式如下:

 [['0' '1' '22','0' '0' '2948']
 ['0' '1' '16', '0' '1' '2945']
 ['0' '2' '19' , '0' '0' '2854']
 ..., 
 ['0' '1' '0' ,'1' '0' '53']
 ['0' '1' '0' , '1' '0' '3498']
 ['0' '21' '9' ,'1' '0' '2878']]

好的,我已经找到答案供以后参考:

对 list/matrix 的每个项目使用 python 中的 float() 函数来获取导致错误的确切项目编号。在我的例子中,只有一行最后有一个额外的 ''