赞
踩
import csvimport numpy as np def loadDataSet(filename): # 读取数据 with open(filename) as f: dataMat = [] labelMat = [] f_csv = csv.reader(f) ## 用csv读取直接是个list headers = next(f_csv) for row in f_csv: dataMat.append([float(row[0]), float(row[1])]) labelMat.append(float(row[2])) f.close() return dataMat, labelMat def loadData(filename): # 读取数据 dataMat=[] labelMat=[] fr=open(filename) next(fr) # 忽略第一行 for line in fr.readlines(): lineArr=line.strip().split(',') dataMat.append([lineArr[0],lineArr[1]]) labelMat.append(lineArr[2]) return dataMat,labelMat # 返回数据特征和数据类别 if __name__ == '__main__': dataMat,labelMat = loadData('test_data.csv') print(dataMat,labelMat)