-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLinearRegression.py
97 lines (74 loc) · 2.76 KB
/
LinearRegression.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
import sys
import csv
import uuid
#************ReadingInput************
unique_filename = str(uuid.uuid4().hex)
args = sys.argv
file = open(args[2], newline='')
reader = csv.reader(file)
data = [row for row in reader]
learning_rate = float(args[4])
threshold_rate = float(args[6])
initial, gradient_list, weight_list, x_values, y_actual = [0], [], [], [], []
grad_list = {}
for i in range(len(data[0])):
grad_list[i] = float(0)
for i in range(0, len(data[0])):
gradient_list.append(initial)
weight_list = {}
for i in range(len(data[0])):
weight_list[i] = float(0)
for j in range(0, len(data)):
val = [1]
val2, c = [], 0
for i in data[j]:
c+=1
if c == len(data[0]):
val2.append(float(i))
break
val.append(float(i))
y_actual.append(val2)
x_values.append(val)
l = len(data[0]) - 1
y_original = {}
for j in range(len(data)):
y_original[j] = float(data[j][l])
iteration = 0
stopping = []
with open(unique_filename+'.csv', 'w', newline = '')as csvfile:
fieldname = ['iteration_number', 'weight0', 'weight1', 'weight2', 'sum_of_squared_errors']
thewriter = csv.writer(csvfile, delimiter = ',')
while True:
print_list = []
y_predict = []
for i in range(len(x_values)):
row = 0
for j in range(len(x_values[0])):
row = row + weight_list[j] * x_values[i][j]
y_predict.append(row)
#****************Calculting SSE************************
error = [y_original[i] - y_predict[i] for i in range(len(data))]
sse = [i*i for i in error]
print_list.append(iteration)
for i in range(len(data[0])):
print_list.append(round(weight_list[i], 4))
print_list.append(round(sum(sse), 4))
thewriter.writerow(print_list)
stopping.append(sum(sse))
#**************Gradient Computation***********************
for j in range(len(data[0])):
gg = []
for i in range(len(data)):
gg.append(x_values[i][j] * error[i])
grad_list[j] = sum(gg)
for i in range(len(data[0])):
weight_list[i] = weight_list[i] + (learning_rate*grad_list[i])
#******************StopIteration*******************************
if((iteration>0) and (stopping[iteration - 1] - stopping[iteration] < threshold_rate)):
break
iteration+=1
#*****************PrintToConsole*********************
with open(unique_filename+'.csv') as f:
curobj = csv.reader(f)
for _ in curobj:
print(', '.join(_))