@@ -102,9 +102,7 @@ def calculate_variance(items: list, means: list, total_count: int) -> float:
102
102
:return: calculated variance for considered dataset
103
103
"""
104
104
105
- squared_diff = (
106
- []
107
- ) # An empty list to store all squared differences
105
+ squared_diff = [] # An empty list to store all squared differences
108
106
n_classes = len (means ) # Number of classes in dataSet
109
107
110
108
# for loo iterates over number of elements in items
@@ -119,7 +117,9 @@ def calculate_variance(items: list, means: list, total_count: int) -> float:
119
117
120
118
121
119
# Making predictions
122
- def predict_y_values (x_items : list , means : list , variance : float , probabilities : list ) -> list :
120
+ def predict_y_values (
121
+ x_items : list , means : list , variance : float , probabilities : list
122
+ ) -> list :
123
123
""" This function predicts new indexes(groups for our data)
124
124
:param x_items: a list containing all items(gaussian distribution of all classes)
125
125
:param means: a list containing real mean values of each class
@@ -130,17 +130,20 @@ def predict_y_values(x_items: list, means: list, variance: float, probabilities:
130
130
131
131
results = (
132
132
[]
133
- ) # An empty list to store generated discriminant values of all items in dataset for each class
133
+ ) # An empty list to store generated discriminant values of all items in dataset for each class
134
134
# for loop iterates over number of elements in list
135
135
for i in range (len (x_items )):
136
136
# for loop iterates over number of inner items of each element
137
137
for j in range (len (x_items [i ])):
138
- temp = [] # to store all discriminant values of each item as a list
138
+ temp = [] # to store all discriminant values of each item as a list
139
139
# for loop iterates over number of classes we have in our dataset
140
140
for k in range (len (x_items )):
141
141
# appending values of discriminants for each class to 'temp' list
142
- temp .append (x_items [i ][j ] * (means [k ] / variance ) - (means [k ] ** 2 / (2 * variance )) +
143
- log (probabilities [k ]))
142
+ temp .append (
143
+ x_items [i ][j ] * (means [k ] / variance )
144
+ - (means [k ] ** 2 / (2 * variance ))
145
+ + log (probabilities [k ])
146
+ )
144
147
# appending discriminant values of each item to 'results' list
145
148
results .append (temp )
146
149
@@ -156,7 +159,7 @@ def accuracy(actual_y: list, predicted_y: list) -> float:
156
159
:param predicted_y: a list containing predicted Y values generated by 'predict_y_values' function
157
160
:return: percentage of accuracy
158
161
"""
159
- correct = 0 # initial value for number of correct predictions
162
+ correct = 0 # initial value for number of correct predictions
160
163
# for loop iterates over one element of each list at a time (zip mode)
161
164
for i , j in zip (actual_y , predicted_y ):
162
165
# if actual Y value equals to predicted Y value
@@ -176,19 +179,25 @@ def main():
176
179
177
180
print (" Linear Discriminant Analysis " .center (100 , "*" ))
178
181
print ("*" * 100 , "\n " )
179
- print ("First of all we should specify the number of classes that \n "
180
- "we want to generate as training dataset" )
182
+ print (
183
+ "First of all we should specify the number of classes that \n "
184
+ "we want to generate as training dataset"
185
+ )
181
186
182
187
# Trying to get number of classes
183
188
n_classes = 0
184
189
while True :
185
190
try :
186
- user_input = int (input ("Enter the number of classes (Data Groupings): " ))
191
+ user_input = int (
192
+ input ("Enter the number of classes (Data Groupings): " )
193
+ )
187
194
if user_input > 0 :
188
195
n_classes = user_input
189
196
break
190
197
else :
191
- print (f"Your entered value is { user_input } , Number of classes should be positive!" )
198
+ print (
199
+ f"Your entered value is { user_input } , Number of classes should be positive!"
200
+ )
192
201
continue
193
202
except ValueError :
194
203
print ("Your entered value is not numerical!" )
@@ -199,32 +208,41 @@ def main():
199
208
# Trying to get the value of standard deviation
200
209
while True :
201
210
try :
202
- user_sd = float (input ("Enter the value of standard deviation"
203
- "(Default value is 1.0 for all classes): " ) or "1.0" )
211
+ user_sd = float (
212
+ input (
213
+ "Enter the value of standard deviation"
214
+ "(Default value is 1.0 for all classes): "
215
+ )
216
+ or "1.0"
217
+ )
204
218
if user_sd >= 0.0 :
205
219
std_dev = user_sd
206
220
break
207
221
else :
208
- print (f"Your entered value is { user_sd } , Standard deviation should not be negative!" )
222
+ print (
223
+ f"Your entered value is { user_sd } , Standard deviation should not be negative!"
224
+ )
209
225
continue
210
226
except ValueError :
211
227
print ("Your entered value is not numerical!" )
212
228
213
229
print ("-" * 100 )
214
230
215
231
# Trying to get number of instances in classes and theirs means to generate dataset
216
- counts = (
217
- []
218
- ) # An empty list to store instance counts of classes in dataset
232
+ counts = [] # An empty list to store instance counts of classes in dataset
219
233
for i in range (n_classes ):
220
234
while True :
221
235
try :
222
- user_count = int (input (f"Enter The number of instances for class_{ i + 1 } : " ))
236
+ user_count = int (
237
+ input (f"Enter The number of instances for class_{ i + 1 } : " )
238
+ )
223
239
if user_count > 0 :
224
240
counts .append (user_count )
225
241
break
226
242
else :
227
- print (f"Your entered value is { user_count } , Number of instances should be positive!" )
243
+ print (
244
+ f"Your entered value is { user_count } , Number of instances should be positive!"
245
+ )
228
246
continue
229
247
except ValueError :
230
248
print ("Your entered value is not numerical!" )
@@ -237,12 +255,16 @@ def main():
237
255
for a in range (n_classes ):
238
256
while True :
239
257
try :
240
- user_mean = float (input (f"Enter the value of mean for class_{ a + 1 } : " ))
258
+ user_mean = float (
259
+ input (f"Enter the value of mean for class_{ a + 1 } : " )
260
+ )
241
261
if isinstance (user_mean , float ):
242
262
user_means .append (user_mean )
243
263
break
244
264
else :
245
- print (f"Your entered value is { user_mean } , And this is not valid!" )
265
+ print (
266
+ f"Your entered value is { user_mean } , And this is not valid!"
267
+ )
246
268
247
269
except ValueError :
248
270
print ("Your entered value is not numerical!" )
@@ -264,9 +286,7 @@ def main():
264
286
print ("-" * 100 )
265
287
266
288
# Generating training dataset drawn from gaussian distribution
267
- x = (
268
- []
269
- ) # An empty list to store generated values of gaussian distribution
289
+ x = [] # An empty list to store generated values of gaussian distribution
270
290
# for loop iterates over number of classes
271
291
for j in range (n_classes ):
272
292
# appending return values of 'gaussian_distribution' function to 'x' list
@@ -282,9 +302,7 @@ def main():
282
302
print ("-" * 100 )
283
303
284
304
# Calculating the value of actual mean for each class
285
- actual_means = (
286
- []
287
- ) # An empty list to store value of actual means
305
+ actual_means = [] # An empty list to store value of actual means
288
306
# for loop iterates over number of classes(data groupings)
289
307
for k in range (n_classes ):
290
308
# appending return values of 'calculate_mean' function to 'actual_means' list
@@ -339,5 +357,5 @@ def main():
339
357
continue
340
358
341
359
342
- if __name__ == ' __main__' :
360
+ if __name__ == " __main__" :
343
361
main ()
0 commit comments