Skip to content
Closed
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
32 changes: 32 additions & 0 deletions machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,38 @@
kl_loss = y_true * np.log(y_true / y_pred)
return np.sum(kl_loss)

def root_mean_squared_error(y_true, y_pred):
"""
Root Mean Squared Error (RMSE)

Check failure on line 668 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

machine_learning/loss_functions.py:668:1: W293 Blank line contains whitespace
Root Mean Squared Error (RMSE) is a standard metric used to evaluate
the accuracy of regression models.
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

Check failure on line 681 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

machine_learning/loss_functions.py:681:1: W293 Blank line contains whitespace
Returns:
float: The RMSE Loss function between y_pred and y_true

Check failure on line 684 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

machine_learning/loss_functions.py:684:1: W293 Blank line contains whitespace
Example:
>>> y_true = np.array([100, 200, 300])
>>> y_pred = np.array([110, 190, 310])
>>> rmse(y_true, y_pred)
3.42
"""
y_true, y_pred = np.array(y_true), np.array(y_pred)

rmse = np.sqrt(np.mean((y_pred - y_true) ** 2))

return rmse


if __name__ == "__main__":
import doctest
Expand Down
Loading