Skip to content

Commit 33b8679

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
fixup! Format Python code with psf/black push
1 parent 0d5d0a7 commit 33b8679

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

Diff for: machine_learning/linear_discriminant_analysis.py

+48-30
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ def calculate_variance(items: list, means: list, total_count: int) -> float:
102102
:return: calculated variance for considered dataset
103103
"""
104104

105-
squared_diff = (
106-
[]
107-
) # An empty list to store all squared differences
105+
squared_diff = [] # An empty list to store all squared differences
108106
n_classes = len(means) # Number of classes in dataSet
109107

110108
# for loo iterates over number of elements in items
@@ -119,7 +117,9 @@ def calculate_variance(items: list, means: list, total_count: int) -> float:
119117

120118

121119
# 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:
123123
""" This function predicts new indexes(groups for our data)
124124
:param x_items: a list containing all items(gaussian distribution of all classes)
125125
: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:
130130

131131
results = (
132132
[]
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
134134
# for loop iterates over number of elements in list
135135
for i in range(len(x_items)):
136136
# for loop iterates over number of inner items of each element
137137
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
139139
# for loop iterates over number of classes we have in our dataset
140140
for k in range(len(x_items)):
141141
# 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+
)
144147
# appending discriminant values of each item to 'results' list
145148
results.append(temp)
146149

@@ -156,7 +159,7 @@ def accuracy(actual_y: list, predicted_y: list) -> float:
156159
:param predicted_y: a list containing predicted Y values generated by 'predict_y_values' function
157160
:return: percentage of accuracy
158161
"""
159-
correct = 0 # initial value for number of correct predictions
162+
correct = 0 # initial value for number of correct predictions
160163
# for loop iterates over one element of each list at a time (zip mode)
161164
for i, j in zip(actual_y, predicted_y):
162165
# if actual Y value equals to predicted Y value
@@ -176,19 +179,25 @@ def main():
176179

177180
print(" Linear Discriminant Analysis ".center(100, "*"))
178181
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+
)
181186

182187
# Trying to get number of classes
183188
n_classes = 0
184189
while True:
185190
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+
)
187194
if user_input > 0:
188195
n_classes = user_input
189196
break
190197
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+
)
192201
continue
193202
except ValueError:
194203
print("Your entered value is not numerical!")
@@ -199,32 +208,41 @@ def main():
199208
# Trying to get the value of standard deviation
200209
while True:
201210
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+
)
204218
if user_sd >= 0.0:
205219
std_dev = user_sd
206220
break
207221
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+
)
209225
continue
210226
except ValueError:
211227
print("Your entered value is not numerical!")
212228

213229
print("-" * 100)
214230

215231
# 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
219233
for i in range(n_classes):
220234
while True:
221235
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+
)
223239
if user_count > 0:
224240
counts.append(user_count)
225241
break
226242
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+
)
228246
continue
229247
except ValueError:
230248
print("Your entered value is not numerical!")
@@ -237,12 +255,16 @@ def main():
237255
for a in range(n_classes):
238256
while True:
239257
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+
)
241261
if isinstance(user_mean, float):
242262
user_means.append(user_mean)
243263
break
244264
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+
)
246268

247269
except ValueError:
248270
print("Your entered value is not numerical!")
@@ -264,9 +286,7 @@ def main():
264286
print("-" * 100)
265287

266288
# 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
270290
# for loop iterates over number of classes
271291
for j in range(n_classes):
272292
# appending return values of 'gaussian_distribution' function to 'x' list
@@ -282,9 +302,7 @@ def main():
282302
print("-" * 100)
283303

284304
# 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
288306
# for loop iterates over number of classes(data groupings)
289307
for k in range(n_classes):
290308
# appending return values of 'calculate_mean' function to 'actual_means' list
@@ -339,5 +357,5 @@ def main():
339357
continue
340358

341359

342-
if __name__ == '__main__':
360+
if __name__ == "__main__":
343361
main()

0 commit comments

Comments
 (0)