From 57e7cdc85e3a4f908a64a8e0024616ccebc396eb Mon Sep 17 00:00:00 2001 From: Arnav Kohli Date: Sat, 7 Oct 2023 09:35:34 +0530 Subject: [PATCH 01/35] Created folder for losses in Machine_Learning --- .../losses/binary_cross_entropy.py | 31 +++++++++++++++++++ machine_learning/losses/mean_squared_error.py | 27 ++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 machine_learning/losses/binary_cross_entropy.py create mode 100644 machine_learning/losses/mean_squared_error.py diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py new file mode 100644 index 000000000000..1c19974877ec --- /dev/null +++ b/machine_learning/losses/binary_cross_entropy.py @@ -0,0 +1,31 @@ +import numpy as np + + +def binary_cross_entropy(y_true, y_pred, epsilon=1e-15): + """ + Calculate the BCE Loss between true labels and predicted probabilities. + + Parameters: + - y_true: True binary labels (0 or 1). + - y_pred: Predicted probabilities for class 1. + - epsilon: Small constant to avoid numerical instability. + + Returns: + - bce_loss: Binary Cross-Entropy Loss. + + Example Usage: + true_labels = np.array([0, 1, 1, 0, 1]) + predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8]) + bce_loss = binary_cross_entropy(true_labels, predicted_probs) + print(f"Binary Cross-Entropy Loss: {bce_loss}") + """ + # Clip predicted probabilities to avoid log(0) and log(1) + y_pred = np.clip(y_pred, epsilon, 1 - epsilon) + + # Calculate binary cross-entropy loss + bce_loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)) + + # Take the mean over all samples + bce_loss = np.mean(bce_loss) + + return bce_loss diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py new file mode 100644 index 000000000000..580b1a123115 --- /dev/null +++ b/machine_learning/losses/mean_squared_error.py @@ -0,0 +1,27 @@ +import numpy as np + + +def mean_squared_error(y_true, y_pred): + """ + Calculate the Mean Squared Error (MSE) between two arrays. + + Parameters: + - y_true: The true values (ground truth). + - y_pred: The predicted values. + + Returns: + - mse: The Mean Squared Error between y_true and y_pred. + + Example usage: + true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) + predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) + mse = mean_squared_error(true_values, predicted_values) + print(f"Mean Squared Error: {mse}") + """ + if len(y_true) != len(y_pred): + raise ValueError("Input arrays must have the same length.") + + squared_errors = np.square(np.subtract(y_true, y_pred)) + mse = np.mean(squared_errors) + + return mse From aedf0b953fc2ac8e1520b4a116a8dfc28081a3d1 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:51:52 +0530 Subject: [PATCH 02/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 1c19974877ec..a287a1e8ca92 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -1,7 +1,7 @@ import numpy as np -def binary_cross_entropy(y_true, y_pred, epsilon=1e-15): +def binary_cross_entropy(y_true: list[int], y_pred: list[float], epsilon: int=1e-15) -> float: """ Calculate the BCE Loss between true labels and predicted probabilities. From 9b7468ccfb75c723ff90f9c3ac8a34570d1b9f01 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 04:22:25 +0000 Subject: [PATCH 03/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/losses/binary_cross_entropy.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index a287a1e8ca92..b3758777e668 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -1,7 +1,9 @@ import numpy as np -def binary_cross_entropy(y_true: list[int], y_pred: list[float], epsilon: int=1e-15) -> float: +def binary_cross_entropy( + y_true: list[int], y_pred: list[float], epsilon: int = 1e-15 +) -> float: """ Calculate the BCE Loss between true labels and predicted probabilities. From 38f405cbf247fa96598918240276a9071ba9ab8a Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:52:57 +0530 Subject: [PATCH 04/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 580b1a123115..e07201656330 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -1,7 +1,7 @@ import numpy as np -def mean_squared_error(y_true, y_pred): +def mean_squared_error(y_true: list[float], y_pred: list[float]) -> float: """ Calculate the Mean Squared Error (MSE) between two arrays. From 448785c664cba3727fe5f6973966554378cc4819 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:56:40 +0530 Subject: [PATCH 05/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index b3758777e668..73717f095434 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -2,7 +2,7 @@ def binary_cross_entropy( - y_true: list[int], y_pred: list[float], epsilon: int = 1e-15 + y_true: np.array[int], y_pred: np.array[float], epsilon: float = 1e-15 ) -> float: """ Calculate the BCE Loss between true labels and predicted probabilities. From 761fe3397451a8950b909690f1ee40fb3918ddf8 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 09:57:24 +0530 Subject: [PATCH 06/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index e07201656330..8777ec3b80e8 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -1,7 +1,7 @@ import numpy as np -def mean_squared_error(y_true: list[float], y_pred: list[float]) -> float: +def mean_squared_error(y_true: np.array[float], y_pred: np.array[float]) -> float: """ Calculate the Mean Squared Error (MSE) between two arrays. From 7d59779a786a9674d1438531d689e5ff205ab569 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 10:03:05 +0530 Subject: [PATCH 07/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 73717f095434..2622c0b1b919 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -1,9 +1,7 @@ import numpy as np -def binary_cross_entropy( - y_true: np.array[int], y_pred: np.array[float], epsilon: float = 1e-15 -) -> float: +def binary_cross_entropy(y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15) -> float: """ Calculate the BCE Loss between true labels and predicted probabilities. From e8e0aa2e5870adac5d0f665a4bbc8880a4e97284 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 04:33:44 +0000 Subject: [PATCH 08/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/losses/binary_cross_entropy.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 2622c0b1b919..c29a6ac940c6 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -1,7 +1,9 @@ import numpy as np -def binary_cross_entropy(y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15) -> float: +def binary_cross_entropy( + y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15 +) -> float: """ Calculate the BCE Loss between true labels and predicted probabilities. From 09cd35066a6b591caa2937fad3c8cccb54ca978e Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 10:03:54 +0530 Subject: [PATCH 09/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 8777ec3b80e8..fb62bb69ea15 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -1,7 +1,7 @@ import numpy as np -def mean_squared_error(y_true: np.array[float], y_pred: np.array[float]) -> float: +def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: """ Calculate the Mean Squared Error (MSE) between two arrays. From ae4e3ee0404013f2979367398a571f9062494c3e Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 10:49:47 +0530 Subject: [PATCH 10/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index c29a6ac940c6..cbbb3b1d82e9 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -31,3 +31,8 @@ def binary_cross_entropy( bce_loss = np.mean(bce_loss) return bce_loss + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 6f26bc36c5db356f3ed4c9ceb40426703b84188f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 05:20:20 +0000 Subject: [PATCH 11/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/losses/binary_cross_entropy.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index cbbb3b1d82e9..fdeed647734a 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -32,6 +32,7 @@ def binary_cross_entropy( return bce_loss + if __name__ == "__main__": import doctest From 66faa358a091700babc58797b0aaa85df68583ab Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 10:50:39 +0530 Subject: [PATCH 12/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index fb62bb69ea15..2e7314841ff3 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -25,3 +25,8 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: mse = np.mean(squared_errors) return mse + +if __name__ == "__main__": + import doctest + + doctest.testmod() From c5a58c372cc9428c65c11bc7c10d6cfb77de8676 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 05:21:14 +0000 Subject: [PATCH 13/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/losses/mean_squared_error.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 2e7314841ff3..84a63e86c522 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -26,6 +26,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: return mse + if __name__ == "__main__": import doctest From 0753db242d051e05a629666dcdb89a72e73af45e Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:00:22 +0530 Subject: [PATCH 14/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index fdeed647734a..4c4656ceca4c 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -16,10 +16,11 @@ def binary_cross_entropy( - bce_loss: Binary Cross-Entropy Loss. Example Usage: - true_labels = np.array([0, 1, 1, 0, 1]) - predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8]) - bce_loss = binary_cross_entropy(true_labels, predicted_probs) - print(f"Binary Cross-Entropy Loss: {bce_loss}") + >>> true_labels = np.array([0, 1, 1, 0, 1]) + >>> predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8]) + >>> bce_loss = binary_cross_entropy(true_labels, predicted_probs) + >>> bce_loss + 0.6785203447911846 """ # Clip predicted probabilities to avoid log(0) and log(1) y_pred = np.clip(y_pred, epsilon, 1 - epsilon) From 277a681ad63c7c284cb17daa67e5ba8187183e2f Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:00:51 +0530 Subject: [PATCH 15/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 84a63e86c522..8257bf981ea1 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -13,10 +13,11 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: - mse: The Mean Squared Error between y_true and y_pred. Example usage: - true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) - predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) - mse = mean_squared_error(true_values, predicted_values) - print(f"Mean Squared Error: {mse}") + >>> true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) + >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) + >>> mse = mean_squared_error(true_values, predicted_values) + >>> mse + 0.10599999999999987 """ if len(y_true) != len(y_pred): raise ValueError("Input arrays must have the same length.") From 1e3baed363c423095ffad760805e773720e08fd6 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:05:41 +0530 Subject: [PATCH 16/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 4c4656ceca4c..e1fef99aeff4 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -20,7 +20,7 @@ def binary_cross_entropy( >>> predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8]) >>> bce_loss = binary_cross_entropy(true_labels, predicted_probs) >>> bce_loss - 0.6785203447911846 + 0.2529995012327421 """ # Clip predicted probabilities to avoid log(0) and log(1) y_pred = np.clip(y_pred, epsilon, 1 - epsilon) From 25c550a388c2f3f01715402b8974597c487b8f88 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:06:41 +0530 Subject: [PATCH 17/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 8257bf981ea1..3f934cdd01a6 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -17,7 +17,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) >>> mse = mean_squared_error(true_values, predicted_values) >>> mse - 0.10599999999999987 + 0.028000000000000032 """ if len(y_true) != len(y_pred): raise ValueError("Input arrays must have the same length.") From 8513bdafe2db9cfee1f506fc8eaeb891ea839858 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:54:56 +0530 Subject: [PATCH 18/35] Update machine_learning/losses/binary_cross_entropy.py Co-authored-by: Christian Clauss --- machine_learning/losses/binary_cross_entropy.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index e1fef99aeff4..7c52ce47302f 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -29,9 +29,7 @@ def binary_cross_entropy( bce_loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred)) # Take the mean over all samples - bce_loss = np.mean(bce_loss) - - return bce_loss + return np.mean(bce_loss) if __name__ == "__main__": From cae669eb1038eac81f7d8e4c0b14c6fcb19092b7 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:55:08 +0530 Subject: [PATCH 19/35] Update machine_learning/losses/mean_squared_error.py Co-authored-by: Christian Clauss --- machine_learning/losses/mean_squared_error.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 3f934cdd01a6..fc389608e44c 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -15,8 +15,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: Example usage: >>> true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) - >>> mse = mean_squared_error(true_values, predicted_values) - >>> mse + >>> mean_squared_error(true_values, predicted_values) 0.028000000000000032 """ if len(y_true) != len(y_pred): From 5624c229e720312cf11e0fa34916869041baf1e0 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:55:42 +0530 Subject: [PATCH 20/35] Update machine_learning/losses/binary_cross_entropy.py Co-authored-by: Christian Clauss --- machine_learning/losses/binary_cross_entropy.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 7c52ce47302f..7e40f8bfd516 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -18,8 +18,7 @@ def binary_cross_entropy( Example Usage: >>> true_labels = np.array([0, 1, 1, 0, 1]) >>> predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8]) - >>> bce_loss = binary_cross_entropy(true_labels, predicted_probs) - >>> bce_loss + >>> binary_cross_entropy(true_labels, predicted_probs) 0.2529995012327421 """ # Clip predicted probabilities to avoid log(0) and log(1) From 7cebd00b057ddba17d2114b9e9cda061d756a6a4 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 14:00:42 +0530 Subject: [PATCH 21/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index fc389608e44c..784f0b065dae 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -22,9 +22,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: raise ValueError("Input arrays must have the same length.") squared_errors = np.square(np.subtract(y_true, y_pred)) - mse = np.mean(squared_errors) - - return mse + return np.mean(squared_errors) if __name__ == "__main__": From f2000499f596feaca7815498648f02e2317b3f20 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:27:40 +0530 Subject: [PATCH 22/35] Update machine_learning/losses/mean_squared_error.py Co-authored-by: Tianyi Zheng --- machine_learning/losses/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 784f0b065dae..e8316fdbed3d 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -21,7 +21,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: if len(y_true) != len(y_pred): raise ValueError("Input arrays must have the same length.") - squared_errors = np.square(np.subtract(y_true, y_pred)) + squared_errors = (y_true - y_pred) ** 2 return np.mean(squared_errors) From b302359d28a66e842446b66bb6d70a6a89100c9e Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:35:32 +0530 Subject: [PATCH 23/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 7e40f8bfd516..d733515e9152 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -1,3 +1,18 @@ +""" +Binary Cross-Entropy (BCE) Loss Function + +This script defines the Binary Cross-Entropy (BCE) loss function, which is commonly used for binary classification problems. + +Description: +Binary Cross-Entropy (BCE), also known as log loss or logistic loss, is a popular loss function for binary classification tasks. It quantifies the dissimilarity between the true binary labels (0 or 1) and the predicted probabilities produced by a model. Lower BCE values indicate better alignment between predicted probabilities and true labels. + +Formula: +BCE = -Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) + +Source: +- [Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) +""" + import numpy as np From ba41942528a76242eda00fc4f0f6677098e0a2cf Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:36:00 +0530 Subject: [PATCH 24/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index e8316fdbed3d..9a6bdbf5b243 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -1,3 +1,18 @@ +""" +Mean Squared Error (MSE) Loss Function + +This script defines the Mean Squared Error (MSE) loss function, which is commonly used for regression problems. + +Description: +The Mean Squared Error (MSE) measures the average squared difference between the true values (ground truth) and the predicted values produced by a model. It is widely used in regression tasks and serves as a measure of the model's accuracy. + +Formula: +MSE = (1/n) * Σ(y_true - y_pred)^2 + +Source: +- [Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) +""" + import numpy as np From 26beff697602058d64d8dc110bcb7e86affa6dfc Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:36:38 +0530 Subject: [PATCH 25/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index d733515e9152..8ac02802ec87 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -4,7 +4,9 @@ This script defines the Binary Cross-Entropy (BCE) loss function, which is commonly used for binary classification problems. Description: -Binary Cross-Entropy (BCE), also known as log loss or logistic loss, is a popular loss function for binary classification tasks. It quantifies the dissimilarity between the true binary labels (0 or 1) and the predicted probabilities produced by a model. Lower BCE values indicate better alignment between predicted probabilities and true labels. +Binary Cross-Entropy (BCE), also known as log loss or logistic loss, is a popular loss function for binary classification tasks. +It quantifies the dissimilarity between the true binary labels (0 or 1) and the predicted probabilities produced by a model. +Lower BCE values indicate better alignment between predicted probabilities and true labels. Formula: BCE = -Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) From e60dd140e11b96c813d81d6eef33816ae00fd34b Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:37:28 +0530 Subject: [PATCH 26/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 9a6bdbf5b243..181268e614fb 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -4,7 +4,8 @@ This script defines the Mean Squared Error (MSE) loss function, which is commonly used for regression problems. Description: -The Mean Squared Error (MSE) measures the average squared difference between the true values (ground truth) and the predicted values produced by a model. It is widely used in regression tasks and serves as a measure of the model's accuracy. +The Mean Squared Error (MSE) measures the average squared difference between the true values (ground truth) and the predicted values produced by a model. +It is widely used in regression tasks and serves as a measure of the model's accuracy. Formula: MSE = (1/n) * Σ(y_true - y_pred)^2 From 747e8cc9e94a3b178ad69c326450ac2369ab5528 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:43:50 +0530 Subject: [PATCH 27/35] Update mean_squared_error.py --- machine_learning/losses/mean_squared_error.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/losses/mean_squared_error.py index 181268e614fb..8f32e4111377 100644 --- a/machine_learning/losses/mean_squared_error.py +++ b/machine_learning/losses/mean_squared_error.py @@ -1,17 +1,16 @@ """ Mean Squared Error (MSE) Loss Function -This script defines the Mean Squared Error (MSE) loss function, which is commonly used for regression problems. +This script defines the Mean Squared Error (MSE) loss function, commonly used for regression problems. Description: -The Mean Squared Error (MSE) measures the average squared difference between the true values (ground truth) and the predicted values produced by a model. -It is widely used in regression tasks and serves as a measure of the model's accuracy. +MSE measures the average squared difference between true values (ground truth) and predicted values. +It serves as a measure of the model's accuracy in regression tasks. Formula: MSE = (1/n) * Σ(y_true - y_pred)^2 -Source: -- [Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) +Source: [Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) """ import numpy as np From e618611d5c1d8e2e44df512e75fb585b6a9e03f3 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:44:43 +0530 Subject: [PATCH 28/35] Update binary_cross_entropy.py --- machine_learning/losses/binary_cross_entropy.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/losses/binary_cross_entropy.py index 8ac02802ec87..1cfd8b96a12a 100644 --- a/machine_learning/losses/binary_cross_entropy.py +++ b/machine_learning/losses/binary_cross_entropy.py @@ -1,18 +1,16 @@ """ Binary Cross-Entropy (BCE) Loss Function -This script defines the Binary Cross-Entropy (BCE) loss function, which is commonly used for binary classification problems. +This script defines the Binary Cross-Entropy (BCE) loss function, commonly used for binary classification. Description: -Binary Cross-Entropy (BCE), also known as log loss or logistic loss, is a popular loss function for binary classification tasks. -It quantifies the dissimilarity between the true binary labels (0 or 1) and the predicted probabilities produced by a model. -Lower BCE values indicate better alignment between predicted probabilities and true labels. +BCE quantifies dissimilarity between true binary labels (0 or 1) and predicted probabilities. +It's widely used in binary classification tasks. Formula: BCE = -Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) -Source: -- [Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) +Source: [Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) """ import numpy as np From fc15fb85f3faa3f34cd7c0ebe7280be81a4fd81b Mon Sep 17 00:00:00 2001 From: Arnav Kohli Date: Sat, 7 Oct 2023 15:47:12 +0530 Subject: [PATCH 29/35] renamed: losses -> loss_functions --- .../{losses => loss_functions}/binary_cross_entropy.py | 0 machine_learning/{losses => loss_functions}/mean_squared_error.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename machine_learning/{losses => loss_functions}/binary_cross_entropy.py (100%) rename machine_learning/{losses => loss_functions}/mean_squared_error.py (100%) diff --git a/machine_learning/losses/binary_cross_entropy.py b/machine_learning/loss_functions/binary_cross_entropy.py similarity index 100% rename from machine_learning/losses/binary_cross_entropy.py rename to machine_learning/loss_functions/binary_cross_entropy.py diff --git a/machine_learning/losses/mean_squared_error.py b/machine_learning/loss_functions/mean_squared_error.py similarity index 100% rename from machine_learning/losses/mean_squared_error.py rename to machine_learning/loss_functions/mean_squared_error.py From 7ff794822363ebe6e83f54853e086bffd98b7a8a Mon Sep 17 00:00:00 2001 From: Arnav Kohli Date: Sat, 7 Oct 2023 15:53:08 +0530 Subject: [PATCH 30/35] updated 2 files --- machine_learning/loss_functions/binary_cross_entropy.py | 7 +++---- machine_learning/loss_functions/mean_squared_error.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/machine_learning/loss_functions/binary_cross_entropy.py b/machine_learning/loss_functions/binary_cross_entropy.py index 1cfd8b96a12a..49e792bd23ad 100644 --- a/machine_learning/loss_functions/binary_cross_entropy.py +++ b/machine_learning/loss_functions/binary_cross_entropy.py @@ -1,16 +1,15 @@ """ Binary Cross-Entropy (BCE) Loss Function -This script defines the Binary Cross-Entropy (BCE) loss function, commonly used for binary classification. - Description: -BCE quantifies dissimilarity between true binary labels (0 or 1) and predicted probabilities. +Quantifies dissimilarity between true labels (0 or 1) and predicted probabilities. It's widely used in binary classification tasks. Formula: BCE = -Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) -Source: [Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) +Source: +[Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) """ import numpy as np diff --git a/machine_learning/loss_functions/mean_squared_error.py b/machine_learning/loss_functions/mean_squared_error.py index 8f32e4111377..674c8be02e77 100644 --- a/machine_learning/loss_functions/mean_squared_error.py +++ b/machine_learning/loss_functions/mean_squared_error.py @@ -1,16 +1,15 @@ """ Mean Squared Error (MSE) Loss Function -This script defines the Mean Squared Error (MSE) loss function, commonly used for regression problems. - Description: -MSE measures the average squared difference between true values (ground truth) and predicted values. +MSE measures the mean squared difference between true values and predicted values. It serves as a measure of the model's accuracy in regression tasks. Formula: MSE = (1/n) * Σ(y_true - y_pred)^2 -Source: [Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) +Source: +[Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) """ import numpy as np From 9502a0cbefc7f0deb94f9d35a7409c6885fdc545 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 10:24:03 +0000 Subject: [PATCH 31/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/loss_functions/binary_cross_entropy.py | 2 +- machine_learning/loss_functions/mean_squared_error.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/machine_learning/loss_functions/binary_cross_entropy.py b/machine_learning/loss_functions/binary_cross_entropy.py index 49e792bd23ad..0f2542c3ff85 100644 --- a/machine_learning/loss_functions/binary_cross_entropy.py +++ b/machine_learning/loss_functions/binary_cross_entropy.py @@ -8,7 +8,7 @@ Formula: BCE = -Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred)) -Source: +Source: [Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) """ diff --git a/machine_learning/loss_functions/mean_squared_error.py b/machine_learning/loss_functions/mean_squared_error.py index 674c8be02e77..6f697f8360dd 100644 --- a/machine_learning/loss_functions/mean_squared_error.py +++ b/machine_learning/loss_functions/mean_squared_error.py @@ -8,7 +8,7 @@ Formula: MSE = (1/n) * Σ(y_true - y_pred)^2 -Source: +Source: [Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error) """ From 2a4989dc6c4fc8845220cb2b07e28ece8f7caf03 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:00:47 +0530 Subject: [PATCH 32/35] Update mean_squared_error.py --- machine_learning/loss_functions/mean_squared_error.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/machine_learning/loss_functions/mean_squared_error.py b/machine_learning/loss_functions/mean_squared_error.py index 6f697f8360dd..5d540e93a07e 100644 --- a/machine_learning/loss_functions/mean_squared_error.py +++ b/machine_learning/loss_functions/mean_squared_error.py @@ -31,6 +31,12 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) >>> mean_squared_error(true_values, predicted_values) 0.028000000000000032 + >>> true_labels = np.array([0, 1, 1, 0, 1]) + >>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2]) + >>> binary_cross_entropy(true_labels, predicted_probs) + Traceback (most recent call last): + ... + ValueError: Input arrays must have the same length. """ if len(y_true) != len(y_pred): raise ValueError("Input arrays must have the same length.") From 5a23be75f906e8998a609ae6143508d7a1f91aa9 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:01:14 +0530 Subject: [PATCH 33/35] Update mean_squared_error.py --- machine_learning/loss_functions/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/loss_functions/mean_squared_error.py b/machine_learning/loss_functions/mean_squared_error.py index 5d540e93a07e..c231499e1992 100644 --- a/machine_learning/loss_functions/mean_squared_error.py +++ b/machine_learning/loss_functions/mean_squared_error.py @@ -33,7 +33,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: 0.028000000000000032 >>> true_labels = np.array([0, 1, 1, 0, 1]) >>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2]) - >>> binary_cross_entropy(true_labels, predicted_probs) + >>> mean_squared_error(true_labels, predicted_probs) Traceback (most recent call last): ... ValueError: Input arrays must have the same length. From 9e1b7d3f7813d1501d3fbd96d0c1d723b98963ea Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:03:00 +0530 Subject: [PATCH 34/35] Update binary_cross_entropy.py --- machine_learning/loss_functions/binary_cross_entropy.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/machine_learning/loss_functions/binary_cross_entropy.py b/machine_learning/loss_functions/binary_cross_entropy.py index 0f2542c3ff85..4ebca7f21757 100644 --- a/machine_learning/loss_functions/binary_cross_entropy.py +++ b/machine_learning/loss_functions/binary_cross_entropy.py @@ -34,7 +34,15 @@ def binary_cross_entropy( >>> predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8]) >>> binary_cross_entropy(true_labels, predicted_probs) 0.2529995012327421 + >>> true_labels = np.array([0, 1, 1, 0, 1]) + >>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2]) + >>> binary_cross_entropy(true_labels, predicted_probs) + Traceback (most recent call last): + ... + ValueError: Input arrays must have the same length. """ + if len(y_true) != len(y_pred): + raise ValueError("Input arrays must have the same length.") # Clip predicted probabilities to avoid log(0) and log(1) y_pred = np.clip(y_pred, epsilon, 1 - epsilon) From f47d2c368029335015699712915678473502e9f6 Mon Sep 17 00:00:00 2001 From: Arnav Kohli <95236897+THEGAMECHANGER416@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:03:33 +0530 Subject: [PATCH 35/35] Update mean_squared_error.py --- machine_learning/loss_functions/mean_squared_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/loss_functions/mean_squared_error.py b/machine_learning/loss_functions/mean_squared_error.py index c231499e1992..d2b0e1e158ba 100644 --- a/machine_learning/loss_functions/mean_squared_error.py +++ b/machine_learning/loss_functions/mean_squared_error.py @@ -31,7 +31,7 @@ def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2]) >>> mean_squared_error(true_values, predicted_values) 0.028000000000000032 - >>> true_labels = np.array([0, 1, 1, 0, 1]) + >>> true_labels = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) >>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2]) >>> mean_squared_error(true_labels, predicted_probs) Traceback (most recent call last):