← Problems

5. Mean Squared Error

EasyLoss FunctionsStatisticsRegression

Implement the Mean Squared Error (MSE) loss function.

Given a vector of true values y\mathbf{y} and predicted values y^\hat{\mathbf{y}}:

MSE=1ni=1n(yiy^i)2\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2

MSE is one of the most common loss functions for regression tasks. Squaring the errors penalises large deviations more heavily than small ones.

Example

y_true = [1, 2, 3] y_pred = [1, 2, 3] Output: 0.0 # perfect predictions y_true = [0, 0, 0] y_pred = [1, 1, 1] Output: 1.0

Constraints

  • Both inputs will always have the same length n1n \geq 1.
  • Return a plain Python float.
Python 3
⌘ + Enter
Run your code to see results
Ctrl + Enter