Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Mean Squared Logarithmic Error (MSLE) Loss Function #10637

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions machine_learning/loss_functions/mean_squared_logarithmic_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
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

Source:
(https://insideaiml.com/blog/MeanSquared-Logarithmic-Error-Loss-1035)
"""

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.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)
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()