-
Notifications
You must be signed in to change notification settings - Fork 0
/
linreg.py
55 lines (40 loc) · 1.59 KB
/
linreg.py
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
import numpy as np
from numpy import linalg
import matplotlib.pyplot as plt
class LinearRegression:
def __init__(self, x, y, regressors=None):
# linear model:
# y_i = xT_i * beta + eps_i
# Create two column vectors with the observed data
self.independent_var = np.array(x).reshape(-1, 1)
self.dependent_var = np.array(y).reshape(-1, 1)
self.regressors = [lambda x: 1, lambda x: x] if regressors is None else regressors
self.x = np.hstack([
np.vectorize(f)(self.independent_var)
for f in self.regressors
])
def fit(self):
# solve ordinary least squares
y = self.dependent_var
self.coeffs = linalg.inv(self.x.T @ self.x) @ self.x.T @ y
self.estimates = self.x @ self.coeffs
# R^2 is defined as the ratio between "explained" variance and "total" variance
self.r_square = np.sum((self.estimates - y.mean())**2) /np.sum((y - y.mean())**2)
def show(self, ax=None):
if ax is None:
fig, ax = plt.subplots()
ax.scatter(self.independent_var, self.dependent_var)
p = np.linspace(min(self.independent_var), max(self.independent_var), 100)
q = sum(
beta * np.vectorize(f)(p)
for beta, f in zip(self.coeffs, self.regressors)
)
ax.plot(p, q, "-r")
plt.show()
def predict(self, x):
return sum(
beta * f(x)
for beta, f in zip(self.coeffs, self.regressors)
)
def get_coeffs(self):
return self.coeffs.flatten().tolist()