#coding=utf-8 from numpy import * def loadDataSet(fileName): numFeat=len(open(fileName).readline().split('\t'))-1 dataMat=[];labelMat=[] fr=open(fileName) for line in fr.readlines(): lineArr=[] currentLine=line.strip().split('\t') for i in range(numFeat): lineArr.append(float(currentLine[i])) dataMat.append(lineArr) labelMat.append(float(currentLine[-1])) return dataMat,labelMat def standRegres(xArr,yArr): xMat=mat(xArr);yMat=mat(yArr).T xTx=xMat.T*xMat if linalg.det(xTx)==0.0: print "This matrix is singular,cannot do inverse" return ws=xTx.I*(xMat.T*yMat) return ws xArr,yArr=loadDataSet('ex0.txt') ws=standRegres(xArr,yArr) print ws xMat=mat(xArr) yMat=mat(yArr) yHat=xMat*ws import matplotlib.pyplot as plt 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()