-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultid_linear_regression_2d.py
67 lines (41 loc) · 1.18 KB
/
multid_linear_regression_2d.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
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import matplotlib as mplt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
X = []
Y = []
for line in open("./data/data_2d.csv"):
x0, x1, y = line.split(",")
X.append([float(x0), float(x1), 1])
Y.append(float(y))
#print(X)
#print(Y)
#Transforming the lists to numpy arrays
X = np.array(X)
Y = np.array(Y)
#Solve the multi-d linear regression using the explicit formulas
W = np.linalg.inv(X.T.dot(X)).dot(X.T.dot(Y))
print(W)
#Predicting Y[0], the first row's value of y
#Y = WX
#Y[0] = WX.Transpose[0]
#Means that Y[0] is the actual Dot product of X[0] with W[0]
X0 = X[0]
Y0 = (X0 * W).sum()
print(Y0)
#Predicting value of W using np.linalg.solve
W0 = np.linalg.solve(X.T.dot(X), X.T.dot(Y))
print(W0)
print(mplt.__version__)
# Gives same result as W
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,0], X[:,1], Y)
plt.show()
#Let 's check how good our model is by computing the Rsquare
Yhat = X.dot(W)
SSres = (Y - Yhat).dot(Y - Yhat)
SStot = (Y - Y.mean()).dot(Y - Y.mean())
R2 = 1 - SSres/SStot
print("R-square:", R2)