Regression
Predict from Line
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
slope=0.8, intercept=1.6 (from least-squares-line). Predictions for x_new=[2,4,6]: 0.8×2+1.6=3.2, 0.8×4+1.6=4.8, 0.8×6+1.6=6.4. x=2 and x=4 interpolate; x=6 extrapolates beyond the training range.
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)
slope ← 0.8
1slope = 0.82intercept = 1.6values this step0.8slopeintercept ← 1.6
1slope = 0.82intercept = 1.63x_new = [2, 4, 6]values this step1.6interceptx_new ← [2, 4, 6]
2intercept = 1.63x_new = [2, 4, 6]4preds = []values this step[2, 4, 6]x_newpreds ← []
3x_new = [2, 4, 6]4preds = []5for xv in x_new:values this step[]predsxv ← 2
4preds = []5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))values this step2xvpreds ← [3.2]
5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))7print('RESULT:', preds)values this step[] → [3.2]predsxv ← 4
4preds = []5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))values this step2 → 4xvpreds ← [3.2, 4.8]
5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))7print('RESULT:', preds)values this step[3.2] → [3.2, 4.8]predsxv ← 6
4preds = []5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))values this step4 → 6xvpreds ← [3.2, 4.8, 6.4]
5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))7print('RESULT:', preds)values this step[3.2, 4.8] → [3.2, 4.8, 6.4]predsfor xv in x_new:
4preds = []5for xv in x_new:6 preds.append(round(slope * xv + intercept, 4))stdout ← RESULT: [3.2, 4.8, 6.4]
6 preds.append(round(slope * xv + intercept, 4))7print('RESULT:', preds)values this stepRESULT: [3.2, 4.8, 6.4]stdout
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.
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.