一、线性回归
# -*- coding: utf-8 -*-
from numpy import *
def loadData(fileName):
numFeat = len(open(fileName).readline().split('\t')) - 1 #通过这样的划分得到特征数
#print "numFeat = " , numFeat
dataMat = [] ; labelMat = []
fr = open(fileName)
for line in fr.readlines():
lineArr = []
curLine = line.strip().split('\t') #把一行划分为三个部分
#print "curLine = " , curLine
for i in range(numFeat):
lineArr.append(float(curLine[i])) #把每行的前两个加入特征集里
dataMat.append(lineArr)#加入特征集
labelMat.append(float(curLine[-1])) #加入标签里
return dataMat,labelMat
def standRegress(xArr,yArr):
xMat = mat(xArr)
yMat = mat(yArr).T
xTx = xMat.T * xMat
if linalg.det(xTx) == 0.0 :
print "this matrix is singular , cannot do inverse "
return
ws = xTx.I * (xMat.T * yMat) #结论:最小二乘法求最优解
return ws
进行回归预测:
# -*- coding: utf-8 -*-
import regression
from numpy import *
xArr , yArr = regression.loadData('ex0.txt') #获取数据集
ws = regression.standRegress(xArr,yArr) #训练回归系数
print ws
xMat = mat(xArr) #构造横坐标集
yMat = mat(yArr)#构建纵坐标集
yHat = xMat*ws #构造预测y
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xMat[:,1].flatten().A[0] , yMat.T[:,0].flatten().A[0]) #散点图
#这里选择第二个特征是因为,第一个特征全是1,没有价值
xCopy = xMat.copy()
xCopy.sort(0) #升序排序防止点混乱
yHat = xCopy*ws
ax.plot(xCopy[:,1],yHat)
plt.show()
yHat = xMat*ws
print corrcoef(yHat.T , yMat)
这里的线性回归为 : hX = ws[0] + ws[1]*X1 , 令X0 = 1
预测效果:
[[ 3.00774324]
[ 1.69532264]]
[[ 1. 0.98647356]
[ 0.98647356 1. ]]
‘
二、局部线性加权回归
def lwlr(testPoint , xArr , yArr , k = 1.0):
xMat = mat(xArr) ; yMat = mat(yArr).T
m = shape(xMat)[0]
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)
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 = zeros(m)
for i in range(m):
yHat[i] = lwlr(testArr[i],xArr,yArr,k)
return yHat
测试:
# -*- coding: utf-8 -*-
import regression
from numpy import *
xArr , yArr = regression.loadData('ex0.txt') #获取数据集
print yArr[0] #实际值
#得到所有数据集的点估计 , 预测值
print regression.lwlr(xArr[0],xArr,yArr,1.0)
print regression.lwlr(xArr[0],xArr,yArr,0.001)
print regression.lwlr(xArr[0],xArr,yArr,0.003)
3.176513
[[ 3.12204471]]
[[ 3.20175729]]
[[ 3.20200665]]
图形化处理:
# -*- coding: utf-8 -*-
import regression
from numpy import *
xArr , yArr = regression.loadData('ex0.txt') #获取数据集
print yArr[0] #实际值
#得到所有数据集的点估计 , 预测值
print regression.lwlr(xArr[0],xArr,yArr,1.0)
print regression.lwlr(xArr[0],xArr,yArr,0.001)
print regression.lwlr(xArr[0],xArr,yArr,0.003)
yHat = regression.lwlrTest(xArr,xArr,yArr,0.03) #修改此处的参数可以得到不同的模型
xMat = mat(xArr)
srtInd = xMat[:,1].argsort(0)
xSort = xMat[srtInd][:,0,:]
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(xSort[:,1],yHat[srtInd])
ax.scatter(xMat[:,1].flatten().A[0],mat(yArr).T.flatten().A[0] , s = 2 )
plt.show()
k=1
k=0.01
k=0.003
k=0.001
从更改系数k可以看出,当k=1时拟合出来很正规的线条,和回归一致。
当k=0.01的时候有着非常好的拟合,可以发现潜在价值
但是从k=0.003已经容纳了太多的噪声,导致出现了过拟合
如果想看到过拟合的情况就再小点,就是下面这个图了