Linear regression — one gradient descent step
Background
Gradient descent improves the line by repeatedly stepping the parameters downhill — opposite the gradient of the SSR — scaled by a learning rate :
The lesson derives the two gradients of with the chain rule:
The gradient points toward higher loss, so subtracting it moves toward lower loss. The learning rate sets the step size: too large overshoots and diverges, too small crawls. This problem is one single step — the body of the training loop.
Problem statement
Implement gd_step(x, y, m, c, lr) that performs one gradient-descent update on the SSR and returns the new (m, c).
Input
x— array-like of inputs.y— array-like of true targets, the same length.m—float, the current slope.c—float, the current intercept.lr—float, the learning rate .
Output
Returns a tuple of two Python floats: the updated (m, c).
Examples
Example 1 — one step off a zero start
Input: x = [1, 2], y = [1, 2], m = 0, c = 0, lr = 0.01
Output: (0.1, 0.06)
Explanation: residuals are , so and ; then and .
Example 2 — already optimal, so it barely moves
Input: x = [1, 2, 3], y = [2, 4, 6], m = 2, c = 0, lr = 0.1
Output: (2.0, 0.0)
Explanation: the fit is perfect, both gradients are 0, so the parameters stay put.
Constraints
- Compute the gradients with the exact formulas above (raw sum, leading ).
- Subtract
lr * gradientfrom each parameter (descent, not ascent). - Compute both gradients from the same old
(m, c)before updating either. - Return plain floats
(m_new, c_new).
Notes
- Call this repeatedly — feeding
(m, c)back in — and the line converges, exactly the animation in the lesson. A correct single step is the whole algorithm; the loop is just repetition.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: one step off a zero start
- •Reference step from zero on y = 2x + 1
- •Sample step from zero on a shifted line