Linear Regression
Predict from Fitted Line
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.
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])
slope ← 0.8
1slope = 0.82intercept = 1.6values this step0.8slopeintercept ← 1.6
1slope = 0.82intercept = 1.63x_new = [3, 6, 10]values this step1.6interceptx_new ← [3, 6, 10]
2intercept = 1.63x_new = [3, 6, 10]4preds = []values this step[3, 6, 10]x_newpreds ← []
3x_new = [3, 6, 10]4preds = []5for x in x_new:values this step[]predsx ← 3
4preds = []5for x in x_new:6 y = slope * x + interceptvalues this step3xy ← 4.0
5for x in x_new:6 y = slope * x + intercept7 preds.append(y)values this step4.0ypreds ← [4.0]
6 y = slope * x + intercept7 preds.append(y)8print('RESULT:', [round(v, 4) for v in preds])values this step[] → [4.0]predsx ← 6
4preds = []5for x in x_new:6 y = slope * x + interceptvalues this step3 → 6xy ← 6.4
5for x in x_new:6 y = slope * x + intercept7 preds.append(y)values this step4.0 → 6.4ypreds ← [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]predsx ← 10
4preds = []5for x in x_new:6 y = slope * x + interceptvalues this step6 → 10xy ← 9.6
5for x in x_new:6 y = slope * x + intercept7 preds.append(y)values this step6.4 → 9.6ypreds ← [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]predsfor x in x_new:
4preds = []5for x in x_new:6 y = slope * x + interceptstdout ← 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.
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.predictrequires 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.predictAPI. - x=6 and x=10 are outside the training range [1,5] — the model extrapolates linearly; whether extrapolation is valid depends on the domain.