Linear regression and regression metrics
scikit-learn 1.9.1
Fit a numerical model and interpret prediction error in the target’s units.
On this page
Fit and predict
LinearRegression fits an intercept and coefficients by ordinary least squares. Predictions use the same feature columns in the same order. Coefficients alone do not establish a causal effect.
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit([[1], [2], [3]], [3, 5, 7])
print(round(float(model.predict([[4]])[0]), 2))
Example output
9.0
Choose MAE, RMSE, or R²
MAE is the average absolute error and has the target’s units. RMSE gives greater weight to large errors and has the same units. R² describes performance relative to the mean-target baseline in the evaluation set and can be negative.
from sklearn.metrics import mean_absolute_error
actual = [100, 200, 300]
predicted = [90, 210, 320]
print(round(mean_absolute_error(actual, predicted), 2))
Example output
13.33