← back

Calculate Root Mean Square Error (RMSE)

#71 · Machine Learning · Easy

⊣ Solve on deep-ml.com

Problem

Calculate the Root Mean Square Error (RMSE) between actual and predicted values. RMSE is a standard metric for measuring the accuracy of regression predictions.

Solution

1
2
3
4
def rmse(y_true, y_pred):
    n = len(y_true)
    mse = sum((yt - yp) ** 2 for yt, yp in zip(y_true, y_pred)) / n
    return round(mse ** 0.5, 4)

Explanation

  1. Compute the squared difference between each actual and predicted value.
  2. Average these squared differences to get the Mean Squared Error (MSE).
  3. Take the square root of MSE to get RMSE.
  4. RMSE has the same units as the target variable, making it more interpretable than MSE.
  5. Lower RMSE indicates better model performance.

Complexity

  • Time: O(n) where n is the number of data points
  • Space: O(1)