Zheng Jie "machine Learning algorithm principles and programming Practices" study notes (seventh. Predictive technology and philosophy) 7.3 Ridge return

Source: Internet
Author: User

7.3 Ridge return

7.3.1 Verifying multi-collinearity

7.3.2 Ridge Regression theory

7.3.3 Ridge Analysis

The judgment of 7.3.4 K value

7.3.5 Auxiliary functions

(1) Importing cubes: Loading datasets

defloaddataset (filename): Numfeat= Len (open (filename). ReadLine (). Split ('\ t'))-1#get number of fieldsDatamat =[] Labelmat=[] FR=open (filename) forLineinchfr.readlines (): Linearr=[] CurLine= Line.strip (). Split ('\ t')         forIinchRange (numfeat): Linearr.append (float (curline[i)) datamat.append (Linearr) Labelmat.append (fl Oat (curline[-1]))    returnDatamat,labelmat

(2) Standardized matrix datasets

# Standardize data sets def Normdata (Xarr,yarr):     = Mat (xarr)    = mat (yarr). T    = mean (ymat,0)    = mean (xmat,0)    = Ymat- ymean  Xvar = var (xmat,0)     = (Xmat-xmean)/xvar    return xnorm,ynorm

(3) Drawing graphics

 def  scatterplot (wmat,k): #   draw a graphic  fig = Plt.figure () ax  = Fig.add_subplot (111< Span style= "COLOR: #000000" >) Wmatt  = wmat.t m,n  = shape (Wmatt)  for  i in   xrange (m): Ax.plot (K,wmatt[i,:]) ax.annotate (  "
    feature[  " +str (i) +"   " " , XY = (0,wmatt[ i,0]), color =  " black   " ) plt.show ()  

7.3.6 Ridge Regression Implementation and K-value determination

#The first 8 columns are arr, and the post 1 column is YarrXarr,yarr = Loaddataset ('Abalone.txt') Xmat,ymat= Normdata (Xarr,yarr)#Standardize data setsKnum= 30#determine the number of iterations of KWmat = Zeros ((Knum,shape (Xmat) [1])) Klist= Zeros ((knum,1)) forIinchxrange (knum): K= Float (i)/500#The purpose of the algorithm is to determine the value of KKlist[i] = k#List of k valuesXTx = xmat.t*Xmat denom= xTx + eye (Shape (Xmat) [1]) *kifLinalg.det (denom) = = 0.0:        Print "This matrix was singular,connot do inverse"sys.exit (0) WS= LINALG.INV (denom) * (xmat.t*Ymat) wmat[i,:]=ws. TPrintKlistscatterplot (klist,klist) scatterplot (wmat,klist)

Reference: Zheng Jie "machine learning algorithms principles and programming practices" for study only

Zheng Jie "machine Learning algorithm principles and programming Practices" study notes (seventh. Predictive technology and philosophy) 7.3 Ridge return

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.