1.工具, numpy 包, 源自 machine learning in action
2.代码:
from numpy import *
def file2matrix(filename):
fr = open(filename)
#get all lines
lines = fr.readlines()
length = len(lines)
#length rows and 3 columns
returnMat = zeros((length, 3))
#for labels
classLabelVector = []
index = 0
for line in lines:
#get rid of spaces etc.
line = line.strip()
#split line according /t and return a list
listFromLine = line.split('\t')
returnMat[index, :] = listFromLine[0:3]
#get the last element from a list
classLabelVector.append(listFromLine[-1])
index += 1
return returnMat, classLabelVector