Neural Mastery
← Back to Practice

One Gradient Descent Step for Linear Regression

Difficulty: Medium · Concept: Linear Regression — Gradient Descent

For single-feature linear regression y^=wx+b\hat{y} = wx + b, one step of gradient descent updates both parameters using the gradients derived in Linear Regression, Section 3:

Jw=2ni=1n(y^(i)y(i))x(i)Jb=2ni=1n(y^(i)y(i))\frac{\partial J}{\partial w} = \frac{2}{n} \sum_{i=1}^{n} (\hat{y}^{(i)} - y^{(i)}) x^{(i)} \qquad \frac{\partial J}{\partial b} = \frac{2}{n} \sum_{i=1}^{n} (\hat{y}^{(i)} - y^{(i)})

wwαJwbbαJbw \leftarrow w - \alpha \frac{\partial J}{\partial w} \qquad b \leftarrow b - \alpha \frac{\partial J}{\partial b}

Your task: implement gradient_descent_step(x, y, w, b, alpha) — given the current w, b, and learning rate alpha, compute one full-batch gradient step over the dataset (x, y) and return the updated (w, b) as a tuple. Don't mutate the inputs in place; return new values.

Implement it yourself
assert abs(gradient_descent_step([1, 2, 3], [2, 4, 6], 0, 0, 0.1)[0] - 1.8666666666666667) < 1e-6 assert abs(gradient_descent_step([1, 2, 3], [2, 4, 6], 0, 0, 0.1)[1] - 0.8) < 1e-6 assert abs(gradient_descent_step([1], [1], 0, 0, 0.5)[0] - 1.0) < 1e-6 assert abs(gradient_descent_step([2, 4], [3, 5], 1, 0, 0.1)[0] - 1.6) < 1e-6 assert abs(gradient_descent_step([2, 4], [3, 5], 1, 0, 0.1)[1] - 0.2) < 1e-6 assert abs(gradient_descent_step([1, 2], [1, 2], 1, 0, 0.1)[0] - 1.0) < 1e-6 assert abs(gradient_descent_step([1, 2], [1, 2], 1, 0, 0.1)[1] - 0.0) < 1e-6

Next: K-Means: One Assignment Step

Last updated Sep 5, 2026Edit this pageReport an issue