This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python linear regression from scratch, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
In the line y = 3x + 2, what does 3 represent?
Answer: The slope
Why: The slope multiplies the input, so each one-unit increase in x adds 3 to the prediction.
Which errors does mean squared error punish most strongly?
Answer: Large errors
Why: Squaring makes a large residual contribute much more than a small one.
Why check the training input range before predicting?
Answer: The line may extrapolate unreliably outside it
Why: A fitted line can produce a number anywhere, but the evidence only covers the observed range.
What is the output?
xs = [1, 2, 3]
ys = [1, 3, 5]
mean_x = sum(xs) / len(xs)
mean_y = sum(ys) / len(ys)
slope = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys)) / sum((x - mean_x) ** 2 for x in xs)
print(round(slope, 1))Answer: 2.0
Why: The points lie on y = 2x, so the centered covariance divided by variance gives a slope of 2.0.
Complete the prediction formula.
def predict(x, slope, intercept):
return ____ * x + intercept
print(predict(2, 3, 1))Answer: slope
Why: The prediction multiplies the input by the slope, then adds the intercept.
Complete the variance denominator.
xs = [1, 2, 3]
mean_x = sum(xs) / len(xs)
print(sum((x - mean_x) ** ____ for x in xs))Answer: 2
Why: The squared distance from the mean is raised to the power 2.
Complete the mean squared error divisor.
xs = [1, 2, 3]
ys = [2, 4, 5]
slope = 1.5
intercept = 2 / 3
total = sum((y - (slope * x + intercept)) ** 2 for x, y in zip(xs, ys))
mse = total / ____
print(round(mse, 2))Answer: len(xs)
Why: The total squared error is divided by the number of observations.
Complete the range guard.
def within_range(x, xs):
return min(xs) <= x <= ____(xs)
print(within_range(2, [1, 2, 3]))Answer: max
Why: The upper bound comes from the largest observed input.
What does this code print?
def predict(x, slope, intercept):
return slope * x + intercept
print(predict(3, 2.0, 1.0))Answer: 7.0
Why: The line gives 2.0 * 3 + 1.0, which is 7.0.
What does the program output?
xs = [1, 2, 3]
ys = [2, 4, 6]
mean_x = sum(xs) / len(xs)
mean_y = sum(ys) / len(ys)
slope = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys)) / sum((x - mean_x) ** 2 for x in xs)
intercept = mean_y - slope * mean_x
print(round(slope, 1), round(intercept, 1))Answer: 2.0 0.0
Why: The points lie exactly on y = 2x, so the slope is 2.0 and the intercept is 0.0.
What appears on the screen?
xs = [1, 2, 3]
ys = [2, 4, 6]
slope = 2.0
intercept = 0.0
mse = sum((y - (slope * x + intercept)) ** 2 for x, y in zip(xs, ys)) / len(xs)
print(mse)Answer: 0.0
Why: Every residual is zero, so the mean squared error is 0.0.
What is printed?
def within_range(x, xs):
return min(xs) <= x <= max(xs)
print(within_range(4, [1, 2, 3]))Answer: False
Why: The value 4 is greater than the largest training input, so the guard returns False.
Try the questions first
Open the practice page for Python linear regression from scratch and answer the same 12 questions with instant feedback.