Compute RMSE between true and predicted values in NumPy

Quick Overview

Implement a NumPy function that returns the root mean squared error between two equal-length arrays as a Python float. It tests whether you average squared residuals over n, guard against shape and dtype problems, and know how RMSE differs from the residual standard error of a regression.

Compute RMSE between true and predicted values in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `compute_rmse(y_true, y_pred)` that takes two 1-D `np.ndarray` of equal length `n` and returns their root mean squared error as a single `float`. Divide by `n` (not `n-1` or residual degrees of freedom). $$ \text{RMSE} = \sqrt{\frac{1}{n}\sum_{i=1}^{n}\left(y_i - \hat{y}_i\right)^2} $$ Here $y_i$ is `y_true[i]` and $\hat{y}_i$ is `y_pred[i]`. ```hint Denominator and shapes Check what your averaging step divides by, and confirm that the subtraction produces a 1-D array of length `n` before you square it. ``` ### Constraints & Assumptions - `y_true` and `y_pred` are 1-D float arrays of the same length `n`. The test inputs have `n = 10`. - Any route is accepted: a manual `np.sqrt` of the mean squared residual, `np.sqrt(sklearn.metrics.mean_squared_error(...))`, or `sklearn.metrics.root_mean_squared_error(...)`. ### What a Strong Answer Covers - Residuals computed elementwise, squared, and averaged over all `n` observations. - A return value that is a plain Python `float`, not an array. - Why the denominator is `n` for this metric, and where `n - p` appears instead. - Input checks for mismatched lengths, accidental broadcasting and integer dtypes. - The time and space cost of the computation. ### Follow-up Questions - How would you compute RMSE over predictions that arrive in batches, without keeping every residual in memory? - When would you report MAE instead of RMSE, and how does a single large outlier move each one? - How does the training RMSE of an OLS fit relate to the residual standard error shown in a regression summary? - How would you extend the function to accept sample weights?

Overview: Implement a NumPy function that returns the root mean squared error between two equal-length arrays as a Python float. It tests whether you average squared residuals over n, guard against shape and dtype problems, and know how RMSE differs from the residual standard error of a regression.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement compute_rmse(y_true, y_pred) that takes two 1-D np.ndarray of equal length n and returns their root mean squared error as a single float. Divide by n (not n-1 or residual degrees of freedom).

RMSE=1n∑i=1n(yi−y^i)2\text{RMSE} = \sqrt{\frac{1}{n}\sum_{i=1}^{n}\left(y_i - \hat{y}_i\right)^2}

Here yiy_i is y_true[i] and y^i\hat{y}_i is y_pred[i].

Constraints & Assumptions

  • y_true and y_pred are 1-D float arrays of the same length n . The test inputs have n = 10 .
  • Any route is accepted: a manual np.sqrt of the mean squared residual, np.sqrt(sklearn.metrics.mean_squared_error(...)) , or sklearn.metrics.root_mean_squared_error(...) .

What a Strong Answer Covers Guidance

  • Residuals computed elementwise, squared, and averaged over all n observations.
  • A return value that is a plain Python float , not an array.
  • Why the denominator is n for this metric, and where n - p appears instead.
  • Input checks for mismatched lengths, accidental broadcasting and integer dtypes.
  • The time and space cost of the computation.

Follow-up Questions Guidance

  • How would you compute RMSE over predictions that arrive in batches, without keeping every residual in memory?
  • When would you report MAE instead of RMSE, and how does a single large outlier move each one?
  • How does the training RMSE of an OLS fit relate to the residual standard error shown in a regression summary?
  • How would you extend the function to accept sample weights?