Compute Mean Squared Error

Beginner
~8 min
code completion

Mean Squared Error Loss

Before we can optimize a model, we need to measure how wrong it is. Mean Squared Error (MSE) is the most common loss function for regression:

where are the true labels and are the predictions.

Properties of MSE:

  • Always non-negative (minimum is 0, perfect predictions)
  • Penalizes large errors heavily (squared)
  • Differentiable everywhere (important for gradient descent)
  • Your task: Implement mean_squared_error(y_true, y_pred) using NumPy. Do not use a for loop.

    Example Tests

    Perfect predictions → MSE = 0

    Input: {"y_pred":[1,2,3],"y_true":[1,2,3]}

    Expected: 0

    Known MSE value

    Input: {"y_pred":[2,2,2,2],"y_true":[1,2,3,4]}

    Expected: 1.5

    Single element

    Input: {"y_pred":[3],"y_true":[5]}

    Expected: 4

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.