Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit afb2513

Browse files
committedNov 25, 2019
Adding main function
1 parent ee3ee30 commit afb2513

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
 

‎machine_learning/linear discriminant analysis.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,168 @@ def accuracy(actual_y: list, predicted_y: list) -> float:
182182
# all data and multiplied by 100
183183
percentage = (correct / len(actual_y)) * 100
184184
return percentage
185+
186+
187+
# Main Function
188+
def main():
189+
""" This function starts execution phase """
190+
191+
while True:
192+
193+
print(" Linear Discriminant Analysis ".center(100, "*"))
194+
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")
197+
198+
# Trying to get number of classes
199+
n_classes = 0
200+
while True:
201+
try:
202+
user_input = int(input("Enter the number of classes (Data Groupings): "))
203+
if user_input > 0:
204+
n_classes = user_input
205+
break
206+
else:
207+
print("Your entered value is {} , Number of classes should be positive!".format(user_input))
208+
continue
209+
except ValueError:
210+
print("Your entered value is not numerical!")
211+
212+
print("-" * 100)
213+
214+
std_dev = 1.0 # Default value for standard deviation of dataset
215+
# Trying to get the value of standard deviation
216+
while True:
217+
try:
218+
user_sd = float(input("Enter the value of standard deviation"
219+
"(Default value is 1.0 for all classes): ") or "1.0")
220+
if user_sd >= 0.0:
221+
std_dev = user_sd
222+
break
223+
else:
224+
print("Your entered value is {}, Standard deviation should not be negative!".format(user_sd))
225+
continue
226+
except ValueError:
227+
print("Your entered value is not numerical!")
228+
229+
print("-" * 100)
230+
231+
# Trying to get number of instances in classes and theirs means to generate dataset
232+
counts = [] # An empty list to store instance counts of classes in dataset
233+
for i in range(n_classes):
234+
while True:
235+
try:
236+
user_count = int(input("Enter The number of instances for class_{}: ".format(i + 1)))
237+
if user_count > 0:
238+
counts.append(user_count)
239+
break
240+
else:
241+
print("Your entered value is {}, Number of instances should be positive!".format(user_count))
242+
continue
243+
except ValueError:
244+
print("Your entered value is not numerical!")
245+
246+
print("-" * 100)
247+
248+
user_means = [] # An empty list to store values of user-entered means of classes
249+
for a in range(n_classes):
250+
while True:
251+
try:
252+
user_mean = float(input("Enter the value of mean for class_{}: ".format(a + 1)))
253+
if isinstance(user_mean, float):
254+
user_means.append(user_mean)
255+
break
256+
else:
257+
print("Your entered value is {}, And this is not valid!".format(user_mean))
258+
259+
except ValueError:
260+
print("Your entered value is not numerical!")
261+
262+
print("-" * 100)
263+
264+
print("Standard deviation: ", std_dev)
265+
266+
# print out the number of instances in classes in separated line
267+
for b in range(len(counts)):
268+
print("Number of instances in class_{} is: {}".format(b + 1, counts[b]))
269+
270+
print("-" * 100)
271+
272+
# print out mean values of classes separated line
273+
for c in range(len(user_means)):
274+
print("Mean of class_{} is: {}".format(c + 1, user_means[c]))
275+
276+
print("-" * 100)
277+
278+
# Generating training dataset drawn from gaussian distribution
279+
x = [] # An empty list to store generated values of gaussian distribution
280+
# for loop iterates over number of classes
281+
for j in range(n_classes):
282+
# appending return values of 'Normal_gen' function to 'x' list
283+
x.append(Normal_gen(user_means[j], std_dev, counts[j]))
284+
print("Generated Normal Distribution: \n", x)
285+
286+
print("-" * 100)
287+
288+
# Generating Ys to detecting corresponding classes
289+
y = Y_gen(n_classes, counts)
290+
print("Generated Corresponding Ys: \n", y)
291+
292+
print("-" * 100)
293+
294+
# Calculating the value of actual mean for each class
295+
actual_means = [] # An empty list to store value of actual means
296+
# for loop iterates over number of classes(data groupings)
297+
for k in range(n_classes):
298+
# appending return values of 'mean_calc' function to 'actual_means' list
299+
actual_means.append(mean_calc(counts[k], x[k]))
300+
# for loop iterates over number of elements in 'actual_means' list and print out them in separated line
301+
for d in range(len(actual_means)):
302+
print("Actual(Real) mean of class_{} is: {}".format(d + 1, actual_means[d]))
303+
304+
print("-" * 100)
305+
306+
# Calculating the value of probabilities for each class
307+
probabilities = [] # An empty list to store values of probabilities for each class
308+
# # for loop iterates over number of classes(data groupings)
309+
for l in range(n_classes):
310+
# appending return values of 'prob_calc' function to 'probabilities' list
311+
probabilities.append(prob_calc(counts[l], sum(counts)))
312+
# for loop iterates over number of elements in 'probabilities' list and print out them in separated line
313+
for e in range(len(probabilities)):
314+
print("Probability of class_{} is: {}".format(e + 1, probabilities[e]))
315+
316+
print("-" * 100)
317+
318+
# Calculating the values of variance for each class
319+
variance = var_calc(x, actual_means, sum(counts))
320+
print("Variance: ", variance)
321+
322+
print("-" * 100)
323+
324+
# Predicting Y values
325+
# storing predicted Y values in 'pre_indexes' variable
326+
pre_indexes = predict(x, actual_means, variance, probabilities)
327+
328+
print("-" * 100)
329+
330+
# Calculating Accuracy of the model
331+
print("Accuracy: ", accuracy(y, pre_indexes))
332+
print("-" * 100)
333+
print(" DONE ".center(100, "+"))
334+
335+
command = input("Press any key to restart and 'q' for quit: ")
336+
if command.lower() == "q":
337+
print("\n" + "GoodBye!".center(100, "-") + "\n")
338+
break
339+
else:
340+
if name == "nt": # Related to Windows OS
341+
system("cls")
342+
continue
343+
else: # Related to Mac OSX and Linux OS
344+
system("clear")
345+
continue
346+
347+
348+
if __name__ == '__main__':
349+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.