如下所示:
# -*- coding: utf-8 -*-
# @ author hulei 2016-5-3
from numpy import *
import operator
from os import listdir
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# x,y=getDataSet_dz('iris.data.txt',4)
def getDataSet(filename,numberOfFeature): #将数据集读入内存
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file file.readlines()是把文件的全部内容读到内存,并解析成一个list
returnMat = zeros((numberOfLines,numberOfFeature)) #prepare matrix to return 3代表数据集中特征数目###
classLabelVector = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip() #strip() 参数为空时,默认删除空白符(包括'\n', '\r', '\t', ' ')
listFromLine = line.split(&#