Answer key · 12 questions

Python linear regression from scratch Exercises With Answers

All 12 questions for Python linear regression from scratch in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

In the line y = 3x + 2, what does 3 represent?

  1. The number of points
  2. The intercept
  3. The slopecorrect
  4. The error

Answer: The slope

Why: The slope multiplies the input, so each one-unit increase in x adds 3 to the prediction.

02 · Multiple choice

Which errors does mean squared error punish most strongly?

  1. Errors near zero
  2. Small errors
  3. Large errorscorrect
  4. Only negative errors

Answer: Large errors

Why: Squaring makes a large residual contribute much more than a small one.

03 · Multiple choice

Why check the training input range before predicting?

  1. It changes the slope automatically
  2. The line may extrapolate unreliably outside itcorrect
  3. It makes Python run faster
  4. It removes the intercept

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.

04 · Multiple choice

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))
  1. 2.0correct
  2. 1.0
  3. 0.5
  4. 3.0

Answer: 2.0

Why: The points lie on y = 2x, so the centered covariance divided by variance gives a slope of 2.0.

05 · Fill in the blank

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.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson