Recover the OLS intercept from a no-intercept fit on centered data

Quick Overview

Reproduce an ordinary least squares fit with an intercept using only a no-intercept solver: center the data, fit the slopes, then reconstruct the intercept from the means. It tests the normal equations, the centroid property of OLS, and careful return types in NumPy and scikit-learn.

Recover the OLS intercept from a no-intercept fit on centered data

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `recover_intercept(X, y)` that reproduces a with-intercept OLS fit using only a no-intercept solver. `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. Requirements: - Center `X` by its per-column means and `y` by its mean, then fit with `fit_intercept=False` to get the slopes `b`. - Reconstruct `b0 = mean(y) - mean(X, axis=0) @ b`. - Return `(coef, intercept)`: `coef` is a shape-`(3,)` ndarray and `intercept` is a Python `float` (not a 0-d array). - Both must match a plain intercept-fitted regression on the raw data. - No direct intercept fit: no `fit_intercept=True`, no `add_constant`, no appended ones column. Any least-squares backend (`sklearn` `LinearRegression` or `np.linalg.lstsq`) is fine as long as no bias term is fitted. ```hint Where the intercept lives Which point does a with-intercept OLS fit always pass through? Once you have the slopes, that point pins down the intercept. ``` ```hint Watch the return type Check what type NumPy gives you for a vector-vector dot product, and compare it with what the contract asks for. ``` ### 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. - The solver must not fit a bias term in any form. - `coef` and `intercept` are compared with a direct intercept fit on the raw data using a floating-point tolerance. ### What a Strong Answer Covers - Centering by column and a least-squares fit that never fits a bias term. - Reconstruction of the intercept from the means, and why it equals the value a direct intercept fit reports. - The return contract: `coef` with shape `(3,)` and `intercept` as a Python `float`. - A numerical check against a direct intercept fit, with a tolerance. ### Follow-up Questions - Why does centering make the intercept term drop out of the fit? - Would this identity still hold with regularization like Ridge? - How would you adapt the function to per-sample weights? - If you also scale each column by its standard deviation before fitting, how do you map the slopes and intercept back to the raw units?

Overview: Reproduce an ordinary least squares fit with an intercept using only a no-intercept solver: center the data, fit the slopes, then reconstruct the intercept from the means. It tests the normal equations, the centroid property of OLS, and careful return types in NumPy and scikit-learn.

General logo
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement recover_intercept(X, y) that reproduces a with-intercept OLS fit using only a no-intercept solver. 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.

Requirements:

  • Center X by its per-column means and y by its mean, then fit with fit_intercept=False to get the slopes b .
  • Reconstruct b0 = mean(y) - mean(X, axis=0) @ b .
  • Return (coef, intercept) : coef is a shape- (3,) ndarray and intercept is a Python float (not a 0-d array).
  • Both must match a plain intercept-fitted regression on the raw data.
  • No direct intercept fit: no fit_intercept=True , no add_constant , no appended ones column.

Any least-squares backend (sklearn LinearRegression or np.linalg.lstsq) is fine as long as no bias term is fitted.

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.
  • The solver must not fit a bias term in any form.
  • coef and intercept are compared with a direct intercept fit on the raw data using a floating-point tolerance.

What a Strong Answer Covers Guidance

  • Centering by column and a least-squares fit that never fits a bias term.
  • Reconstruction of the intercept from the means, and why it equals the value a direct intercept fit reports.
  • The return contract: coef with shape (3,) and intercept as a Python float .
  • A numerical check against a direct intercept fit, with a tolerance.

Follow-up Questions Guidance

  • Why does centering make the intercept term drop out of the fit?
  • Would this identity still hold with regularization like Ridge?
  • How would you adapt the function to per-sample weights?
  • If you also scale each column by its standard deviation before fitting, how do you map the slopes and intercept back to the raw units?