Continuous-value prediction usually starts with a simple baseline before more flexible estimators are worth tuning. In scikit-learn, LinearRegression fits an ordinary least squares model through the standard estimator API, so the same script can fit coefficients, make predictions, and score held-out rows.
A small regression smoke test can use the built-in diabetes dataset, a reproducible train/test split, and held-out metrics. Fit the estimator only on training rows so mean_absolute_error and r2_score describe unseen rows rather than rows the model already used.
Ordinary least squares does not regularize coefficients, so high-dimensional or strongly correlated features may need Ridge, Lasso, or a preprocessing pipeline later. The first baseline should still print the split sizes, fitted parameters, a few predictions, and metrics from the same test set.
Steps to train a scikit-learn linear regression model:
- Save the training script as train_linear_regression.py.
- train_linear_regression.py
from sklearn.datasets import load_diabetes from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_absolute_error, r2_score from sklearn.model_selection import train_test_split X, y = load_diabetes(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, ) model = LinearRegression() model.fit(X_train, y_train) predictions = model.predict(X_test) print(f"train rows: {X_train.shape[0]}") print(f"test rows: {X_test.shape[0]}") print(f"intercept: {model.intercept_:.2f}") print(f"coefficients: {model.coef_.round(2).tolist()}") print(f"first predictions: {predictions[:3].round(1).tolist()}") print(f"mean absolute error: {mean_absolute_error(y_test, predictions):.2f}") print(f"r2 score: {r2_score(y_test, predictions):.3f}")
random_state=42 makes the train/test split reproducible while the script is being checked.
- Run the script from a Python environment that has scikit-learn installed.
$ python3 train_linear_regression.py train rows: 353 test rows: 89 intercept: 151.35 coefficients: [37.9, -241.96, 542.43, 347.7, -931.49, 518.06, 163.42, 275.32, 736.2, 48.67] first predictions: [139.5, 179.5, 134.0] mean absolute error: 42.79 r2 score: 0.453
- Check the split counts before reading the score.
The built-in diabetes dataset has 442 rows, so a test_size=0.2 split leaves 353 training rows and 89 held-out rows. Different counts usually mean the dataset, split size, or random seed changed.
- Compare the fitted parameters with the prediction and metric lines.
intercept_ and coef_ confirm that fit() produced a fitted linear model. The prediction and metric lines confirm that predict() ran against the held-out test rows.
- Replace the sample dataset with your own feature matrix and target vector after the baseline run works.
X, y = load_diabetes(return_X_y=True)
Keep the same split, fit, predict, and metric pattern. Add preprocessing in a Pipeline before LinearRegression when real features need scaling, encoding, or imputation.
Related: How to create a scikit-learn pipeline - Remove the sample script if it was created only for the smoke test.
$ rm train_linear_regression.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.