Compute Mean Squared Error

~8 mincode completion

Implement using NumPy. Do not use a for loop.

Examples

Perfect predictions → MSE = 0

Input
mean_squared_error([1, 2, 3], [1, 2, 3])
Output
0

Known MSE value

Input
mean_squared_error([1, 2, 3, 4], [2, 2, 2, 2])
Output
1.5

Single element

Input
mean_squared_error([5], [3])
Output
4

Hints

Hint 1

does the sum and the division in one step.

Hint 2

Do not forget to square. That step is easy to skip.

Requirements

  • y_true: Ground truth values, shape (n,)

  • y_pred: Model predictions, shape (n,)

  • Return Scalar MSE value.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

8 employers weight this skill

2 health and bio companies, 2 quant funds, 2 enterprise vendors, 1 AI product company, 1 frontier lab. Top match scores 87.

Python
import numpy as np

def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute mean squared error between targets and predictions.

    Args:
        y_true: Ground truth values, shape (n,)
        y_pred: Model predictions, shape (n,)

    Returns:
        Scalar MSE value.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.