Skip to content
Open
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
20 changes: 20 additions & 0 deletions machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,23 @@
import doctest

doctest.testmod()


def root_mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Calculate the Root Mean Squared Error (RMSE) between ground truth and predicted values.

Check failure on line 674 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/loss_functions.py:674:89: E501 Line too long (91 > 88)
"""
# Checking if input arrays have same length
if len(y_true) != len(y_pred):
raise ValueError("Input arrays must have the same length.")

# Calculating squared differences between true and predicted values
# (y_true - y_pred) gives errors, then we square each error
squared_errors = (y_true - y_pred) ** 2

# Calculate mean of all squared errors
# So that to get the MSE
mean_squared_error = np.mean(squared_errors)

# Taken the square root of MSE to get RMSE
return np.sqrt(mean_squared_error)
Loading