Start learning free

LESSON 77 OF 165 · MACHINE LEARNING: ML INTERVIEW QUESTIONS

Linear Regression with Gradient Descent

Practice

A classic interview question: "Fit a linear regression with gradient descent, using numpy only." The idea in one sentence: start with a wrong line, measure how wrong it is, move it a little in the direction that makes it less wrong, and repeat.

The line is y = w·x + b. With a 2D array X (one row per example, one column per feature), X @ w multiplies each row by the weights w and adds the results up: one prediction per row. The mean squared error squares each error pred - y and takes the average. Its gradient says which way the error grows, so you step the other way. Here gradient descent finds the one number m closest to three values:

values = np.array([4.0, 6.0, 11.0])
m = 0.0
for step in range(100):
    grad = 2 * (m - values).mean()
    m = m - 0.1 * grad
print(round(m, 3))   # 7.0, the mean of the values

The line needs two gradients. For w: 2 times X.T @ error, divided by len(X). X.T is X turned on its side, so each column is multiplied by the errors and summed: one number per weight. For b: 2 times error.mean(), the average error. The learning rate lr sets how big each step is, and epochs is how many steps the loop takes.

The six points in the editor lie on y = 3x + 2. With lr = 0.05, your program must print these lines (the last three come from the lesson's check, which also fits two columns and compares your function with scikit-learn's LinearRegression):

After 10 steps: w = [3.223] b = 1.208
After 1000 steps: w = [3.] b = 2.0
Mean squared error now: 0.0
Two columns, y = 1.5 x1 - 2 x2 + 0.5: w = [ 1.5 -2. ] b = 0.5
Same as scikit-learn's LinearRegression on noisy data: True

Your task: the points and the start of fit_line are in the editor: w starts as np.zeros(X.shape[1]), an array with one zero per column of X, and b as 0.0. Inside fit_line, write the loop: the predictions, the errors, the two gradients and the two updates.


Assignment
Follow the Coding Tutorial to write the gradient descent loop inside fit_line.


Hint
Every line inside the loop sits 8 spaces in. Subtract lr times each gradient, don't add it: the gradient points toward more error.


STEP 1 OF 6
    Ctrl + Enter to check