Run gradient descent for a line ŷ = w x + b with the mean squared error, exactly as in the lesson: start at w = 0, b = 0, and on each step compute both gradients with the current w and b, then update both.
dw = (2/n) × Σ (w xᵢ + b − yᵢ) xᵢ w ← w − rate × dw
db = (2/n) × Σ (w xᵢ + b − yᵢ) b ← b − rate × db
Input
The first line has n, the learning rate and the number of steps (1 ≤ n ≤ 50, 1 ≤ steps ≤ 1000). Each of the next n lines has a point x y.
Output
w W b B after the last step, with 4 decimal places each.
Examples
Example 1 · Input
5 0.03 1 1 2 2 4.5 3 5.5 4 8.5 5 9.5
Expected output
w 1.3080 b 0.3600
Example 2 · Input
5 0.03 30 1 2 2 4.5 3 5.5 4 8.5 5 9.5
Expected output
w 1.8571 b 0.4548
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each step: work out dw = (2/n) Σ (wx + b − y) x and db = (2/n) Σ (wx + b − y) with the current w and b.
Hint 2
Then update both: w = w − rate·dw, b = b − rate·db. Compute both gradients before changing either.
Hint 3
Start from w = 0 and b = 0.
Solution
Show one correct solution (try it yourself first)
n, rate, steps = input().split()
n, rate, steps = int(n), float(rate), int(steps)
points = [tuple(map(float, input().split())) for _ in range(n)]
w = b = 0.0
for _ in range(steps):
dw = db = 0.0
for x, y in points:
error = w * x + b - y
dw += 2 / n * error * x
db += 2 / n * error
w, b = w - rate * dw, b - rate * db
print(f"w {w:.4f} b {b:.4f}")