Python实现
import numpy
def loadDataSet(fileName):
numFeat=len(open(fileName).readline().split('\t'))-1
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[i]))
return dataMat,labelMat
def standRegres(xArr,yArr):
xMat=mat(xArr)
yMat=mat(yArr).T
xTx=xMat.T*xMat
if linalg.det(xTx)==0:
print("This matrix is singular,cannot do inverse")
return
ws=xTx.I*(xMat.T*yMat)
return ws