From e8b2047a67069e4a834e1dfd2accf10c9742c2d1 Mon Sep 17 00:00:00 2001 From: Addyk-24 Date: Fri, 10 Oct 2025 00:42:30 +0530 Subject: [PATCH 1/6] Adding RMSE - Root Mean Squared Error Loss function for ML Evaluation --- machine_learning/loss_functions.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0bd9aa8b5401..b993d08f3a1a 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -663,6 +663,50 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float return np.sum(kl_loss) +def root_mean_squared_error(y_true, y_pred): + """ + Root Mean Squared Error (RMSE) + + Root Mean Squared Error (RMSE) is a standard metric, + it measures the average magnitude of the prediction errors, giving + higher weight to larger errors due to squaring. + + RMSE = sqrt( (1/n) * Σ (y_true - y_pred) ^ 2) + + Reference: https://en.wikipedia.org/wiki/Root_mean_square_deviation + + Parameters: + - y_pred: Predicted Value + - y_true: Actual Value + + Returns: + float: The RMSE Loss function between y_pred and y_true + + >>> true_labels = np.array([100, 200, 300]) + >>> predicted_probs = np.array([110, 190, 310]) + >>> root_mean_squared_error(true_labels, predicted_probs) + 3.42 + + >>> true_labels = [2, 4, 6, 8] + >>> predicted_probs = [3, 5, 7, 10] + >>> root_mean_squared_error(true_labels, predicted_probs) + 1.2247 + + >>> 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]) + >>> root_mean_squared_error(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.") + y_true, y_pred = np.array(y_true), np.array(y_pred) + + mse = np.mean((y_pred - y_true) ** 2) + return np.sqrt(mse) + + if __name__ == "__main__": import doctest From 7a5d06e58266b6ad3e166b8f98b89c683e5dc751 Mon Sep 17 00:00:00 2001 From: Aditya Katkar Date: Fri, 10 Oct 2025 10:13:35 +0530 Subject: [PATCH 2/6] Update loss_functions.py --- machine_learning/loss_functions.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index b993d08f3a1a..0761c648c412 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -663,7 +663,7 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float return np.sum(kl_loss) -def root_mean_squared_error(y_true, y_pred): +def root_mean_squared_error(y_true: np.array, y_pred:np.array) -> float: """ Root Mean Squared Error (RMSE) @@ -682,15 +682,10 @@ def root_mean_squared_error(y_true, y_pred): Returns: float: The RMSE Loss function between y_pred and y_true - >>> true_labels = np.array([100, 200, 300]) - >>> predicted_probs = np.array([110, 190, 310]) + >>> true_labels = np.array([2, 4, 6, 8]) + >>> predicted_probs = np.array([3, 5, 7, 10]) >>> root_mean_squared_error(true_labels, predicted_probs) - 3.42 - - >>> true_labels = [2, 4, 6, 8] - >>> predicted_probs = [3, 5, 7, 10] - >>> root_mean_squared_error(true_labels, predicted_probs) - 1.2247 + 1.3228 >>> 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]) @@ -703,7 +698,7 @@ def root_mean_squared_error(y_true, y_pred): raise ValueError("Input arrays must have the same length.") y_true, y_pred = np.array(y_true), np.array(y_pred) - mse = np.mean((y_pred - y_true) ** 2) + mse = np.mean((y_true - y_pred) ** 2) return np.sqrt(mse) From 6fbb8955993c1c47d14e3fb51d573cbf655b4104 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Oct 2025 04:44:16 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/loss_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0761c648c412..a9022e2b0d96 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -663,7 +663,7 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float return np.sum(kl_loss) -def root_mean_squared_error(y_true: np.array, y_pred:np.array) -> float: +def root_mean_squared_error(y_true: np.array, y_pred: np.array) -> float: """ Root Mean Squared Error (RMSE) From fee0797adcd3048fff870bddebf24b515c4eeb31 Mon Sep 17 00:00:00 2001 From: Aditya Katkar Date: Fri, 10 Oct 2025 10:56:11 +0530 Subject: [PATCH 4/6] Update loss_functions.py --- machine_learning/loss_functions.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index a9022e2b0d96..524bfc303673 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -676,16 +676,21 @@ def root_mean_squared_error(y_true: np.array, y_pred: np.array) -> float: Reference: https://en.wikipedia.org/wiki/Root_mean_square_deviation Parameters: - - y_pred: Predicted Value - - y_true: Actual Value + y_true: Actual Value + y_pred: Predicted Value Returns: float: The RMSE Loss function between y_pred and y_true - >>> true_labels = np.array([2, 4, 6, 8]) - >>> predicted_probs = np.array([3, 5, 7, 10]) - >>> root_mean_squared_error(true_labels, predicted_probs) - 1.3228 + >>> true_labels = np.array([100, 200, 300]) + >>> predicted_probs = np.array([110, 190, 310]) + >>> round(root_mean_squared_error(true_labels, predicted_probs), 4) + 10.0 + + >>> true_labels = [2, 4, 6, 8] + >>> predicted_probs = [3, 5, 7, 10] + >>> round(root_mean_squared_error(true_labels, predicted_probs), 4) + 1.3229 >>> 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]) @@ -698,7 +703,7 @@ def root_mean_squared_error(y_true: np.array, y_pred: np.array) -> float: raise ValueError("Input arrays must have the same length.") y_true, y_pred = np.array(y_true), np.array(y_pred) - mse = np.mean((y_true - y_pred) ** 2) + mse = np.mean((y_pred - y_true) ** 2) return np.sqrt(mse) From bb00bdce2ea7d5231b18cc59dcfb37f3faeee725 Mon Sep 17 00:00:00 2001 From: Aditya Katkar Date: Fri, 10 Oct 2025 11:35:46 +0530 Subject: [PATCH 5/6] Update loss_functions.py --- machine_learning/loss_functions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 524bfc303673..0f5b1191f1b9 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -690,7 +690,7 @@ def root_mean_squared_error(y_true: np.array, y_pred: np.array) -> float: >>> true_labels = [2, 4, 6, 8] >>> predicted_probs = [3, 5, 7, 10] >>> round(root_mean_squared_error(true_labels, predicted_probs), 4) - 1.3229 + 1.3228756555322954 >>> 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]) @@ -701,7 +701,6 @@ def root_mean_squared_error(y_true: np.array, y_pred: np.array) -> float: """ if len(y_true) != len(y_pred): raise ValueError("Input arrays must have the same length.") - y_true, y_pred = np.array(y_true), np.array(y_pred) mse = np.mean((y_pred - y_true) ** 2) return np.sqrt(mse) From b17bb67dc6cca849962dc6847a9c3d45d38a2813 Mon Sep 17 00:00:00 2001 From: Aditya Katkar Date: Fri, 10 Oct 2025 12:16:22 +0530 Subject: [PATCH 6/6] Update loss_functions.py --- machine_learning/loss_functions.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0f5b1191f1b9..bc81238cd211 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -684,14 +684,9 @@ def root_mean_squared_error(y_true: np.array, y_pred: np.array) -> float: >>> true_labels = np.array([100, 200, 300]) >>> predicted_probs = np.array([110, 190, 310]) - >>> round(root_mean_squared_error(true_labels, predicted_probs), 4) + >>> float(root_mean_squared_error(true_labels, predicted_probs)) 10.0 - >>> true_labels = [2, 4, 6, 8] - >>> predicted_probs = [3, 5, 7, 10] - >>> round(root_mean_squared_error(true_labels, predicted_probs), 4) - 1.3228756555322954 - >>> 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]) >>> root_mean_squared_error(true_labels, predicted_probs)