提交 687237aa 编写于 作者: H hathackerwang

ch07

上级 231f8fa1
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 15:05:20 2018
@author: Administrator
"""
#前向逐步回归
#@eps:每次迭代需要调整的步长
def stageWise(xArr,yArr,eps=0.01,numIt=100):
xMat=mat(xArr);yMat=mat(yArr)
yMean=mean(yMat,0)
yMat=yMat-yMean
#将特征标准化处理为均值为0,方差为1
xMat=regularize(xMat)
m,n=shape(xMat)
#将每次迭代中得到的回归系数存入矩阵
returnMat=zeros((numIt,m))
ws=zeros((n,1));wsTest=ws.copy();wsMat=ws.copy()
for i in range(numIt):
print(ws.T)
#初始化最小误差为正无穷
lowestError=inf;
for j in range(n):
#对每个特征的系数执行增加和减少eps*sign操作
for sign in [-1,1]:
wsTest=ws.copy()
wsTest[j]+=eps*sign
#变化后计算相应预测值
yTest=xMat*wsTest
#保存最小的误差以及对应的回归系数
rssE=rssError(yMat.A,yTest.A)
if rssE<lowestError:
lowestError=rssE
wsMat=wsTest
ws=wsMat.copy()
returnMat[i,:]=ws
return returnMat
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 15:03:07 2018
@author: Administrator
"""
#局部加权线性回归
def rssError(yArr,yHatArr):
#返回平方误差和
return ((yArr-yHatArr)**2).sum()
def lwlr(testPoint,xArr,yArr,k=1.0):
'''
#每个测试点赋予权重系数
@testPoint:测试点
@xArr:样本数据矩阵
@yArr:样本对应的原始值
@k:用户定义的参数,决定权重的大小,默认1.0
'''
#转为矩阵形式
xMat=mat(xArr);yMat=mat(yArr).T
#样本数
m=shape(xMat)[0]
#初始化权重矩阵为m*m的单位阵
weights=mat(eye((m)))
#循环遍历各个样本
for j in range(m):
#计算预测点与该样本的偏差
diffMat=testPoint-xMat[j,:]
#根据偏差利用高斯核函数赋予该样本相应的权重
weights[j,j]=exp(diffMat*diffMat.T/(-2.0*k**2))
#将权重矩阵应用到公式中
xTx=xMat.T*(weights*xMat)
#计算行列式值是否为0,即确定是否可逆
if linalg.det(xTx)==0.0:
print('This matrix is singular,cannot do inverse')
return
#根据公式计算回归系数
ws=xTx.I*(xMat.T*(weights*yMat))
#计算测试点的预测值
return testPoint*ws
#测试集进行预测
def lwlrTest(testArr,xArr,yArr,k=1.0):
#测试集样本数
m=shape(testArr)[0]
#测试集预测结果保存在yHat列表中
yHat=zeros(m)
#遍历每一个测试样本
for i in range(m):
#计算预测值
yHat[i]=lwlr(testArr[i],xArr,yArr,k)
return yHat
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册