Mean Squared Error: Why Big Misses Cost More

Loss FunctionsEasy+10 XP+10

Mean Squared Error: Why Big Misses Cost More

Mean squared error is the default way to score a regression: how far off were you, on average, with the misses squared before averaging.

MSE=1n∑i(yi−y^i)2MSE = \frac{1}{n} \sum_i (y_i - \hat{y}_i)^2MSE=n1​i∑​(yi​−y^​i​)2

Task: write mse(y_true, y_pred) returning that average, rounded to 4 decimal places.

  • The two lists are the same length and line up example by example.
  • Square each error, then take the mean — divide by n, the number of examples.
  • Squaring handles the signs: being 3 too high and 3 too low are equally wrong, and without the square they would cancel to zero.
  • A perfect prediction gives exactly 0.0.

Squaring does something else too, and it's the reason MSE is sometimes the wrong choice. Being off by 10 costs a hundred times as much as being off by 1, so a single outlier can dominate the whole score — and a model trained to minimise MSE will contort itself to accommodate that one point. When that's not what you want, the usual answers are mean absolute error or Huber loss.

1. Square each error4 9 1 25
2. Add them up—
3. Divide by 4—
Mean Squared Error—

Examples

y_true = [1, 2, 3]
y_pred = [2, 2, 4]
returns 0.6667
y_true = [3, 5, 7]
y_pred = [3, 5, 7]
returns 0.0

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run