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