A model earns its place only by beating a simple baseline on rows it has not seen. Fit a line, then compare it with the baseline on the test rows.
What to build
A program that fits a least-squares line on training rows and compares its test error with the baseline's.
Requirements
- Print
baseline MAE Xandmodel MAE Y, both measured on the test rows, to two decimals. - Then print
model wins,baseline winsortie, comparing the two MAEs rounded to two decimals.
Input
Line 1: the number of training rows, followed by those rows x y. Then the number of test rows, followed by those rows x y.
Output
Three lines.
Constraints
2 to 1000 training rows whose x values are not all equal; 1 to 1000 test rows.
Example
Input:
6
1 52
2 55
3 61
4 64
5 70
6 73
2
7 77
8 85
Output:
baseline MAE 18.50
model MAE 1.81
model wins
Why it works
Both predictions are judged on the same unseen rows with the same error measure, so the comparison is fair, and only information from the training rows is used to make them.
Examples
6 1 52 2 55 3 61 4 64 5 70 6 73 2 7 77 8 85
baseline MAE 18.50 model MAE 1.81 model wins
3 0 1 1 3 2 5 2 3 7 4 9
baseline MAE 5.00 model MAE 0.00 model wins
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Compute the slope and intercept from the training rows only.
Hint 2
The baseline predicts the training mean of y for every test row.
Hint 3
Compare the two MAEs after rounding them to two decimals.
Solution
Show one correct solution (try it yourself first)
n = int(input())
train = [tuple(map(float, input().split())) for _ in range(n)]
m = int(input())
test = [tuple(map(float, input().split())) for _ in range(m)]
mx = sum(x for x, _ in train) / n
my = sum(y for _, y in train) / n
b = sum((x - mx) * (y - my) for x, y in train) / sum((x - mx) ** 2 for x, _ in train)
a = my - b * mx
base = round(sum(abs(y - my) for _, y in test) / m, 2)
model = round(sum(abs(y - (a + b * x)) for x, y in test) / m, 2)
print(f"baseline MAE {base:.2f}")
print(f"model MAE {model:.2f}")
print("model wins" if model < base else "baseline wins" if base < model else "tie")