-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression.py
47 lines (36 loc) · 1.22 KB
/
regression.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
import numpy as np
def linear_regression(data_x, data_y):
n1 = data_x.size
n2 = np.sum(data_x)
n3 = np.sum(np.square(data_x))
n4 = np.sum(data_y)
n5 = 0
for i in range(data_x.size):
n5 += (data_x[i] * data_y[i])
b = (n4 + (-n1 / n2) * n5) / (n2 + (-n1 / n2) * n3)
a = (n4 - n2 * b) / n1
return [a, b]
def test_linear_regression():
"""
Example:
Data_x = 2, 4, 6, 8
Data_y = 2, 11, 28, 40
b = -12.5
a = 6.55
Note:
In case of non linear models like y = a * e ^ (bx)
We have to linearize it to obtain a function to use with linear regression
So, by taking ln() of the two sides we get: ln(y) = ln(a) + bx
We can then use linear regression with each y = ln(y) and x = ln(x)
"""
print("Testing Linear Regression:")
data_x = np.array([])
data_y = np.array([])
n = int(input("Enter the number of points: "))
for i in range(n):
data_x = np.append(data_x, float(input("X[{}]: ".format(i))))
for i in range(n):
data_y = np.append(data_y, float(input("Y[{}]: ".format(i))))
a, b = linear_regression(data_x, data_y)
print("Y = {}X + {}".format(b, a))
test_linear_regression()