# coding=utf-8
from numpy import *
import matplotlib.pyplot as plt
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[-1]))
return dataMat,labelMat
#最小二乘法,ws=(X.T*X).I*X.T*Y
def standRegres(xArr,yArr):
xMat=mat(xArr)
yMat=mat(yArr).T
xTx=xMat.T*xMat
if linalg.det(xTx)==0.0:#当不能求逆矩阵的时候,就返回
return
ws=xTx.I*(xMat.T*yMat)
return ws
def linear():
xArr,yArr=loadDataSet("ex0.txt")
ws=standRegres(xArr,yArr)
xMat=mat(xArr)
yMat=mat(yArr)
yHat=xMat*ws
fig=plt.figure()
ax=fig.add_subplot(111)
ax.scatter(xMat[:,1].flatten().A[0],yMat.T[:,0].flatten().A[0])
xCopy=xMat.copy()
xCopy.sort(0)
yHat=xCopy*ws
ax.plot(xCopy[:,1],yHat)
plt.show()
return
使用最小二乘法来求解ws系数,ws=(X.T*X).I*X.T*Y