运行 k-邻近树 样例中
def file2matrix(filename):
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file
returnMat = zeros((numberOfLines,3)) #prepare matrix to return
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]
classLabelVector.append(int(listFromLine[-1]))
index += 1
return returnMat,classLabelVector
调用方法是
ret,ds=file2matrix('C:\ml\machinelearninginaction\Ch02\datingTestSet.txt')
会出现 ValueError: invalid literal for int() with base 10: 'largeDoses'的错误
可以 换
ret,ds=file2matrix('C:\ml\machinelearninginaction\Ch02\datingTestSet2.txt')
也可以换 classLabelVector.append(int(listFromLine[-1]))为
classLabelVector.append(listFromLine[-1])