-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradientdist.py
159 lines (135 loc) · 5.53 KB
/
gradientdist.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
import random
import numpy as np
import sklearn as sklearn
def generateData(datapoints, features, bias, noise):
x, y, original_w = sklearn.datasets.make_regression(n_samples=datapoints, n_features=features,
n_informative=features, n_targets=1,
bias=bias, effective_rank=None, tail_strength=0.5, noise=noise,
shuffle=False, coef=True, random_state=None)
ones = np.ones([len(x), 1])
x = np.append(ones, x, 1)
w0_init = np.array([5])
original_w = np.append(w0_init, original_w)
y = y + w0_init
return x, y, original_w
def euclideanDistanceDerivative(x, y, w):
updated_w = [0] * len(w)
first_num = 1
denom = 0
for m in range(1, len(w)):
denom += w[m] ** 2
while first_num < len(w):
final_sum = 0
constant_sum = 0
for i in range(0, len(x)):
sum = 0
for j in range(0, len(w)):
sum += w[j] * x[i][j]
constant_sum += (sum - y[i]) * x[i][0]
final_sum += ((denom + 1) * (sum - y[i]) * x[i][first_num] - ((sum - y[i]) ** 2) * w[first_num])
updated_w[first_num] = 2 * final_sum / (denom + 1) ** 2
updated_w[0] = 2 * constant_sum / (denom + 1)
first_num += 1
return updated_w
def errorDerivative(x, y, w):
updated_w = [0] * len(w)
first_num = 0
while first_num < len(w):
final_sum = 0
for i in range(0, len(x)):
sum = 0
for j in range(0, len(w)):
sum += w[j] * x[i][j]
final_sum += (sum - y[i]) * x[i][first_num]
updated_w[first_num] = final_sum
first_num += 1
return updated_w
def gradientDescentDist(x, y, starting_w, alpha):
itr = 0
while 1:
threshold = 0
temp = euclideanDistanceDerivative(x, y, starting_w)
for i in range(0, len(starting_w)):
old_w = starting_w[i]
starting_w[i] = starting_w[i] - alpha * temp[i]
new_w = starting_w[i]
threshold += ((new_w - old_w) ** 2)
itr += 1
if threshold < 10 ** -6:
break
# if (itr > 100000):
# break
return starting_w
def gradientDescentSse(x, y, starting_w, alpha):
itr = 0
while 1:
threshold = 0
temp = errorDerivative(x, y, starting_w)
for i in range(0, len(starting_w)):
old_w = starting_w[i]
starting_w[i] = starting_w[i] - 2 * alpha * temp[i]
new_w = starting_w[i]
threshold += ((new_w - old_w) ** 2)
itr += 1
if threshold < 10 ** -6:
break
# if (itr > 10000):
# break
return starting_w
def maximumLikelihoodClosedForm(x, y):
x_transpose = np.transpose(x)
product_term = np.dot(x_transpose, x)
inverse_term = np.linalg.inv(product_term)
final_term = np.dot(x_transpose, y)
weight_ml = np.dot(inverse_term, final_term)
return weight_ml
def fit(size, features, noise, bias, alpha):
x, y, original_w = generateData(size, features, noise, bias)
starting_w = random.sample(range(1, 100), len(original_w))
updated_weights_dist = gradientDescentDist(x, y, starting_w, alpha)
updated_weights_sse = gradientDescentSse(x, y, starting_w, alpha)
updated_weights_ml = maximumLikelihoodClosedForm(x, y)
predcited_y_dist = [0] * len(y)
predcited_y_sse = [0] * len(y)
predicted_y_ml = [0] * len(y)
for i in range(0, len(x)):
sum_dist = 0
sum_sse = 0
sum_ml = 0
for j in range(1, len(updated_weights_dist)):
sum_dist += updated_weights_dist[j] * x[i][j]
sum_sse += updated_weights_sse[j] * x[i][j]
sum_ml += updated_weights_ml[j] * x[i][j]
predcited_y_dist[i] = updated_weights_dist[0] + sum_dist
predcited_y_sse[i] = updated_weights_sse[0] + sum_sse
predicted_y_ml[i] = updated_weights_ml[0] + sum_ml
final_value_dist = computerRsquaredValue(y, predcited_y_dist)
final_value_see = computerRsquaredValue(y, predcited_y_sse)
final_value_ml = computerRsquaredValue(y, predicted_y_ml)
print('***************DATA SIZE=',size,' FEATURES=',features,'NOISE=','LEARNING RATE=', alpha,'BIAS=',bias,'NOISE=', noise,'********************')
print('Original weights of the data set', original_w)
print('Starting weights for iterations:', starting_w)
print('Updated weights for ML:', updated_weights_ml)
print('THE R-SQUARED VALUE FOR THE ML PREDICTIONS IS : ', final_value_ml)
print('Updated weights for SSE:', updated_weights_sse)
print('THE R-SQUARED VALUE FOR THE SSE PREDICTIONS IS : ', final_value_see)
print('Updated weights for Euclidean Distance:', updated_weights_dist)
print('THE R-SQUARED VALUE FOR THE EUCLIDEAN DISTANCE PREDICTIONS IS : ', final_value_dist)
def computerRsquaredValue(original_y, predicted_y):
mean = sum(original_y) / len(original_y)
total = 0
denom = 0
for i in range(0, len(predicted_y)):
total += (predicted_y[i] - original_y[i]) ** 2
denom += (mean - original_y[i]) ** 2
final_value = 1 - (total / denom)
return final_value
def main():
sizes = [50, 100, 500, 1000, 1000]
features = [3, 10, 10, 20, 2]
learning_rates = [0.01, 0.001, 0.0001, 0.0001, 0.0001]
biases = [0.1, 0.4, 2, 5, 0]
noises = [0.2, 0.5, 4, 10, 0]
for i in range(0,len(sizes)):
fit(sizes[i], features[i], noises[i], biases[i], learning_rates[i])
main()