【线性回归】读取txt

2020-08-26 10:32:13 浏览数 (1)

txt中部分数据如下:

代码语言:javascript复制
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

读取数据:

代码语言:javascript复制
from numpy import *
import numpy as np

def loadDataSet(fileName):      #general function to parse tab -delimited floats
    numFeat = len(open(fileName).readline().split('t')) - 1 #get number of fields 
    dataMat = []
    labelMat = []
    fr = open(fileName)
    for line in fr.readlines():
        lineArr =[]
        curLine = line.strip().split('t')
        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])

结果:

txt

0 人点赞