Created
November 13, 2018 08:43
-
-
Save Eyongkevin/a9588f9dba80a330abcaf910f7c0923f to your computer and use it in GitHub Desktop.
This implement a linear weighted linear regression method to overcome underfetting in linear regression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lwlr(testPoint, xArr, yArr, k=0.1): | |
""" | |
Generate an estimate for any point in the x space | |
Parameters | |
---------- | |
testPoint: float | |
point in the x space | |
xArr : List | |
Our data | |
yArr: List | |
our predicted value | |
k: float | |
user-defined constant that determine how much to weight the point | |
Returns | |
------- | |
ws : float | |
regression weight | |
""" | |
xMat = mat(xArr); yMat = mat(yArr).T | |
# Create diagonal matrix | |
m = shape(xMat)[0] | |
weights = mat(eye((m))) | |
# Populate weights with exponentially decaying values | |
for j in range(m): | |
diffMat = testPoint - xMat[j,:] | |
weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2)) | |
# find extimate for testpoint | |
xTwx = xMat.T * (weights * xMat) | |
if linalg.det(xTwx) == 0.0: | |
print("This matrix is singular, cannot do inverse") | |
return | |
ws = xTwx.I * (xMat.T * (weights*yMat)) | |
return testPoint*ws | |
def lwlrTest(testArr, xArr, yArr, k=1.0): | |
""" | |
Generate an estimate for any point in the x space | |
Parameters | |
---------- | |
testPoint: float | |
point in the x space | |
xArr : List | |
Our data | |
yArr: List | |
our predicted value | |
k: float | |
user-defined constant that determine how much to weight the point | |
Returns | |
------- | |
ws : float | |
regression weight | |
""" | |
# Get arbituary yHat to be of same length as test data | |
m = shape(testArr)[0] | |
yHat = zeros(m) | |
# Replace each entry by its weight | |
for i in range(m): | |
yHat[i] = lwlr(testArr[i], xArr, yArr, k) | |
return yHat | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment