Skip to content

Commit

Permalink
增加前向逐步回归
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Cherish committed Dec 3, 2017
1 parent ceba349 commit 4fe915d
Showing 1 changed file with 59 additions and 22 deletions.
81 changes: 59 additions & 22 deletions Regression/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def regularize(xMat, yMat):
Website:
http://www.cuijiahua.com/
Modify:
2017-11-23
2017-12-03
"""
inxMat = xMat.copy() #数据拷贝
inyMat = yMat.copy()
Expand All @@ -123,44 +123,81 @@ def regularize(xMat, yMat):
inxMat = (inxMat - inMeans) / inVar #数据减去均值除以方差实现标准化
return inxMat, inyMat

def rssError(yArr,yHatArr):
"""
函数说明:计算平方误差
Parameters:
yArr - 预测值
yHatArr - 真实值
Returns:
Website:
http://www.cuijiahua.com/
Modify:
2017-12-03
"""
return ((yArr-yHatArr)**2).sum()

def stageWise(xArr, yArr, eps = 0.01, numIt = 100):
"""
函数说明:前向逐步线性回归
Parameters:
xArr - x数据集
yArr - y数据集
xArr - x输入数据
yArr - y预测数据
eps - 每次迭代需要调整的步长
numIt - 迭代次数
Returns:
returnMat - numIt次迭代的回归系数矩阵
Website:
http://www.cuijiahua.com/
Modify:
2017-11-23
2017-12-03
"""
xMat = np.mat(xArr); yMat = np.mat(yArr).T #数据集
yMean = np.mean(yMat, 0) #计算y的均值
yMat = yMat - yMean #
xMat = regularize(xMat)
xMat, yMat = regularize(xMat, yMat) #数据标准化
m, n = np.shape(xMat)
returnMat = np.zeros((numIt, n))
ws = np.zeros((n, 1))
returnMat = np.zeros((numIt, n)) #初始化numIt次迭代的回归系数矩阵
ws = np.zeros((n, 1)) #初始化回归系数矩阵
wsTest = ws.copy()
wsMax = ws.copy()
for i in range(numIt):
print ws.T
lowestError = inf;
for j in range(n):
for sign in [-1,1]:
for i in range(numIt): #迭代numIt次
# print(ws.T) #打印当前回归系数矩阵
lowestError = float('inf'); #正无穷
for j in range(n): #遍历每个特征的回归系数
for sign in [-1, 1]:
wsTest = ws.copy()
wsTest[j] += eps*sign
yTest = xMat*wsTest
rssE = rssError(yMat.A,yTest.A)
if rssE < lowestError:
wsTest[j] += eps * sign #微调回归系数
yTest = xMat * wsTest #计算预测值
rssE = rssError(yMat.A, yTest.A) #计算平方误差
if rssE < lowestError: #如果误差更小,则更新当前的最佳回归系数
lowestError = rssE
wsMax = wsTest
ws = wsMax.copy()
returnMat[i,:] = ws.T
returnMat[i,:] = ws.T #记录numIt次迭代的回归系数矩阵
return returnMat

if __name__ == '__main__':
def plotstageWiseMat():
"""
函数说明:绘制岭回归系数矩阵
Website:
http://www.cuijiahua.com/
Modify:
2017-12-03
"""
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
xArr, yArr = loadDataSet('abalone.txt')
stageWise(xArr, yArr)
returnMat = stageWise(xArr, yArr, 0.005, 1000)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(returnMat)
ax_title_text = ax.set_title(u'前向逐步回归:迭代次数与回归系数的关系', FontProperties = font)
ax_xlabel_text = ax.set_xlabel(u'迭代次数', FontProperties = font)
ax_ylabel_text = ax.set_ylabel(u'回归系数', FontProperties = font)
plt.setp(ax_title_text, size = 15, weight = 'bold', color = 'red')
plt.setp(ax_xlabel_text, size = 10, weight = 'bold', color = 'black')
plt.setp(ax_ylabel_text, size = 10, weight = 'bold', color = 'black')
plt.show()


if __name__ == '__main__':
plotstageWiseMat()

0 comments on commit 4fe915d

Please sign in to comment.