Apply a fitted slope and intercept to predict y for new x values: y=slope·x+intercept. A loop computes one prediction per new x. Library: LinearRegression().fit(X, y).predict(X_new) — same formula vectorized. RESULT: list of predictions (rounded).

By hand

slope=0.8, intercept=1.6 (from normal-equation-1d, this chapter). x_new=[3,6,10]. Predictions: 0.8·3+1.6=4.0, 0.8·6+1.6=6.4, 0.8·10+1.6=9.6.

naive.py
Replay: real traced execution (multi-file project)
slope = 0.8
intercept = 1.6
x_new = [3, 6, 10]
preds = []
for x in x_new:
    y = slope * x + intercept
    preds.append(y)
print('RESULT:', [round(v, 4) for v in preds])
  1. slope ← 0.8

    1slope = 0.82intercept = 1.6
    values this step0.8slope
  2. intercept ← 1.6

    1slope = 0.82intercept = 1.63x_new = [3, 6, 10]
    values this step1.6intercept
  3. x_new ← [3, 6, 10]

    2intercept = 1.63x_new = [3, 6, 10]4preds = []
    values this step[3, 6, 10]x_new
  4. preds ← []

    3x_new = [3, 6, 10]4preds = []5for x in x_new:
    values this step[]preds
  5. x ← 3

    4preds = []5for x in x_new:6    y = slope * x + intercept
    values this step3x
  6. y ← 4.0

    5for x in x_new:6    y = slope * x + intercept7    preds.append(y)
    values this step4.0y
  7. preds ← [4.0]

    6    y = slope * x + intercept7    preds.append(y)8print('RESULT:', [round(v, 4) for v in preds])
    values this step[] [4.0]preds
  8. x ← 6

    4preds = []5for x in x_new:6    y = slope * x + intercept
    values this step3 6x
  9. y ← 6.4

    5for x in x_new:6    y = slope * x + intercept7    preds.append(y)
    values this step4.0 6.4y
  10. preds ← [4.0, 6.4]

    6    y = slope * x + intercept7    preds.append(y)8print('RESULT:', [round(v, 4) for v in preds])
    values this step[4.0] [4.0, 6.4]preds
  11. x ← 10

    4preds = []5for x in x_new:6    y = slope * x + intercept
    values this step6 10x
  12. y ← 9.6

    5for x in x_new:6    y = slope * x + intercept7    preds.append(y)
    values this step6.4 9.6y
  13. preds ← [4.0, 6.4, 9.6]

    6    y = slope * x + intercept7    preds.append(y)8print('RESULT:', [round(v, 4) for v in preds])
    values this step[4.0, 6.4] [4.0, 6.4, 9.6]preds
  14. for x in x_new:

    4preds = []5for x in x_new:6    y = slope * x + intercept
  15. stdout ← RESULT: [4.0, 6.4, 9.6]

    7    preds.append(y)8print('RESULT:', [round(v, 4) for v in preds])
    values this stepRESULT: [4.0, 6.4, 9.6]stdout

With scikit-learn

model.predict(X_new) applies the fitted parameters to new inputs. X_new must be 2D (one column), matching the shape used in .fit.

library.py
from sklearn.linear_model import LinearRegression
from dalib.display import set_display
set_display()

x = [1, 2, 3, 4, 5]
y = [2, 4, 4, 4, 6]
X = [[v] for v in x]
model = LinearRegression()
model.fit(X, y)
x_new = [3, 6, 10]
X_new = [[v] for v in x_new]
preds = [round(float(p), 4) for p in model.predict(X_new)]
print('x_new:', x_new)
print('RESULT:', preds)
x_new: [3, 6, 10]
RESULT: [4.0, 6.4, 9.6]

Honesty

This lesson shows the computation of a prediction from a fitted line exactly, on a tiny pinned sample. The arithmetic is correct and reproducible, but the line was fit to a handful of points, so predicting a new point is a mechanism demo, not a valid forecast. Predicting outside the observed x-range is an extrapolation the data cannot support, and no confidence claim is warranted at this sample size; a trustworthy prediction needs an adequate sample and a held-out check.

Implementation notes

  • slope and intercept are hardcoded from normal-equation-1d (this chapter) — prediction requires only the fitted parameters and new x values.
  • model.predict requires 2D input; [[v] for v in x_new] reshapes to match the one-column X used at fit time.
  • Cross-reference: predict-from-line (python-stats ch08) uses the same arithmetic by hand; this lesson applies it via the sklearn .predict API.
  • x=6 and x=10 are outside the training range [1,5] — the model extrapolates linearly; whether extrapolation is valid depends on the domain.