📘
Gradient Descent for Linear Regression
Implement batch gradient descent for simple linear regression.
Given data points (x,y), initial weight w and bias b, learning rate α, and number of iterations, update w and b to minimize Mean Squared Error:
MSE=n1∑i=1n(yi−(w⋅xi+b))2
The gradients are: ∂w∂MSE=n−2∑i=1nxi(yi−(w⋅xi+b)) ∂b∂MSE=n−2∑i=1n(yi−(w⋅xi+b))
Return a tuple (w, b) after all iterations, both rounded to 4 decimal places.
Example:
Input:
X = [1, 2, 3] y = [2, 4, 6] w = 0.0, b = 0.0 learning_rate = 0.1, iterations = 100
Output:
(1.9715, 0.0647)
Reasoning:
- We start with the given data points (x,y): (1,2), (2,4), (3,6), initial weight w=0.0, bias b=0.0, learning rate α=0.1, and number of iterations =100.
- We calculate the gradients ∂w∂MSE and ∂b∂MSE using the given formulas: ∂w∂MSE=n−2∑i=1nxi(yi−(w⋅xi+b)) and ∂b∂MSE=n−2∑i=1n(yi−(w⋅xi+b)), and update w and b using w=w−α⋅∂w∂MSE and b=b−α⋅∂b∂MSE for each iteration.
- We repeat the process of calculating gradients and updating w and b for 100 iterations, which minimizes the Mean Squared Error MSE=n1∑i=1n(yi−(w⋅xi+b))2.
- After 100 iterations, we obtain the updated values of w and b, which are then rounded to 4 decimal places, resulting in the output (1.9715,0.0647).
Constraints:
- X and y are lists of equal length
- learning_rate > 0, iterations >= 1
- Initial w and b are given as floats
- Return tuple (w, b) rounded to 4 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.