7.1.1 Regression and modern prediction
7.1.2 Least Squares
7.1.3 Code Implementation
(1) Import data
def Loaddataset (self,filename): # load DataSet X = []; Y = [] = open (filename) for in fr.readlines (): = Line.strip (). Split ('\ t') x.append (float (curline[0])) Y.append ( Float (curline[-1])) return x, y
(2) Drawing function
#(2) Drawing functiondefPlotscatter (XMAT,YMAT,A,B,PLT): Fig=plt.figure () Ax= Fig.add_subplot (111)#draw a graphic positionAx.scatter (xmat,ymat,c='Blue', marker='o')#Plot scatter plotsXmat.sort ()#sort the Xmat elementsYhat = [A.float (xi) +b forXiinchXmat]#Calculate forecast ValuesPlt.plot (Xmat,yhat,'R') plt.show ()returnYhat
(3) Main function
Xmat,ymat = Loaddataset ("Regdataset.txt")#Importing Data FilesMeanx = Mean (Xmat)#mean value of raw dataMeany = Mean (Ymat)#mean value of raw dataDX = Xmat-meanx#The difference between the elements and the mean valuesDY = Ymat-meany#The difference between the elements and the mean values#manual Calculation#sumxy = 0; SQX = 0#For i in xrange (len (dx)):#Sumxy + = double (Dx[i]) *double (Dy[i])#Sqx = double (Dx[i]) **2Sumxy= VDOT (Dx,dy)#returns the point multiplication of two vectors multiplySQX = SUM (Power (dx,2))#Square of the vector: (x-meanx) ^2#calculate slope and interceptA = sumxy/SQXB= meany-a*MeanxPrintA, b#Draw a graphicPlotscatter (XMAT,YMAT,A,B,PLT)
7.1.4 Normal Equation Group methodCode implementation of 7.1.5 normal equation set
#data Matrix, category labelsXarr,yarr = Loaddataset ("Regdataset.txt")#Importing Data Filesm= Len (Xarr)#generate x-coordinate columnsXmat = Mat (Ones (m,2))) forIinchxrange (m): Xmat[i,1] =Xarr[i]ymat= Mat (Yarr). T#Convert to y columnXTx = xmat.t*Xmatws= []#slope and intercept of a lineifLinalg.det (xTx)! = 0.0:#the determinant is not 0WS = LINALG.INV (xmat.t*xmat) * (Xmat.t*ymat)#formulas for normal equations of matrices: Inv (x.t*x) *x.t*yElse: PrintU"matrix is singular array, no inverse matrix"sys.exit (0)#Exit ProgramPrint "WS:"Ws
Source: Zheng Jie "machine Learning algorithm principles and programming practices" for study only
Zheng Jie "machine learning algorithms principles and programming Practices" study notes (seventh. Predictive technology and philosophy) 7.1 Prediction of linear systems