From 978375fa92e959e8137d3eebf48cc9185f8b53f7 Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Fri, 18 Sep 2020 19:50:31 -0500 Subject: [PATCH 1/8] Reduce complexity linear_discriminant_analysis. --- .../linear_discriminant_analysis.py | 127 +++++++++--------- 1 file changed, 62 insertions(+), 65 deletions(-) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index 22ee63a5a62b..694cfffbb432 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -44,6 +44,7 @@ from math import log from os import name, system from random import gauss, seed +from typing import TypeVar, Callable # Make a training dataset drawn from a gaussian distribution @@ -245,6 +246,38 @@ def accuracy(actual_y: list, predicted_y: list) -> float: return (correct / len(actual_y)) * 100 +num = TypeVar("num") + + +def valid_input( + input_type: Callable[[object], num], # Usually float or int + input_msg: str, + err_msg: str, + condition: Callable[[num], bool] = lambda x: True, + default: str = None, +) -> num: + """ + Ask for user value and validate that it fulfill a condition. + + :input_type: user input expected type of value + :input_msg: message to show user in the screen + :err_msg: message to show in the screen in case of error + :condition: function that represents the condition that user input is valid. + :default: Default value in case the user does not type anything + :return: user's input + """ + while True: + try: + user_input = input_type(input(input_msg).strip() or default) + if condition(user_input): + return user_input + else: + print(f"Your entered value is {user_input}. {err_msg}") + continue + except ValueError: + print("Your entered value is not numerical!") + + # Main Function def main(): """ This function starts execution phase """ @@ -254,48 +287,26 @@ def main(): print("First of all we should specify the number of classes that") print("we want to generate as training dataset") # Trying to get number of classes - n_classes = 0 - while True: - try: - user_input = int( - input("Enter the number of classes (Data Groupings): ").strip() - ) - if user_input > 0: - n_classes = user_input - break - else: - print( - f"Your entered value is {user_input} , Number of classes " - f"should be positive!" - ) - continue - except ValueError: - print("Your entered value is not numerical!") + n_classes = valid_input( + input_type=int, + condition=lambda x: x > 0, + input_msg="Enter the number of classes (Data Groupings): ", + err_msg="Number of classes should be positive!", + ) print("-" * 100) - std_dev = 1.0 # Default value for standard deviation of dataset # Trying to get the value of standard deviation - while True: - try: - user_sd = float( - input( - "Enter the value of standard deviation" - "(Default value is 1.0 for all classes): " - ).strip() - or "1.0" - ) - if user_sd >= 0.0: - std_dev = user_sd - break - else: - print( - f"Your entered value is {user_sd}, Standard deviation should " - f"not be negative!" - ) - continue - except ValueError: - print("Your entered value is not numerical!") + std_dev = valid_input( + input_type=float, + condition=lambda x: x >= 0, + input_msg=( + "Enter the value of standard deviation" + "(Default value is 1.0 for all classes): " + ), + err_msg="Standard deviation should not be negative!", + default="1.0", + ) print("-" * 100) @@ -303,38 +314,24 @@ def main(): # dataset counts = [] # An empty list to store instance counts of classes in dataset for i in range(n_classes): - while True: - try: - user_count = int( - input(f"Enter The number of instances for class_{i+1}: ") - ) - if user_count > 0: - counts.append(user_count) - break - else: - print( - f"Your entered value is {user_count}, Number of " - "instances should be positive!" - ) - continue - except ValueError: - print("Your entered value is not numerical!") + user_count = valid_input( + input_type=int, + condition=lambda x: x > 0, + input_msg=(f"Enter The number of instances for class_{i+1}: "), + err_msg="Number of instances should be positive!", + ) + counts.append(user_count) print("-" * 100) # An empty list to store values of user-entered means of classes user_means = [] for a in range(n_classes): - while True: - try: - user_mean = float( - input(f"Enter the value of mean for class_{a+1}: ") - ) - if isinstance(user_mean, float): - user_means.append(user_mean) - break - print(f"You entered an invalid value: {user_mean}") - except ValueError: - print("Your entered value is not numerical!") + user_mean = valid_input( + input_type=float, + input_msg=(f"Enter the value of mean for class_{a+1}: "), + err_msg="This is an invalid value.", + ) + user_means.append(user_mean) print("-" * 100) print("Standard deviation: ", std_dev) From 8429db68c67187433f20523627147c3368804f21 Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Sat, 19 Sep 2020 16:35:51 -0500 Subject: [PATCH 2/8] Fix whitespace --- machine_learning/linear_discriminant_analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index 694cfffbb432..012d5c2ffd25 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -250,7 +250,7 @@ def accuracy(actual_y: list, predicted_y: list) -> float: def valid_input( - input_type: Callable[[object], num], # Usually float or int + input_type: Callable[[object], num], # Usually float or int input_msg: str, err_msg: str, condition: Callable[[num], bool] = lambda x: True, From f772d459ebcb885dd10dcffc443363eb390f99e5 Mon Sep 17 00:00:00 2001 From: poloso Date: Tue, 10 Nov 2020 09:02:51 -0500 Subject: [PATCH 3/8] Update machine_learning/linear_discriminant_analysis.py Co-authored-by: Dhruv Manilawala --- machine_learning/linear_discriminant_analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index 012d5c2ffd25..bb897e8040dd 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -272,7 +272,7 @@ def valid_input( if condition(user_input): return user_input else: - print(f"Your entered value is {user_input}. {err_msg}") + print(f"{user_input}: {err_msg}") continue except ValueError: print("Your entered value is not numerical!") From 06a38a674a460c380e38c99c1289c7b4021950ac Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 10 Nov 2020 14:04:19 +0000 Subject: [PATCH 4/8] fixup! Format Python code with psf/black push --- machine_learning/linear_discriminant_analysis.py | 2 +- strings/can_string_be_rearranged_as_palindrome.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index bb897e8040dd..0143ac83e319 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -44,7 +44,7 @@ from math import log from os import name, system from random import gauss, seed -from typing import TypeVar, Callable +from typing import Callable, TypeVar # Make a training dataset drawn from a gaussian distribution diff --git a/strings/can_string_be_rearranged_as_palindrome.py b/strings/can_string_be_rearranged_as_palindrome.py index 92bc3b95b243..7fedc5877e26 100644 --- a/strings/can_string_be_rearranged_as_palindrome.py +++ b/strings/can_string_be_rearranged_as_palindrome.py @@ -8,7 +8,9 @@ # Counter is faster for long strings and non-Counter is faster for short strings. -def can_string_be_rearranged_as_palindrome_counter(input_str: str = "",) -> bool: +def can_string_be_rearranged_as_palindrome_counter( + input_str: str = "", +) -> bool: """ A Palindrome is a String that reads the same forward as it does backwards. Examples of Palindromes mom, dad, malayalam From b69d426d29d2cc2468b10dffe66e73dc0161c571 Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Tue, 10 Nov 2020 09:36:08 -0500 Subject: [PATCH 5/8] Fix format to surpass pre-commit tests --- machine_learning/linear_discriminant_analysis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index 0143ac83e319..00725a3be3da 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -2,6 +2,7 @@ Linear Discriminant Analysis + Assumptions About Data : 1. The input variables has a gaussian distribution. 2. The variance calculated for each input variables by class grouping is the From ddf4c5824c9d780ca3571793fb41a57f5775b145 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 10 Nov 2020 14:37:53 +0000 Subject: [PATCH 6/8] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3c066122a167..5490d426759a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -86,6 +86,7 @@ ## Conversions * [Binary To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_decimal.py) + * [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py) * [Decimal To Any](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_any.py) * [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py) * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py) @@ -709,6 +710,7 @@ ## Strings * [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py) * [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py) + * [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py) * [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py) * [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py) * [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py) From cd532f148874124252b5796b5280979667fb07ae Mon Sep 17 00:00:00 2001 From: poloso Date: Tue, 10 Nov 2020 15:17:49 -0500 Subject: [PATCH 7/8] Update machine_learning/linear_discriminant_analysis.py Co-authored-by: Dhruv Manilawala --- machine_learning/linear_discriminant_analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index 00725a3be3da..087a4c90eabf 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -276,7 +276,7 @@ def valid_input( print(f"{user_input}: {err_msg}") continue except ValueError: - print("Your entered value is not numerical!") + print(f"{user_input}: Incorrect input type, expected {input_type.__name__!r}") # Main Function From db826885fe8f73d87c038e1610077657526034c2 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 10 Nov 2020 20:18:36 +0000 Subject: [PATCH 8/8] fixup! Format Python code with psf/black push --- machine_learning/linear_discriminant_analysis.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py index 087a4c90eabf..0d19e970e973 100644 --- a/machine_learning/linear_discriminant_analysis.py +++ b/machine_learning/linear_discriminant_analysis.py @@ -276,7 +276,9 @@ def valid_input( print(f"{user_input}: {err_msg}") continue except ValueError: - print(f"{user_input}: Incorrect input type, expected {input_type.__name__!r}") + print( + f"{user_input}: Incorrect input type, expected {input_type.__name__!r}" + ) # Main Function