Compare sklearn and statsmodels OLS slope coefficients in Python
Quick Overview
Fit the same linear regression with scikit-learn and statsmodels in Python and return both slope vectors in a matching layout. It tests where each library stores the intercept, how add_constant builds the design matrix, and why the two least-squares fits agree.
Compare sklearn and statsmodels OLS slope coefficients in Python
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Fit the same regression of `y` on `X` twice: with `sklearn.linear_model.LinearRegression` (default `fit_intercept=True`) on raw `X`, and with `statsmodels.api.OLS` on `sm.add_constant(X)`. Return the 2-tuple `(sk_coef, sm_coef)` of slope vectors.
```python
def sklearn_vs_statsmodels(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
...
```
- `sk_coef` is sklearn's `coef_`, shape `(3,)`.
- `sm_coef` is the statsmodels `params` vector with the intercept (`params[0]`) stripped, shape `(3,)`, in `X` column order.
- Both arrays must agree to floating-point tolerance.
```hint Where the intercept lives
Check which attribute holds the intercept in each library, and at which position `add_constant` puts the column of ones.
```
### Constraints & Assumptions
- `X` is a float ndarray of shape `(40, 3)` with no column of ones.
- `y` is a float ndarray of shape `(40,)`.
### What a Strong Answer Covers
- Fits both models with an intercept, so each estimates the same three slopes plus one intercept.
- Knows that sklearn keeps the intercept in `intercept_` and the slopes in `coef_`, while statsmodels returns a single `params` vector with the constant first.
- Removes the constant so `sm_coef` has shape `(3,)` and follows the original `X` column order.
- Explains why the two fits agree and checks agreement with a tolerance such as `np.allclose`.
### Follow-up Questions
- Why can `add_constant` silently fail to add a column, and how do you guard against it?
- How would you also compare the intercepts?
- sklearn does not report standard errors. How would you compute them for the sklearn fit and check them against statsmodels?
Overview: Fit the same linear regression with scikit-learn and statsmodels in Python and return both slope vectors in a matching layout. It tests where each library stores the intercept, how add_constant builds the design matrix, and why the two least-squares fits agree.
Compare sklearn and statsmodels OLS slope coefficients in Python
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0
Fit the same regression of y on X twice: with sklearn.linear_model.LinearRegression (default fit_intercept=True) on raw X, and with statsmodels.api.OLS on sm.add_constant(X). Return the 2-tuple (sk_coef, sm_coef) of slope vectors.
sm_coef
is the statsmodels
params
vector with the intercept (
params[0]
) stripped, shape
(3,)
, in
X
column order.
Both arrays must agree to floating-point tolerance.
Constraints & Assumptions
X
is a float ndarray of shape
(40, 3)
with no column of ones.
y
is a float ndarray of shape
(40,)
.
What a Strong Answer Covers Guidance
Fits both models with an intercept, so each estimates the same three slopes plus one intercept.
Knows that sklearn keeps the intercept in
intercept_
and the slopes in
coef_
, while statsmodels returns a single
params
vector with the constant first.
Removes the constant so
sm_coef
has shape
(3,)
and follows the original
X
column order.
Explains why the two fits agree and checks agreement with a tolerance such as
np.allclose
.
Follow-up Questions Guidance
Why can
add_constant
silently fail to add a column, and how do you guard against it?
How would you also compare the intercepts?
sklearn does not report standard errors. How would you compute them for the sklearn fit and check them against statsmodels?