SciPy:链接错误

时间:2015-07-12 08:07:04

标签: python numpy scipy

任务 我想在从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

然而,当我运行此代码时,它会给我一个"错误,无法将字符串转换为浮动"。我已经在stackoverflow上探讨了其他问题并发现要解决这种错误格式的数据需要考虑。

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']]

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案供将来参考:

在列表/矩阵的每个项目中使用python中的float()函数来获取导致错误的确切项目编号。在我的情况下,只有一行在末尾有一个额外的''

相关问题