作者:大学城贝岗彩虹屋屋公寓 | 来源:互联网 | 2023-10-16 20:02
txt中部分数据如下:
1.000000 0.067732 3.176513
1.000000 0.427810 3.816464
1.000000 0.995731 4.550095
1.000000 0.738336 4.256571
1.000000 0.981083 4.560815
1.000000 0.526171 3.929515
1.000000 0.378887 3.526170
1.000000 0.033859 3.156393
1.000000 0.132791 3.110301
1.000000 0.138306 3.149813
读取数据:
from numpy import *
import numpy as np
def loadDataSet(fileName): #general function to parse tab -delimited floats
numFeat = len(open(fileName).readline().split(‘ ‘)) - 1 #get number of fields
dataMat = []
labelMat = []
fr = open(fileName)
for line in fr.readlines():
lineArr =[]
curLine = line.strip().split(‘ ‘)
for i in range(numFeat):
lineArr.append(float(curLine[i]))
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
return dataMat,labelMat
xArr,yArr=loadDataSet("ex0.txt")
print(xArr[:10])
print(yArr[:10])
结果: