# 回归

## 平方误差确定回归系数

$$\sum_{i=1}^m (y_i – x_i^T w)^2)$$

$$w = (X^T X){-1}XTy$$

from numpy import *
import matplotlib.pyplot as plt

dataMat = []; labelMat = []
fr = open(fileName)
lineArr =[]
curLine = line.strip().split('\t')
for i in range(numFeat):
lineArr.append(float(curLine[i]))
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
return dataMat,labelMat

def standRegres(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

def regression1():
xMat = mat(xArr)
yMat = mat(yArr)
ws = standRegres(xArr, yArr)
fig = plt.figure()
print xMat[:, 1].flatten()
print yMat.T[:, 0].flatten()
ax.scatter(xMat[:, 1].flatten(), yMat.T[:, 0].flatten().A[0])
xCopy = xMat.copy()
xCopy.sort(0)
yHat = xCopy * ws
ax.plot(xCopy[:, 1], yHat)
plt.show()

if __name__ == "__main__":
regression1()


## 局部加权线性回归

$$w = (X^T WX){-1}XTWy$$

$$w(i, j) = exp (\frac {|x^{(i)} – x|} {-2k^2})$$

### 算法实现

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):                      #next 2 lines create weights matrix
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):  #loops over all the data points and applies lwlr to each one
m = shape(testArr)[0]
yHat = zeros(m)
for i in range(m):
yHat[i] = lwlr(testArr[i],xArr,yArr,k)
return yHat

def regression2():
yhat = lwlrTest(xArr, xArr, yArr, 0.01)
fig = plt.figure()
xMat = mat(xArr)
srtInd = xMat[:, 1].argsort(0)
xSort = xMat[srtInd][:, 0, :]
ax.plot(xSort[:, 1], yhat[srtInd])
ax.scatter(xMat[:, -1].flatten(), mat(yArr).T.flatten().A[0], s=2, c="red")
plt.show()


Original: https://www.cnblogs.com/coder2012/p/4601295.html
Author: cococo点点
Title: 回归

(0)