Mean Squared Error Loss From Scratch
Implement Mean Squared Error (MSE) — the notes' own regression loss function — from scratch, and compute it for a set of predictions against the actual values.
Approach: MSE is the average of the squared difference between each prediction and its corresponding actual value.
Input: Two lines: the predicted values (space-separated), then the actual values (space-separated, same count).
Output: One line: the MSE, rounded to 4 decimal places.
3 5 2.5 2.5 5 4
0.8333
- 1 <= count of values <= 20
Hint 1
zip(preds, actuals) pairs up each prediction with its matching actual value.
Hint 2
(p - a) ** 2 is the squared error for one pair; sum them all up and divide by the count.
MSE is defined as the average of the squared differences between each prediction and its actual value: sum((p - a) ** 2 for p, a in zip(preds, actuals)) / len(preds). A lower MSE means the predictions are, on average, closer to the true values — exactly the quantity a regression model's training tries to minimize.