-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.py
121 lines (89 loc) · 3.02 KB
/
student.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
# -*- coding: utf-8 -*-
import sys
import csv
import uuid
"""
Loading input values from command prompt
"""
#"""
#loading our csv file
args=sys.argv
#loading our csv file
file=open(args[2],newline='')
reader=csv.reader(file)
data=[row for row in reader]
#initializing learning rate
learning_rate= float(args[4])
#initializing threshold rate
threshold_rate= float(args[6])
#initializing list for x values
x_values = [ ]
#initializing list for y values
y_actual = [ ]
#finding number of rows in our dataset
rows = len(data)
#finding number of columns in our dataset
columns = len(data[0])
#counter : to notify when we reach last column in every row
counter = 0
#seperating dependent and independent variables
for i in range(0 , rows) :
independent_variable = [1] #initializing x1 == 1
dependent_variable,counter=[],0
for j in data[i] :
counter += 1
if counter == columns :
dependent_variable.append(float(j))
break
independent_variable.append(float(j))
y_actual.append(dependent_variable)
x_values.append(independent_variable)
#initializing a list for weights
#weight_list = []
#initializing for weights
grad_list={ }
# intializing evry column with a 0.000
for i in range(columns):
grad_list[i]=float(0)
weight_list={}
# intializing evry column with a 0.000
for i in range(columns):
weight_list[i]=float(0)
#print(weight_list)
l=columns-1
y_original={}
for j in range(rows):
y_original[j]=float(data[j][l])
#print(y_original)
iteration=0
stopping_criteria=[]
while True:
final_output=[] #initializing list for priniting
hypothesis =[] #initializing list for hypothesis
for i in range(len(x_values)):
row = 0
for j in range(len(x_values[0])):
row += weight_list[j]*x_values[i][j]
hypothesis.append(row)
error = [y_original[i]-hypothesis[i] for i in range(rows) ]
sum_of_squared_error =[error * error for error in error]
# for output printing
final_output.append(iteration)
print(iteration , end = ',')
for i in range(len(data[0])):
print(round(weight_list[i],4), end =',')
final_output.append(round(weight_list[i],4))
final_output.append(round(sum(sum_of_squared_error),4))
print(round(sum(sum_of_squared_error),4))
stopping_criteria.append(sum(sum_of_squared_error))
for j in range(len(data[0])):
a =[]
for i in range(len(data)):
a.append(x_values[i][j]*error[i])
grad_list[j]=sum(a)
for i in range(len(data[0])):
weight_list[i]=weight_list[i]+(learning_rate*grad_list[i])
#for coming out of loop or stopping criteria
if(iteration>0 and stopping_criteria[iteration-1]-stopping_criteria[iteration] < threshold_rate):
break
iteration+=1