forked from Purusottamdas/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwi00174_ECA.py
290 lines (187 loc) · 8.69 KB
/
mwi00174_ECA.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import random
#
# NAME:Oarabile Mwiya
# ID :201700174.
# ECA
# VectorAdd() returns a vector obtained by adding two vectors,
#which are parameters of the function
# result is a vector
def VectorAdd(a,b):
#Check equality of the vectors
assert len(a)==len(b)
#Initialise an empty vector list
newVector = []
#Iterate with respect to the vector length and add corresponding values
for i in range(len(a)):
v = a[i] + b[i]
newVector.append(v)
return newVector
# performs vector subtraction on two vectors and returns a
# vector
def VectorSub(a,b):
#check if length of vectors are equal
assert len(a)==len(b)
#Initialize an empty vector list
newVector = []
#Iterate the indices wrt to vector length and subtract corresponding values
for i in range(len(a)):
v = a[i] - b[i]
newVector.append(v)
return newVector
# Implement this: VectorMult() performs vector multiplication and returns a scalar
# (number)
def VectorMult(a,b):
#compare length equality of vector a and b
assert len(a)==len(b)
total = 0
#Iterate the vector indices wrt to the vector length and multiply corresponding values
for i in range(len(a)):
v = a[i] * b[i]
total = total + v
return total
# DO NOT MODIFY THIS FUNCTION
def argMax(scores):
"""
Returns the key with the highest value.
"""
if len(scores) == 0: return None
all = scores.items()
values = [x[1] for x in all]
maxIndex = values.index(max(values))
return all[maxIndex][0]
#MulticlassPerceptronLearning Function
def MulticlassPerceptronLearning(trainingvectors):
#Initalize weight
weights={'A':[0,0,0],'B':[0,0,0],'C':[0,0,0]}
#Set threshold to zero
threshold = 0
#Initialize scores of class(A,B,C respectively )
scoreA = 0
scoreB = 0
scoreC = 0
#Check if selected apple is bad
Badapple = True
#Feature Vector for apples
VectorApples = ""
#Declare and initialize variable iteration
iteration = 0
#Run the loop for a thousand iterations and check if a Bad apple is found
while(Badapple == True and iteration < 1000):
#Initialize empty list for classification
classification = []
#Increment variable iteration by 1 and ouput each iteration at each interval
iteration += 1
print "\nIteration Number:",iteration
#Iterate apples in the trainingvectors using for loop
for AppleVector in trainingvectors:
#Distiguish apart classless features
appleFeatures = [AppleVector[0], AppleVector[1]]
#Instance of the current feature vector
VectorApples = AppleVector[2]
#Threshold is appended to the feature vector
appleFeatures.append(1)
# Feature vectors mutplied with correspong weight vectors
scoreA = VectorMult(appleFeatures, weights['A'])
scoreB = VectorMult(appleFeatures, weights['B'])
scoreC = VectorMult(appleFeatures, weights['C'])
#Scores stored in a dictionary
scores = {'A': scoreA, 'B': scoreB, 'C': scoreC}
#Using percept identify class with highest score
MaximumClass = argMax(scores)
#Check if The highest score correspond with the Current class
if(MaximumClass != VectorApples):
#Subtract Apple Feature Vector From Incorrect Class Vector and output desired results
print "Bad Apple Detectad : ",AppleVector
bad = VectorSub(weights[MaximumClass],appleFeatures)
weights[MaximumClass] = bad
#Add Apple Feature Vector To Correct Class Vector
Good = VectorAdd(weights[VectorApples],appleFeatures)
weights[VectorApples] = Good
#append bad apples to classification list
classification.append("Bad")
#If its highest score classify it a good apple in list then transcend to next iteration
else:
classification.append("Good")
print "\AFTER ITERATION NUMBER:",iteration
print weights
# Check If Bad Apple Was Found
if "Bad" in classification:
Badapple = True
else:
Badapple = False
print "\n\nFinal W:",weights
return weights
#LoadFile function to load file
def LoadFile(fileName):
dataFile=open(fileName,"r")
#Initalize empty apples list
Apples = []
#Intialize empty firstlist variable
firstlist = []
#Initialize empty secondlist variable
secondlist = []
#start from the first value in tha data file
next(dataFile)
#Iterate line by line the data txt file
for line in dataFile:
#Our data file cointains list of apples in terms of(color ,size and class) thus use them to split data
#Use split to convert each corresponding line into an array
lineSplit = line.split()
#Split color from the data text and store in array..
color = float(lineSplit[0])
#split size from the data text and store in array..
size = float(lineSplit[1])
#Append the split features into apples empty list ..
Apples.append([color,size,lineSplit[2]])
#Random number seed generator
random.seed(2)
#Randomise apples
random.shuffle(Apples)
# system must read in the data file, split such that 70% of apples are used for training and 30% for testing
#Extract first 84apples which is 70% for training
firstlist = Apples[:84]
#Extract last 84apples which is 70% for training
secondlist = Apples[84:]
#Return list
return firstlist,secondlist
#Evaluate Function(Interpretation of classified apples)..
def Evaluate(weights,appleVectors):
numapples=len(appleVectors)
numcorrect=0
#Initialize scores of class(A,B,C respectively )
scoreA = 0
scoreB = 0
scoreC = 0
print "\n\nAPPLES CLASSIFICATION"
#Iterate apples in apple set
for SetApples in appleVectors:
# Extract cost and size
appleFeatures = [SetApples[0], SetApples[1]]
# Set vector to apples class
VectorApples = SetApples[2]
#Threshold appended to vector
appleFeatures.append(1)
print "\n\n Apple Being Classified:",SetApples
# Feature vectors mutplied with correspong weight vectors
scoreA = VectorMult(appleFeatures, weights['A'])
scoreB = VectorMult(appleFeatures, weights['B'])
scoreC = VectorMult(appleFeatures, weights['C'])
#Scores stored in a dictionary
scores = {'A': scoreA, 'B': scoreB, 'C': scoreC}
# Determine Class With Max Score
MaximumClass = argMax(scores)
print "Maximum Class Score : ",MaximumClass
#Check if max class equals the current apple class and iterate if condition met
if(MaximumClass == VectorApples):
numcorrect+=1
return round(float(numcorrect)/numapples*100,2)
# DO NOT MODIFY THIS FUNCTION
def RunExperiment():
training,testing=LoadFile("data.txt")
w=MulticlassPerceptronLearning(training)
score=Evaluate(w,testing)
print "Evaluation Score:",score
RunExperiment()
#Performance of Machine Learning model****
# The model has an evaluation score of 97.22 which is much closer to a perfect score (100) which derives
#that the model can clearly classify apples as bad or good apples based on the color , size and class.