-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (138 loc) · 3.75 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# CHANGE THESE #
"""
points for regression
in form [x,y]
"""
data = [[0, 0], [1, 1], [2, 2], [3, 3]]
"""
2 = linear
3 = quadratic
4 = cubic
"""
power = 2
"""
how many decimal points of accuracy you want
higher numbers may lead to longer run times
"""
dec = 5
"""
choose lower number for more accurate regressions, but slower run times
choose higher number for less accurate regressions, but faster run times
"""
a = 0.001
"""
chooses regression with highest r^2 value
won't go higher than higher power entered above
will include other types of regressions later on
"""
smart = False
# DONT CHANGE BELOW #
# whether the data fits or not
fit = 0
# training set size
m = len(data)
# theta
theta = []
# temp theta for simultaneous update
tempTheta = []
# old theta
oldTheta = []
# finals
fTheta = []
# adds items in to the list
if smart == False:
for i in range(power):
theta.append(0)
tempTheta.append(0)
oldTheta.append(1)
# hypothesis
def hypL(theta, x):
n = 0
for i in range(power):
n += theta[i] * x ** i
return n
# gradiant decent formula
def grad(theta, z, data):
n = 0
for i in range(m):
n += (hypL(theta, data[i][0]) - data[i][1]) * data[i][0] ** z
return theta[z] - a / m * n
def res(theta, data):
predict = []
mean = 0
RSS = 0
TSS = 0
for i in data:
temp = 0
for z in range(len(theta)):
temp += theta[z] * i[0] ** z
predict.append(temp)
for i in range(len(predict)):
mean += predict[i]
mean /= len(predict)
for i in range(len(data)):
RSS += (data[i][1] - predict[i]) ** 2
TSS += (data[i][1] - mean) ** 2
return 1 - RSS / TSS
if smart == True:
fTheta.append([0, 0, 0])
fTheta.append([0, 0, 0, 0])
fTheta.append([0, 0, 0, 0, 0])
for power in range(2, 5):
for i in range(power):
theta.append(0)
tempTheta.append(0)
oldTheta.append(1)
while fit != power:
fit = 0
for i in range(power):
if round(theta[i], dec) == round(oldTheta[i], dec):
fit += 1
oldTheta[i] = theta[i]
tempTheta[i] = grad(theta, i, data)
for i in range(power):
theta[i] = tempTheta[i]
fTheta[power - 2][0] = res(theta, data)
for i in range(power):
fTheta[power - 2][i + 1] = theta[i]
theta = theta[:-power]
tempTheta = tempTheta[:-power]
oldTheta = oldTheta[:-power]
if fTheta[0][0] >= fTheta[1][0]:
if fTheta[0][0] >= fTheta[2][0]:
for i in range(2):
theta.append(fTheta[0][i + 1])
num = 2
else:
for i in range(4):
theta.append(fTheta[2][i + 1])
num = 3
else:
for i in range(3):
theta.append(fTheta[1][i + 1])
num = 4
print("y = ", end="")
for i in range(num):
if i + 1 != num:
print(str(round(theta[i], dec)) + " x^" + str(i) + " + ", end="")
else:
print(str(round(theta[i], dec)) + " x^" + str(i))
print("r^2 = " + str(res(theta, data)))
else:
while fit != power:
fit = 0
for i in range(power):
if round(theta[i], dec) == round(oldTheta[i], dec):
fit += 1
oldTheta[i] = theta[i]
tempTheta[i] = grad(theta, i, data)
for i in range(power):
theta[i] = tempTheta[i]
# printing
print("y = ", end="")
for i in range(power):
if i + 1 != power:
print(str(round(theta[i], dec)) + " x^" + str(i) + " + ", end="")
else:
print(str(round(theta[i], dec)) + " x^" + str(i))
print("r^2 = " + str(res(theta, data)))