Two numbers summarise how far a model's predictions are from the truth. Compute both.
What to build
A program that prints the mean absolute error and the root mean squared error.
Requirements
- Print
MAE XandRMSE Y, each to two decimals.
Input
Two lines with the same number of values: the actual values, then the predictions.
Output
Two lines.
Constraints
1 to 1000 values per line.
Example
Input:
3 5 2 7
2 5 4 7
Output:
MAE 0.75
RMSE 1.12
Why it works
Taking absolute values or squares stops positive and negative errors from cancelling out, and the square root brings RMSE back to the units of the target.
Examples
Example 1 · Input
3 5 2 7 2 5 4 7
Expected output
MAE 0.75 RMSE 1.12
Example 2 · Input
10 20 10 20
Expected output
MAE 0.00 RMSE 0.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The error of each row is actual − predicted.
Hint 2
RMSE is the square root of the mean of the squared errors.
Solution
Show one correct solution (try it yourself first)
actual = list(map(float, input().split()))
pred = list(map(float, input().split()))
errors = [a - p for a, p in zip(actual, pred)]
print(f"MAE {sum(abs(e) for e in errors) / len(errors):.2f}")
print(f"RMSE {(sum(e * e for e in errors) / len(errors)) ** 0.5:.2f}")