Get confidence interval from sklearn linear regression in python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Scikit-learn's LinearRegression does not provide confidence intervals for its coefficients because it is designed for prediction, not statistical inference. To get confidence intervals, you need to compute them manually using the variance-covariance matrix, or use statsmodels.OLS which provides them out of the box. This article shows both approaches — manual computation with sklearn and the simpler statsmodels alternative.
The Problem
Method 1: Manual Confidence Intervals with sklearn
Method 2: Using statsmodels (Recommended)
statsmodels.OLS provides confidence intervals, p-values, t-statistics, R-squared, and residual diagnostics in a single summary() call.
Prediction Confidence Intervals
Confidence intervals for predictions (not just coefficients):
Bootstrapped Confidence Intervals
When OLS assumptions are violated, bootstrap provides non-parametric confidence intervals:
Bootstrap works with any sklearn estimator, not just linear regression, and does not require normality assumptions.
Common Pitfalls
- Assuming sklearn provides confidence intervals:
LinearRegressionis a prediction tool, not a statistical inference tool. It does not compute standard errors, p-values, or confidence intervals. Usestatsmodels.OLSfor inference. - Forgetting to add the intercept column for manual computation: The variance-covariance matrix requires an
Xmatrix with a column of ones for the intercept. Without it, the standard errors are wrong. - Confusing confidence intervals with prediction intervals: Confidence intervals describe uncertainty about the mean response. Prediction intervals describe uncertainty about individual future observations and are always wider.
- Violating OLS assumptions: Confidence intervals from the normal equations assume linearity, independence, homoscedasticity, and normally distributed errors. If these are violated, use robust standard errors (
sm.OLS().fit(cov_type='HC3')) or bootstrap. - Using too few bootstrap samples: Bootstrap with 100 samples gives unstable intervals. Use at least 1,000 samples (10,000 for publication-quality results).
Summary
- Sklearn's
LinearRegressiondoes not provide confidence intervals — usestatsmodels.OLSfor built-in support - For manual computation, calculate the variance-covariance matrix from
MSE * inv(X'X)and apply the t-distribution - Use
model.conf_int()in statsmodels for coefficient CIs andmodel.get_prediction().summary_frame()for prediction CIs - Bootstrap confidence intervals work with any estimator and do not require OLS assumptions
- Always distinguish between confidence intervals (mean response) and prediction intervals (individual observations)
Related reading
- get_config missing while loading previously saved model without custom layers
- get_config missing while loading previously saved model without custom layers
- Get Gradients with Keras Tensorflow 2.0
- Get info of exposed models in Tensorflow Serving
- Get first element from a dictionary
- Get fully qualified class name of an object in Python
- Get Keras model input from inside a custom callback
- Get labels from dataset when using tensorflow image_dataset_from_directory
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.