Skip to content

Commit 105990d

Browse files
committed
Adding RMSE - Root Mean Squared Error Loss function for ML Evaluation
1 parent 5af8a2b commit 105990d

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

machine_learning/loss_functions.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -666,32 +666,44 @@ def root_mean_squared_error(y_true, y_pred):
666666
"""
667667
Root Mean Squared Error (RMSE)
668668
669-
Root Mean Squared Error (RMSE) is a standard metric used to evaluate
670-
the accuracy of regression models.
671-
It measures the average magnitude of the prediction errors, giving
669+
Root Mean Squared Error (RMSE) is a standard metric,
670+
it measures the average magnitude of the prediction errors, giving
672671
higher weight to larger errors due to squaring.
673672
674673
RMSE = sqrt( (1/n) * Σ (y_true - y_pred) ^ 2)
675674
676675
Reference: https://en.wikipedia.org/wiki/Root_mean_square_deviation
677676
678677
Parameters:
679-
y_pred: Predicted Value
680-
y_true: Actual Value
678+
- y_pred: Predicted Value
679+
- y_true: Actual Value
681680
682681
Returns:
683682
float: The RMSE Loss function between y_pred and y_true
684683
685-
>>> y_true = np.array([100, 200, 300])
686-
>>> y_pred = np.array([110, 190, 310])
687-
>>> rmse(y_true, y_pred)
684+
>>> true_labels = np.array([100, 200, 300])
685+
>>> predicted_probs = np.array([110, 190, 310])
686+
>>> root_mean_squared_error(true_labels, predicted_probs)
688687
3.42
688+
689+
>>> true_labels = [2, 4, 6, 8]
690+
>>> predicted_probs = [3, 5, 7, 10]
691+
>>> root_mean_squared_error(true_labels, predicted_probs)
692+
1.2247
693+
694+
>>> true_labels = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
695+
>>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2])
696+
>>> root_mean_squared_error(true_labels, predicted_probs)
697+
Traceback (most recent call last):
698+
...
699+
ValueError: Input arrays must have the same length.
689700
"""
701+
if len(y_true) != len(y_pred):
702+
raise ValueError("Input arrays must have the same length.")
690703
y_true, y_pred = np.array(y_true), np.array(y_pred)
691704

692-
rmse = np.sqrt(np.mean((y_pred - y_true) ** 2))
693-
694-
return rmse
705+
mse = np.mean((y_pred - y_true) ** 2)
706+
return np.sqrt(mse)
695707

696708

697709
if __name__ == "__main__":

0 commit comments

Comments
 (0)