Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

3. Mean Squared Error

easy
GoogleGoogle
entry

Implement a function that computes the Mean Squared Error (MSE) between two NumPy arrays: ground-truth values (y_true) and predicted values (y_pred).

The MSE is defined as the average of the squared differences between corresponding elements:

MSE=1n∑i=1n(ytrue(i)−ypred(i))2\text{MSE}=\frac{1}{n}\sum_{i=1}^{n}(y^{(i)}_{\text{true}}-y^{(i)}_{\text{pred}})^2

Your function should return the MSE as a Python float.

Input Signature
ArgumentType
y_prednp.ndarray
y_truenp.ndarray
Output Signature
Return NameType
valuefloat

Constraints

  • 1≤n≤1051 \le n \le 10^5

  • Inputs are NumPy arrays of dtype=float

  • Return value must be a Python float

Hint 1

Compute squared error efficiently with vectorization: (y_true - y_pred) ** 2, then take np.mean(...).

Roles
Data Scientist
ML Engineer
Companies
GoogleGoogle
UberUber
Levels
entry
Tags
numpy
12 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit