Predict a number
Fit regression models, understand loss, and compare models across validation folds. This module has 3 lessons and takes about 1h 15m. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 11 in the Learning LabFit a line and inspect its errors
Fit a simple relation between synthetic floor area and daily energy demand.
Linear regression predicts a number as intercept + coefficient × feature. fit(X, y) learns those values from examples. predict(X_new) applies the learned relationship to new inputs. Even one feature must be a two-dimensional table, such as [[150]].
An error, or residual, is the observed value minus the prediction. Inspect where the model misses, not only its average error. A line learned from small buildings may fail on a factory far outside the training range. These numbers are invented for practice.
See it in action
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
# Synthetic building area in square metres and daily energy in kWh.
X_train = np.array([[50], [100], [150], [200], [250]], dtype=float)
y_train = np.array([16, 26, 34, 47, 54], dtype=float)
X_test = np.array([[75], [175], [225]], dtype=float)
y_test = np.array([21, 40, 51], dtype=float)
model = LinearRegression().fit(X_train, y_train)
predictions = model.predict(X_test)
print("Intercept:", round(model.intercept_, 3))
print("kWh per square metre:", round(model.coef_[0], 3))
print("Residuals:", (y_test - predictions).round(2))
print("Test MAE:", round(mean_absolute_error(y_test, predictions), 2))Fit a line, check unseen errors, and respect the range of the training data.
Watch a model learn from loss
Implement small gradient updates for a one-parameter synthetic calibration model.
A loss turns prediction errors into a number to minimize. Mean squared error, or MSE, squares each error before averaging, so large misses receive more weight. Our tiny model uses prediction = weight × x, with its intercept fixed at zero.
The gradient tells us how the loss changes when weight changes. Subtract a small learning_rate times the gradient to reduce the loss. A learning rate that is too large can make updates unstable. This code shows the mechanism that later powers neural-network training.
See it in action
import numpy as np
# Synthetic calibration relationship y = 3x, with no intercept.
x = np.array([0, 0.25, 0.5, 0.75, 1.0])
y = 3 * x
weight = 0.0
learning_rate = 0.2
for step in range(60):
prediction = weight * x
error = prediction - y
gradient = 2 * np.mean(x * error)
weight -= learning_rate * gradient
if step in [0, 9, 29, 59]:
loss = np.mean((weight * x - y) ** 2)
print("Step", step + 1, "weight", round(weight, 3), "MSE", round(loss, 5))Training is a loop: predict, measure loss, and update the parameters.
Compare models across folds
Use validation folds to measure how a regression model varies across data splits.
A single train-validation split can give a lucky or unlucky result. Cross-validation repeats fitting on several training portions and evaluates each on its held-out fold. KFold splits independent observations into roughly equal groups.
Here we compare linear regression with a regularized model called Ridge. Ridge discourages large coefficients. Keep scaling inside the pipeline so each fold learns its own scaling. scikit-learn reports negative MAE for scoring because larger scores are preferred; negate it to recover normal errors. Use the final test set only after model selection.
See it in action
import numpy as np
from sklearn.model_selection import KFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge
# Synthetic independent invoice workloads and processing times.
rng = np.random.default_rng(42)
X = rng.uniform(1, 10, size=(30, 2))
y = 4 * X[:, 0] + 2 * X[:, 1] + rng.normal(0, 2, size=30)
cv = KFold(n_splits=3, shuffle=True, random_state=42)
model = make_pipeline(StandardScaler(), Ridge(alpha=1.0))
mae = -cross_val_score(model, X, y, cv=cv, scoring="neg_mean_absolute_error")
print("Validation MAE per fold:", mae.round(2))
print("Mean MAE:", round(mae.mean(), 2))
print("Fold spread:", round(mae.std(), 2))
print("Synthetic workflow example, not financial advice.")Compare validation results across folds and reserve the test set for the final check.
Free, open and run by a nonprofit.
This page is part of Python to AI, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.