bhylu.com

Advertisements

AI Linear Regression

Fit a numeric target and inspect a held-out error.

How it works

Linear regression estimates coefficients that minimise squared residuals under its model. A coefficient describes the fitted association in the chosen representation, not a causal effect. This local scikit-learn example learns an exact synthetic line from four sessions and evaluates two different inputs. Noise-free constructed data makes the arithmetic inspectable.

Run the code

This is a local Python example, not supported by Bhylu’s plain Python browser editor. In a separate virtual environment use Python 3.12.14 with the pinned requirements: NumPy 2.5.4 and scikit-learn 1.9.1. Run python -m pip install -r requirements-local.txt in that environment, then run this complete downloaded source. These are reviewed versions, not a newest-release promise. No external dataset, provider account, database or PHP setting is needed.

Download the complete linear-regression.py

Example

linear-regression.py

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

model = LinearRegression().fit([[1],[2],[3],[4]], [7,9,11,13])
predicted = model.predict([[1.5],[3.5]])
print("predictions:", predicted.round(1).tolist())
print(f"MAE: {mean_absolute_error([8,12],predicted):.3f}")

Expected result

predictions: [8.0, 12.0]
MAE: 0.000

The training and held-out coordinates differ, but both were deliberately generated from the same exact line. A zero error here tests implementation; it is not evidence of a useful real-world forecast. Real evaluation needs representative observations and uncertainty.

Technical references