Before training anything, use a given line: predict each example and measure how wrong the line is.
What to build
A program that applies a line to some examples and prints its mean squared error.
Requirements
- Print the predictions with 2 decimal places, separated by spaces.
- Then print
MSE Mwith 3 decimal places.
Input
Line 1: w and b. Line 2: the x values. Line 3: the true y values.
Output
Two lines.
Constraints
1 to 1000 examples.
Example
Input:
2 1
1 2 3
3 5 8
Output:
3.00 5.00 7.00
MSE 0.333
Why it works
The mean squared error turns all the residuals into one number, so lines can be compared: training looks for the line that makes it smallest.
Examples
Example 1 · Input
2 1 1 2 3 3 5 8
Expected output
3.00 5.00 7.00 MSE 0.333
Example 2 · Input
0.5 0 2 4 1 1
Expected output
1.00 2.00 MSE 0.500
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each prediction is w × x + b.
Hint 2
The MSE is the mean of (prediction − y)².
Solution
Show one correct solution (try it yourself first)
w, b = map(float, input().split())
xs = list(map(float, input().split()))
ys = list(map(float, input().split()))
predictions = [w * x + b for x in xs]
mse = sum((p - y) ** 2 for p, y in zip(predictions, ys)) / len(xs)
print(*[f"{p:.2f}" for p in predictions])
print(f"MSE {mse:.3f}")