From a64f174d908e48aafaee88883bf68c136b0b5a17 Mon Sep 17 00:00:00 2001 From: Ankit Avinash Date: Tue, 17 Oct 2023 16:38:33 +0530 Subject: [PATCH 1/3] Added Mean Squared Logarithmic Error (MSLE) --- .../mean_squared_logarithmic_error.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 machine_learning/loss_functions/mean_squared_logarithmic_error.py diff --git a/machine_learning/loss_functions/mean_squared_logarithmic_error.py b/machine_learning/loss_functions/mean_squared_logarithmic_error.py new file mode 100644 index 000000000000..afcf3aceda27 --- /dev/null +++ b/machine_learning/loss_functions/mean_squared_logarithmic_error.py @@ -0,0 +1,51 @@ +""" +Mean Squared Logarithmic Error (MSLE) Loss Function + +Description: +MSLE measures the mean squared logarithmic difference between +true values and predicted values, particularly useful when +dealing with regression problems involving skewed or large-value +targets. It is often used when the relative differences between +predicted and true values are more important than absolute +differences. + +Formula: +MSLE = (1/n) * Σ(log(1 + y_true) - log(1 + y_pred))^2 +""" + +import numpy as np + + +def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: + """ + Calculate the Mean Squared Logarithmic Error (MSLE) between two arrays. + + Parameters: + - y_true: The true values (ground truth). + - y_pred: The predicted values. + + Returns: + - msle: The Mean Squared Logarithmic 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]) + >>> mean_squared_logarithmic_error(true_values, predicted_values) + 0.00308608779251813744 + >>> 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_logarithmic_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.") + + squared_logarithmic_errors = (np.log1p(y_true) - np.log1p(y_pred)) ** 2 + return np.mean(squared_logarithmic_errors) + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From ded7e631d89e1e8ca777dfd853047245f8a2e2a4 Mon Sep 17 00:00:00 2001 From: Ankit Avinash Date: Tue, 17 Oct 2023 16:43:36 +0530 Subject: [PATCH 2/3] Added Mean Squared Logarithmic Error (MSLE) --- .../loss_functions/mean_squared_logarithmic_error.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine_learning/loss_functions/mean_squared_logarithmic_error.py b/machine_learning/loss_functions/mean_squared_logarithmic_error.py index afcf3aceda27..afb35348776e 100644 --- a/machine_learning/loss_functions/mean_squared_logarithmic_error.py +++ b/machine_learning/loss_functions/mean_squared_logarithmic_error.py @@ -11,6 +11,9 @@ Formula: MSLE = (1/n) * Σ(log(1 + y_true) - log(1 + y_pred))^2 + +Source: +(https://insideaiml.com/blog/MeanSquared-Logarithmic-Error-Loss-1035) """ import numpy as np @@ -31,7 +34,7 @@ def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl >>> 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]) >>> mean_squared_logarithmic_error(true_values, predicted_values) - 0.00308608779251813744 + 0.0030860877925181344 >>> 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_logarithmic_error(true_labels, predicted_probs) From cb2a22333055810af8a9cdea87efab79ed49e40a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 11:17:54 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../mean_squared_logarithmic_error.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/machine_learning/loss_functions/mean_squared_logarithmic_error.py b/machine_learning/loss_functions/mean_squared_logarithmic_error.py index afb35348776e..935ebff37a51 100644 --- a/machine_learning/loss_functions/mean_squared_logarithmic_error.py +++ b/machine_learning/loss_functions/mean_squared_logarithmic_error.py @@ -2,11 +2,11 @@ Mean Squared Logarithmic Error (MSLE) Loss Function Description: -MSLE measures the mean squared logarithmic difference between -true values and predicted values, particularly useful when -dealing with regression problems involving skewed or large-value -targets. It is often used when the relative differences between -predicted and true values are more important than absolute +MSLE measures the mean squared logarithmic difference between +true values and predicted values, particularly useful when +dealing with regression problems involving skewed or large-value +targets. It is often used when the relative differences between +predicted and true values are more important than absolute differences. Formula: @@ -51,4 +51,5 @@ def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod()