Update LinearRegression_1.py
This commit is contained in:
1 parent
75a4123e9c
commit
47574bd853
1 file changed
+39
-23
@@ -10,6 +10,7 @@ class LinearRegressionLS(LinearRegression):
|
||||
"""
|
||||
solve linear regression problem via least squares
|
||||
"""
|
||||
|
||||
X: np.array
|
||||
Y: np.array
|
||||
w: np.array
|
||||
@@ -21,19 +22,19 @@ class LinearRegressionLS(LinearRegression):
|
||||
self.w = np.random.rand(2)
|
||||
|
||||
def homogeneous(self, xlist):
|
||||
""" build homogeneous coordinates """
|
||||
"""build homogeneous coordinates"""
|
||||
raise NotImplementedError
|
||||
|
||||
def linout(self, xlist):
|
||||
""" linear output for given data """
|
||||
"""linear output for given data"""
|
||||
raise NotImplementedError
|
||||
|
||||
def loss_sq(self, X, Y):
|
||||
""" loss function: (half) sum of square errors """
|
||||
"""loss function: (half) sum of square errors"""
|
||||
raise NotImplementedError
|
||||
|
||||
def solve(self, lr, nepoch):
|
||||
""" form normal equation """
|
||||
"""form normal equation"""
|
||||
XtX = np.dot(self.X.T, self.X)
|
||||
XtY = np.dot(self.X.T, self.Y)
|
||||
# print(XtX.shape, XtY.shape)
|
||||
@@ -46,6 +47,7 @@ class LinearRegressionGD1(LinearRegression):
|
||||
solve linear regression problem via gradient descent,
|
||||
using single weight vector
|
||||
"""
|
||||
|
||||
X: np.array
|
||||
Y: np.array
|
||||
w: np.array
|
||||
@@ -57,19 +59,20 @@ class LinearRegressionGD1(LinearRegression):
|
||||
self.w = np.random.rand(2)
|
||||
|
||||
def homogeneous(self, xlist):
|
||||
""" build homogeneous coordinates """
|
||||
"""build homogeneous coordinates"""
|
||||
raise NotImplementedError
|
||||
|
||||
def linout(self, xlist):
|
||||
""" linear output for given data """
|
||||
"""linear output for given data"""
|
||||
raise NotImplementedError
|
||||
|
||||
def loss_sq(self, X, Y):
|
||||
""" loss function: (half) sum of square errors """
|
||||
"""loss function: (half) sum of square errors"""
|
||||
raise NotImplementedError
|
||||
|
||||
def gd(self, lr):
|
||||
""" gradient descent update """
|
||||
"""gradient descent update"""
|
||||
|
||||
def gradient(Y_hat, Y, X):
|
||||
return np.sum((Y_hat - Y) * X, axis=0)
|
||||
|
||||
@@ -79,10 +82,10 @@ class LinearRegressionGD1(LinearRegression):
|
||||
self.w -= lr * grad
|
||||
|
||||
def solve(self, lr, nepoch):
|
||||
""" iterative solver """
|
||||
"""iterative solver"""
|
||||
for epoch in range(num_epochs):
|
||||
self.gd(lr)
|
||||
print(f'epoch {epoch + 1}, loss {self.loss_sq(self.X, self.Y)}')
|
||||
print(f"epoch {epoch + 1}, loss {self.loss_sq(self.X, self.Y)}")
|
||||
|
||||
|
||||
class LinearRegressionGD2(LinearRegression):
|
||||
@@ -99,20 +102,20 @@ class LinearRegressionGD2(LinearRegression):
|
||||
self.b = np.min(ylist)
|
||||
|
||||
def linout(self, xlist):
|
||||
""" linear output for given data """
|
||||
"""linear output for given data"""
|
||||
raise NotImplementedError
|
||||
|
||||
def loss_sq(self, X, Y):
|
||||
""" loss function: (half) sum of square errors """
|
||||
"""loss function: (half) sum of square errors"""
|
||||
raise NotImplementedError
|
||||
|
||||
def gd(self, lr):
|
||||
""" gradient descent update """
|
||||
"""gradient descent update"""
|
||||
|
||||
def gradient(Y_hat, Y, X):
|
||||
return np.array(
|
||||
[np.sum((Y_hat - Y) * X, axis=0),
|
||||
np.sum((Y_hat - Y), axis=0)
|
||||
])
|
||||
[np.sum((Y_hat - Y) * X, axis=0), np.sum((Y_hat - Y), axis=0)]
|
||||
)
|
||||
|
||||
Y_hat = self.linout(self.X)
|
||||
# print(Y_hat)
|
||||
@@ -121,27 +124,40 @@ class LinearRegressionGD2(LinearRegression):
|
||||
self.b -= lr * grad[1]
|
||||
|
||||
def solve(self, lr, nepoch):
|
||||
""" iterative solver """
|
||||
"""iterative solver"""
|
||||
for epoch in range(num_epochs):
|
||||
self.gd(lr)
|
||||
print(f'epoch {epoch + 1}, loss {self.loss_sq(self.X, self.Y)}')
|
||||
print(f"epoch {epoch + 1}, loss {self.loss_sq(self.X, self.Y)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
hp = [14213, 13448, 13870, 16192, 16415, 21501, 25910, 24866, 28981, 32926, 36741, 40974]
|
||||
hp = [x / 10000. for x in hp]
|
||||
hp = [
|
||||
14213,
|
||||
13448,
|
||||
13870,
|
||||
16192,
|
||||
16415,
|
||||
21501,
|
||||
25910,
|
||||
24866,
|
||||
28981,
|
||||
32926,
|
||||
36741,
|
||||
40974,
|
||||
]
|
||||
hp = [x / 10000.0 for x in hp]
|
||||
|
||||
lr = 0.001
|
||||
num_epochs = 10
|
||||
|
||||
ls = LinearRegressionLS(hp)
|
||||
ls.solve(lr, num_epochs)
|
||||
print(f'next prediction (LS): {ls.linout([len(hp)])}')
|
||||
print(f"next prediction (LS): {ls.linout([len(hp)])}")
|
||||
|
||||
gd1 = LinearRegressionGD1(hp)
|
||||
gd1.solve(lr, num_epochs)
|
||||
print(f'next prediction year (GD1): {gd1.linout([len(hp)])}')
|
||||
print(f"next prediction year (GD1): {gd1.linout([len(hp)])}")
|
||||
|
||||
gd2 = LinearRegressionGD1(hp)
|
||||
gd2.solve(lr, num_epochs)
|
||||
print(f'next prediction year (GD2): {gd2.linout([len(hp)])}')
|
||||
print(f"next prediction year (GD2): {gd2.linout([len(hp)])}")
|
||||
Reference in new issue
Block a user