Fit OLS without an intercept on mean-centered data and match the raw-data slopes
Quick Overview
Center the features and target, fit scikit-learn's LinearRegression without an intercept, and return slopes that match an intercept fit on the raw data. It tests why mean-centering absorbs the OLS intercept and how to check the identity numerically in Python.
Fit OLS without an intercept on mean-centered data and match the raw-data slopes
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `def centered_fit_matches(X: np.ndarray, y: np.ndarray) -> np.ndarray`. `X` is a float array of shape `(40, 3)` with non-zero column means, and `y` is a float array of shape `(40,)` with a non-zero mean. Center both, fit OLS without an intercept, and return the slopes, which match an intercept fit on the raw data.
Requirements:
- Subtract each column's mean from `X` (`axis=0`) and the mean of `y` from `y`.
- Fit `sklearn.linear_model.LinearRegression` with `fit_intercept=False` on the centered data.
- Return only the shape-`(3,)` coefficient `np.ndarray`, not the model.
The result should pass this check:
```python
from sklearn.linear_model import LinearRegression
coef = centered_fit_matches(X, y)
np.allclose(coef, LinearRegression().fit(X, y).coef_) # -> True
```
```hint Where the intercept comes from
Write the OLS intercept in terms of the slopes and the means of `X` and `y`. What does that formula give when every mean is zero?
```
### Constraints & Assumptions
- `X` is float with shape `(40, 3)` and non-zero column means; `y` is float with shape `(40,)` and a non-zero mean.
- NumPy and scikit-learn are available.
- The returned slopes are compared with the raw-data intercept fit using a floating-point tolerance.
### What a Strong Answer Covers
- A correct implementation that centers by column, fits with `fit_intercept=False`, and returns only `coef_` with shape `(3,)`.
- Why the intercept fit and the centered fit without an intercept produce the same slopes.
- How to confirm the match numerically, with a tolerance.
- Mistakes that break the identity, such as the wrong axis for the means or centering only one side.
### Follow-up Questions
- Why does centering make the intercept unnecessary?
- Does the same identity hold if you standardize (scale) the columns too?
- Do the residuals and $R^2$ change when you fit on the centered data?
- How would you recover the raw-data intercept from the centered fit?
Overview: Center the features and target, fit scikit-learn's LinearRegression without an intercept, and return slopes that match an intercept fit on the raw data. It tests why mean-centering absorbs the OLS intercept and how to check the identity numerically in Python.
Fit OLS without an intercept on mean-centered data and match the raw-data slopes
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0
Implement def centered_fit_matches(X: np.ndarray, y: np.ndarray) -> np.ndarray. X is a float array of shape (40, 3) with non-zero column means, and y is a float array of shape (40,) with a non-zero mean. Center both, fit OLS without an intercept, and return the slopes, which match an intercept fit on the raw data.
Requirements:
Subtract each column's mean from
X
(
axis=0
) and the mean of
y
from
y
.
Fit
sklearn.linear_model.LinearRegression
with
fit_intercept=False
on the centered data.
Return only the shape-
(3,)
coefficient
np.ndarray
, not the model.