MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / mean_squared_logarithmic_error

Function mean_squared_logarithmic_error

machine_learning/loss_functions.py:403–436  ·  view source on GitHub ↗

Calculate the mean squared logarithmic error (MSLE) between ground truth and predicted values. MSLE measures the squared logarithmic difference between true values and predicted values for regression models. It's particularly useful for dealing with skewed or large-value data,

(y_true: np.ndarray, y_pred: np.ndarray)

Source from the content-addressed store, hash-verified

401
402
403def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
404 """
405 Calculate the mean squared logarithmic error (MSLE) between ground truth and
406 predicted values.
407
408 MSLE measures the squared logarithmic difference between true values and predicted
409 values for regression models. It's particularly useful for dealing with skewed or
410 large-value data, and it's often used when the relative differences between
411 predicted and true values are more important than absolute differences.
412
413 MSLE = (1/n) * Σ(log(1 + y_true) - log(1 + y_pred))^2
414
415 Reference: https://insideaiml.com/blog/MeanSquared-Logarithmic-Error-Loss-1035
416
417 Parameters:
418 - y_true: The true values (ground truth)
419 - y_pred: The predicted values
420
421 >>> true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
422 >>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2])
423 >>> float(mean_squared_logarithmic_error(true_values, predicted_values))
424 0.0030860877925181344
425 >>> true_labels = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
426 >>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2])
427 >>> mean_squared_logarithmic_error(true_labels, predicted_probs)
428 Traceback (most recent call last):
429 ...
430 ValueError: Input arrays must have the same length.
431 """
432 if len(y_true) != len(y_pred):
433 raise ValueError("Input arrays must have the same length.")
434
435 squared_logarithmic_errors = (np.log1p(y_true) - np.log1p(y_pred)) ** 2
436 return np.mean(squared_logarithmic_errors)
437
438
439def mean_absolute_percentage_error(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected