42
42
# importing modules
43
43
from random import gauss
44
44
from math import log
45
- from os import system , name # to use < clear > or < cls > commands in terminal or cmd
45
+ from os import system , name # to use < clear > or < cls > commands in terminal or cmd
46
46
47
47
48
48
# Making training dataset drawn from a gaussian distribution
@@ -139,23 +139,28 @@ def predict(x_items: list, means: list, variance: float, probabilities: list) ->
139
139
:return: a list containing predicted Y values
140
140
"""
141
141
142
- results = [] # An empty list to store generated discriminant values of all items in dataset for each class
142
+ results = (
143
+ []
144
+ ) # An empty list to store generated discriminant values of all items in dataset for each class
143
145
# for loop iterates over number of elements in list
144
146
for i in range (len (x_items )):
145
147
# for loop iterates over number of inner items of each element
146
148
for j in range (len (x_items [i ])):
147
- temp = [] # to store all discriminant values of each item as a list
149
+ temp = [] # to store all discriminant values of each item as a list
148
150
# for loop iterates over number of classes we have in our dataset
149
151
for k in range (len (x_items )):
150
152
# appending values of discriminants for each class to 'temp' list
151
- temp .append (x_items [i ][j ] * (means [k ] / variance ) - (means [k ] ** 2 / (2 * variance )) +
152
- log (probabilities [k ]))
153
+ temp .append (
154
+ x_items [i ][j ] * (means [k ] / variance )
155
+ - (means [k ] ** 2 / (2 * variance ))
156
+ + log (probabilities [k ])
157
+ )
153
158
# appending discriminant values of each item to 'results' list
154
159
results .append (temp )
155
160
156
161
print ("Generated Discriminants: \n " , results )
157
162
158
- predicted_index = [] # An empty list to store predicted indexes
163
+ predicted_index = [] # An empty list to store predicted indexes
159
164
# for loop iterates over elements in 'results'
160
165
for l in results :
161
166
# after calculating the discriminant value for each class , the class with the largest
@@ -171,7 +176,7 @@ def accuracy(actual_y: list, predicted_y: list) -> float:
171
176
:param predicted_y: a list containing predicted Y values generated by 'predict' function
172
177
:return: percentage of accuracy
173
178
"""
174
- correct = 0 # initial value for number of correct predictions
179
+ correct = 0 # initial value for number of correct predictions
175
180
# for loop iterates over one element of each list at a time (zip mode)
176
181
for i , j in zip (actual_y , predicted_y ):
177
182
# if actual Y value equals to predicted Y value
@@ -192,19 +197,27 @@ def main():
192
197
193
198
print (" Linear Discriminant Analysis " .center (100 , "*" ))
194
199
print ("*" * 100 , "\n " )
195
- print ("First of all we should specify the number of classes that \n "
196
- "we want to generate as training dataset" )
200
+ print (
201
+ "First of all we should specify the number of classes that \n "
202
+ "we want to generate as training dataset"
203
+ )
197
204
198
205
# Trying to get number of classes
199
206
n_classes = 0
200
207
while True :
201
208
try :
202
- user_input = int (input ("Enter the number of classes (Data Groupings): " ))
209
+ user_input = int (
210
+ input ("Enter the number of classes (Data Groupings): " )
211
+ )
203
212
if user_input > 0 :
204
213
n_classes = user_input
205
214
break
206
215
else :
207
- print ("Your entered value is {} , Number of classes should be positive!" .format (user_input ))
216
+ print (
217
+ "Your entered value is {} , Number of classes should be positive!" .format (
218
+ user_input
219
+ )
220
+ )
208
221
continue
209
222
except ValueError :
210
223
print ("Your entered value is not numerical!" )
@@ -215,13 +228,22 @@ def main():
215
228
# Trying to get the value of standard deviation
216
229
while True :
217
230
try :
218
- user_sd = float (input ("Enter the value of standard deviation"
219
- "(Default value is 1.0 for all classes): " ) or "1.0" )
231
+ user_sd = float (
232
+ input (
233
+ "Enter the value of standard deviation"
234
+ "(Default value is 1.0 for all classes): "
235
+ )
236
+ or "1.0"
237
+ )
220
238
if user_sd >= 0.0 :
221
239
std_dev = user_sd
222
240
break
223
241
else :
224
- print ("Your entered value is {}, Standard deviation should not be negative!" .format (user_sd ))
242
+ print (
243
+ "Your entered value is {}, Standard deviation should not be negative!" .format (
244
+ user_sd
245
+ )
246
+ )
225
247
continue
226
248
except ValueError :
227
249
print ("Your entered value is not numerical!" )
@@ -233,28 +255,44 @@ def main():
233
255
for i in range (n_classes ):
234
256
while True :
235
257
try :
236
- user_count = int (input ("Enter The number of instances for class_{}: " .format (i + 1 )))
258
+ user_count = int (
259
+ input (
260
+ "Enter The number of instances for class_{}: " .format (i + 1 )
261
+ )
262
+ )
237
263
if user_count > 0 :
238
264
counts .append (user_count )
239
265
break
240
266
else :
241
- print ("Your entered value is {}, Number of instances should be positive!" .format (user_count ))
267
+ print (
268
+ "Your entered value is {}, Number of instances should be positive!" .format (
269
+ user_count
270
+ )
271
+ )
242
272
continue
243
273
except ValueError :
244
274
print ("Your entered value is not numerical!" )
245
275
246
276
print ("-" * 100 )
247
277
248
- user_means = [] # An empty list to store values of user-entered means of classes
278
+ user_means = (
279
+ []
280
+ ) # An empty list to store values of user-entered means of classes
249
281
for a in range (n_classes ):
250
282
while True :
251
283
try :
252
- user_mean = float (input ("Enter the value of mean for class_{}: " .format (a + 1 )))
284
+ user_mean = float (
285
+ input ("Enter the value of mean for class_{}: " .format (a + 1 ))
286
+ )
253
287
if isinstance (user_mean , float ):
254
288
user_means .append (user_mean )
255
289
break
256
290
else :
257
- print ("Your entered value is {}, And this is not valid!" .format (user_mean ))
291
+ print (
292
+ "Your entered value is {}, And this is not valid!" .format (
293
+ user_mean
294
+ )
295
+ )
258
296
259
297
except ValueError :
260
298
print ("Your entered value is not numerical!" )
@@ -304,7 +342,9 @@ def main():
304
342
print ("-" * 100 )
305
343
306
344
# Calculating the value of probabilities for each class
307
- probabilities = [] # An empty list to store values of probabilities for each class
345
+ probabilities = (
346
+ []
347
+ ) # An empty list to store values of probabilities for each class
308
348
# # for loop iterates over number of classes(data groupings)
309
349
for l in range (n_classes ):
310
350
# appending return values of 'prob_calc' function to 'probabilities' list
@@ -345,5 +385,5 @@ def main():
345
385
continue
346
386
347
387
348
- if __name__ == ' __main__' :
388
+ if __name__ == " __main__" :
349
389
main ()
0 commit comments