Skip to content

Commit b0c8a2f

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
fixup! Format Python code with psf/black push
1 parent afb2513 commit b0c8a2f

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

Diff for: machine_learning/linear discriminant analysis.py

+61-21
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
# importing modules
4343
from random import gauss
4444
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
4646

4747

4848
# Making training dataset drawn from a gaussian distribution
@@ -139,23 +139,28 @@ def predict(x_items: list, means: list, variance: float, probabilities: list) ->
139139
:return: a list containing predicted Y values
140140
"""
141141

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
143145
# for loop iterates over number of elements in list
144146
for i in range(len(x_items)):
145147
# for loop iterates over number of inner items of each element
146148
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
148150
# for loop iterates over number of classes we have in our dataset
149151
for k in range(len(x_items)):
150152
# 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+
)
153158
# appending discriminant values of each item to 'results' list
154159
results.append(temp)
155160

156161
print("Generated Discriminants: \n", results)
157162

158-
predicted_index = [] # An empty list to store predicted indexes
163+
predicted_index = [] # An empty list to store predicted indexes
159164
# for loop iterates over elements in 'results'
160165
for l in results:
161166
# 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:
171176
:param predicted_y: a list containing predicted Y values generated by 'predict' function
172177
:return: percentage of accuracy
173178
"""
174-
correct = 0 # initial value for number of correct predictions
179+
correct = 0 # initial value for number of correct predictions
175180
# for loop iterates over one element of each list at a time (zip mode)
176181
for i, j in zip(actual_y, predicted_y):
177182
# if actual Y value equals to predicted Y value
@@ -192,19 +197,27 @@ def main():
192197

193198
print(" Linear Discriminant Analysis ".center(100, "*"))
194199
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+
)
197204

198205
# Trying to get number of classes
199206
n_classes = 0
200207
while True:
201208
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+
)
203212
if user_input > 0:
204213
n_classes = user_input
205214
break
206215
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+
)
208221
continue
209222
except ValueError:
210223
print("Your entered value is not numerical!")
@@ -215,13 +228,22 @@ def main():
215228
# Trying to get the value of standard deviation
216229
while True:
217230
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+
)
220238
if user_sd >= 0.0:
221239
std_dev = user_sd
222240
break
223241
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+
)
225247
continue
226248
except ValueError:
227249
print("Your entered value is not numerical!")
@@ -233,28 +255,44 @@ def main():
233255
for i in range(n_classes):
234256
while True:
235257
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+
)
237263
if user_count > 0:
238264
counts.append(user_count)
239265
break
240266
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+
)
242272
continue
243273
except ValueError:
244274
print("Your entered value is not numerical!")
245275

246276
print("-" * 100)
247277

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
249281
for a in range(n_classes):
250282
while True:
251283
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+
)
253287
if isinstance(user_mean, float):
254288
user_means.append(user_mean)
255289
break
256290
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+
)
258296

259297
except ValueError:
260298
print("Your entered value is not numerical!")
@@ -304,7 +342,9 @@ def main():
304342
print("-" * 100)
305343

306344
# 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
308348
# # for loop iterates over number of classes(data groupings)
309349
for l in range(n_classes):
310350
# appending return values of 'prob_calc' function to 'probabilities' list
@@ -345,5 +385,5 @@ def main():
345385
continue
346386

347387

348-
if __name__ == '__main__':
388+
if __name__ == "__main__":
349389
main()

0 commit comments

Comments
 (0)