Gradient Descent Step
You are implementing the core optimization step for training a linear regression model. At each training iteration, gradient descent computes how much each weight contributed to the prediction error, then adjusts the weights in the direction that reduces error.
Given a feature matrix X (n_samples × n_features), target vector y (n_samples,), weight vector w (n_features,), and learning rate alpha, perform one gradient descent update:
predictions=X@werrors=predictions-ygradient=X.T@errors/n_samplesnew_w=w-alpha*gradient
Return new_w rounded to 6 decimal places as a list.
Example 1
([[1, 2], [3, 4]], [3, 7], [1, 1], 0.1)[1.0, 1.0]Predictions [3,7] exactly match targets. Errors are [0,0]. Gradient is [0,0]. Weights unchanged.
Example 2
([[1]], [10], [0], 0.1)[1.0]Prediction=0, error=-10, gradient=X.T@[-10]/1=-10. w_new=0-0.1*(-10)=1.0. Weight increases toward target.
Example 3
([[2], [3]], [4, 6], [0], 0.5)[6.5]Predictions [0,0], errors [-4,-6], gradient=(2*(-4)+3*(-6))/2=-13. w_new=0-0.5*(-13)=6.5.
- ›2 <= n_samples <= 100
- ›1 <= n_features <= 20
- ›-100 <= X[i][j], y[i], w[j] <= 100
- ›0 < alpha <= 1.0
Reference solution available after you attempt the question.
Ready to solve it?
Start a session on Mockbit #76. Write your code, run it against hidden tests, and get graded with specific critique on each axis.