Zheng Jie "machine learning algorithms principles and programming Practices" study notes (seventh. Predictive technology and philosophy) 7.1 Prediction of linear systems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.