File tree Expand file tree Collapse file tree 1 file changed +64
-1
lines changed Expand file tree Collapse file tree 1 file changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -99,5 +99,68 @@ def plotwMat():
9999 plt .setp (ax_ylabel_text , size = 10 , weight = 'bold' , color = 'black' )
100100 plt .show ()
101101
102+
103+ def regularize (xMat , yMat ):
104+ """
105+ 函数说明:数据标准化
106+ Parameters:
107+ xMat - x数据集
108+ yMat - y数据集
109+ Returns:
110+ inxMat - 标准化后的x数据集
111+ inyMat - 标准化后的y数据集
112+ Website:
113+ http://www.cuijiahua.com/
114+ Modify:
115+ 2017-11-23
116+ """
117+ inxMat = xMat .copy () #数据拷贝
118+ inyMat = yMat .copy ()
119+ yMean = np .mean (yMat , 0 ) #行与行操作,求均值
120+ inyMat = yMat - yMean #数据减去均值
121+ inMeans = np .mean (inxMat , 0 ) #行与行操作,求均值
122+ inVar = np .var (inxMat , 0 ) #行与行操作,求方差
123+ inxMat = (inxMat - inMeans ) / inVar #数据减去均值除以方差实现标准化
124+ return inxMat , inyMat
125+
126+ def stageWise (xArr , yArr , eps = 0.01 , numIt = 100 ):
127+ """
128+ 函数说明:前向逐步线性回归
129+ Parameters:
130+ xArr - x数据集
131+ yArr - y数据集
132+ Returns:
133+
134+ Website:
135+ http://www.cuijiahua.com/
136+ Modify:
137+ 2017-11-23
138+ """
139+ xMat = np .mat (xArr ); yMat = np .mat (yArr ).T #数据集
140+ yMean = np .mean (yMat , 0 ) #计算y的均值
141+ yMat = yMat - yMean #
142+ xMat = regularize (xMat )
143+ m , n = np .shape (xMat )
144+ returnMat = np .zeros ((numIt , n ))
145+ ws = np .zeros ((n , 1 ))
146+ wsTest = ws .copy ()
147+ wsMax = ws .copy ()
148+ for i in range (numIt ):
149+ print ws .T
150+ lowestError = inf ;
151+ for j in range (n ):
152+ for sign in [- 1 ,1 ]:
153+ wsTest = ws .copy ()
154+ wsTest [j ] += eps * sign
155+ yTest = xMat * wsTest
156+ rssE = rssError (yMat .A ,yTest .A )
157+ if rssE < lowestError :
158+ lowestError = rssE
159+ wsMax = wsTest
160+ ws = wsMax .copy ()
161+ returnMat [i ,:] = ws .T
162+ return returnMat
163+
102164if __name__ == '__main__' :
103- plotwMat ()
165+ xArr , yArr = loadDataSet ('abalone.txt' )
166+ stageWise (xArr , yArr )
You can’t perform that action at this time.
0 commit comments