-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathlineRegression.py
More file actions
58 lines (49 loc) · 1.52 KB
/
lineRegression.py
File metadata and controls
58 lines (49 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from numpy import *
from utils import tool
# 线性回归
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 ridgeRegres(testPoint, xArr, yArr, lam = 0.2):
'''
岭回归,与之类似的还有前向逐步回归
'''
xMat = mat(xArr)
yMat = mat(yArr).T
yMean = mean(yMat, 0)
yMat = yMat - yMean
xMeans = mean(xMat, 0)
xVar = var(xMat, 0)
xMat = (xMat - xMeans) / xVar
xTx = xMat.T * xMat
denom = xTx + eye(shape(xMat)[1]) * lam
if linalg.det(denom) == 0.0:
print("this matrix is singular")
return
ws = denom.I * (xMat.T * yMat)
return testPoint * ws
def main():
trainingset, traininglabels = tool.file2floatMatrix('ex0.txt', 2)
yhat = lwlr(trainingset[0], trainingset, traininglabels, k = 1.0)
print(yhat)
yhat = lwlr(trainingset[0], trainingset, traininglabels, k = 0.001)
print(yhat)
trainingset, traininglabels = tool.file2floatMatrix('abalone.txt', 8)
yhat = ridgeRegres(trainingset[0], trainingset, traininglabels)
print(yhat)
if __name__ == "__main__":
main()