Apply a fitted line ŷ = slope×x + intercept to new x values via a loop. Slope=0.8 and intercept=1.6 are hardcoded from the least-squares fit on x=[1,2,3,4,5], y=[2,4,4,4,6] (see least-squares-line). Library: fit with scipy.stats.linregress then apply the same formula to x_new. RESULT: list of predicted y values (rounded).

By hand

With the library

scipy.stats.linregress fits the line; the same slope×x+intercept formula is applied to x_new. The hardcoded naive values equal linregress output to 4 dp, so RESULT matches.

naive.py
slope = 0.8
intercept = 1.6
x_new = [2, 4, 6]
preds = []
for xv in x_new:
    preds.append(round(slope * xv + intercept, 4))
print('RESULT:', preds)
library.py
from scipy import stats
from dalib.display import set_display
set_display()

x = [1, 2, 3, 4, 5]
y = [2, 4, 4, 4, 6]
fit = stats.linregress(x, y)
slope = fit.slope
intercept = fit.intercept
x_new = [2, 4, 6]
preds = [round(float(slope * xv + intercept), 4) for xv in x_new]
print('slope:', round(float(slope), 4))
print('intercept:', round(float(intercept), 4))
print('RESULT:', preds)
slope: 0.8
intercept: 1.6
RESULT: [3.2, 4.8, 6.4]

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 underlying line was fit to a handful of points, so any prediction is a mechanism demo, not a valid forecast. Predicting outside the observed x-range is an extrapolation the data cannot support, and no interval or significance claim is warranted at this sample size; real inference needs an adequate sample and assumption checks.

Implementation notes

  • Parity: naive hardcodes slope=0.8, intercept=1.6 — the exact rounded values scipy.stats.linregress returns for this data. Any floating-point difference in intercept (scipy gives 1.5999...≈1.6) is absorbed by round(..., 4).
  • Extrapolation (x=6) applies the model outside the training range [1,5]. The formula is identical but reliability degrades as x moves further from the training data.
  • Cross-reference: least-squares-line (this chapter) for the fit that produced slope and intercept.