## 2012年9月7日 星期五

### [ ML In Action ] Predicting numeric values : regression - Linear regression (2)

Problem :

Locally weighted linear regression :

1. def lwlr(testPoint,xArr,yArr,k=1.0):
2.     xMat = mat(xArr); yMat = mat(yArr).T
3.     m = shape(xMat)[0]
4.     weights = mat(eye((m)))
5.     for j in range(m):                      #next 2 lines create weights matrix
6.         diffMat = testPoint - xMat[j,:]     #
7.         weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
8.     xTx = xMat.T * (weights * xMat)
9.     if linalg.det(xTx) == 0.0:
10.         print "This matrix is singular, cannot do inverse"
11.         return
12.     ws = xTx.I * (xMat.T * (weights * yMat))
13.     return testPoint * ws

>>> import regression
>>> xArr, yArr = regression.loadDataSet('ex0.txt') # 載入 training data
>>> regression.lwlr(xArr[0], xArr, yArr) # 預測 xArr[0] 點的 y 值
matrix([[ 3.12204471]])

1. def lwlrTest(testArr,xArr,yArr,k=1.0):  #loops over all the data points and applies lwlr to each one
2.     m = shape(testArr)[0]
3.     yHat = zeros(m)
4.     for i in range(m):
5.         yHat[i] = lwlr(testArr[i],xArr,yArr,k)
6.     return yHat

1. def lwlrWeight(xArr,yArr,k=1.0):
2.     xMat = mat(xArr); yMat = mat(yArr).T
3.     def _w(testPoint):
4.         m = shape(xMat)[0]
5.         weights = mat(eye((m)))
6.         for j in range(m):                      #next 2 lines create weights matrix
7.             diffMat = testPoint - xMat[j,:]     #
8.             weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
9.         xTx = xMat.T * (weights * xMat)
10.         if linalg.det(xTx) == 0.0:
11.             print "This matrix is singular, cannot do inverse"
12.             return
13.         ws = xTx.I * (xMat.T * (weights * yMat))
14.         return testPoint * ws
15.     return _w

>>> wf = regression.lwlrWeight(xArr, yArr)
>>> wf(xArr[0]) # 預測 xArr[0] 點的 y 值
matrix([[ 3.12204471]])

1. def drawLWLRChart(xArr, yArr, lwlrWS):
2.     xMat = mat(xArr)
3.     yMat = mat(yArr)
4.
5.     import matplotlib.pyplot as plt
6.     fig = plt.figure()
8.     ax.scatter(xMat[:,1].flatten().A[0], yMat.T[:,0].flatten().A[0])
9.     xCopy = xMat.copy()
10.     xCopy.sort(0)
11.     yHat = zeros([200,1])
12.     m = shape(xMat)[0]
13.     for i in range(m):
14.         yHat[i][0] = lwlrWS(xCopy[i])
15.     ax.plot(xCopy[:,1], yHat)
16.     plt.show()

>>> wf = regression.lwlrWeight(xArr, yArr)
>>> regression.drawLWLRChart(xArr, yArr, wf)

>>> wf = regression.lwlrWeight(xArr, yArr, 0.01) # 使用 k 值 = 0.01 (預設是1)
>>> regression.drawLWLRChart(xArr, yArr, wf)

## 關於我自己

Where there is a will, there is a way!