其他分享
首页 > 其他分享> > 【线性回归】读取txt

【线性回归】读取txt

作者:互联网

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('\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])

结果:

标签:读取,curLine,labelMat,线性,dataMat,txt,1.000000,append
来源: https://www.cnblogs.com/xiximayou/p/12628846.html