-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
121 lines (104 loc) · 3.34 KB
/
index.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
import random
import pandas as pd
import sys
threshold = 0.2
def learning():
data = pd.read_csv(sys.argv[2])
#data = data.iloc[:100]
dataList = data.values.tolist()
numberOfColumns = len(data.columns)
numberOfRows = len(dataList)
#print("Rows: ", numberOfRows)
learningRate = 0.1
flage = True
weights = []
for i in range(0, numberOfColumns-1):
weights.append(round(random.uniform(-0.5, 0.5), 3))
numberOfInputs = len(weights)
while(flage == True):
flage = False
for row in dataList:
value = 0
for i in range(0, numberOfInputs):
value = value + (row[i]*weights[i])
if(value >= threshold): # if perceptoron excited
output = 1
else:
output = 0
index = numberOfColumns-1
if(output != row[index]):
for i in range(0, numberOfColumns-1):
weights[i] = round(
weights[i]+(learningRate*(row[index]-output)*row[i]), 3)
# to round the out to 1 decimal point
# print(weights)
flage = True
# print(weights)
weightsFile = open('weights.txt', 'w')
for i in range(0, numberOfInputs):
weightsFile.write('{0}\n'.format(weights[i]))
weightsFile.close()
def testing():
data = pd.read_csv(sys.argv[2])
#data = data.iloc[300:]
dataList = data.values.tolist()
numberOfColumns = len(data.columns)
numberOfRows = len(dataList)
#print("Rows: ", numberOfRows)
weights = []
weightsFile = open('weights.txt', 'r')
for w in weightsFile.readlines():
weights.append(float(w))
numberOfInputs = len(weights)
columns = []
for i in range(0, numberOfInputs):
columns.append('x'+str(i+1))
columns.append('Actual')
columns.append('Predicted')
out = pd.DataFrame(columns=columns)
for i in range(0, numberOfRows):
values = []
for j in range(0, numberOfColumns):
values.append(data.iloc[i, j])
actual = 0
for k in range(0, numberOfInputs):
actual += weights[k]*dataList[i][k]
if actual < threshold:
actual = 0
else:
actual = 1
values.append(actual)
out.loc[len(out)] = values
tp = 0
tn = 0
fp = 0
fn = 0
for i in range(0, numberOfRows):
if (out.iloc[i, -1] == out.iloc[i, -2]) & (out.iloc[i, -1] == 1):
tp += 1
elif(out.iloc[i, -1] == out.iloc[i, -2]) & (out.iloc[i, -1] == 0):
tn += 1
elif(out.iloc[i, -1] != out.iloc[i, -2]) & (out.iloc[i, -1] == 1):
fp += 1
elif(out.iloc[i, -1] != out.iloc[i, -2]) & (out.iloc[i, -1] == 0):
fn += 1
precision = tp/(tp+fp)*100
recall = tp/(tp+fn)*100
accuracy = (tp+tn)/(tp+tn+fp+fn)*100
results = open('Accuracy.txt', 'w')
results.write("Precision: ")
results.write('%d\n' % precision)
results.write("Recall: ")
results.write('%d\n' % recall)
results.write("Accuracy: ")
results.write('%d\n' % accuracy)
results.close()
print(precision)
print(recall)
print(accuracy)
out.to_csv("results.csv", index=False)
if __name__ == '__main__':
if '-learning' in sys.argv:
learning()
if '-testing' in sys.argv:
testing()