ValueError:无法将字符串转换为float:XX

时间:2014-02-20 05:59:38

标签: python numpy

我正在运行以下代码:

from numpy import *
import operator


def file2matrix(filename):
    fr = open(filename)
    arrayOLines = fr.readlines()
    numberOfLines = len(arrayOLines)            #get the number of lines in the file
    returnMat = zeros((numberOfLines,3))        #prepare matrix to return
    classLabelVector = []                       #prepare labels return
    print numberOfLines

    index = 0
    for line in arrayOLines:
        line = line.strip()
        listFromLine = line.split('\t')
        print index
##        print listFromLine
##        returnMat[index ,0] = listFromLine[0]
        returnMat[index,:] =['XX','XY','XZ']
##        returnMat[index ,2] = listFromLine[2]
##        returnMat[index ,3] = listFromLine[3]
        classLabelVector.append(listFromLine[0])
        index += 1
    return returnMat,classLabelVector

第二个代码是:

import my_bayes as kNN datingDataMat,datingLabels = kNN.file2matrix('train.txt')

错误是:

ValueError: could not convert string to float: XX`

1 个答案:

答案 0 :(得分:0)

该行:

returnMat[index,:] =['XX','XY','XZ']

将所有矩阵值设置为字符串。