Fit OLS with an intercept and return only the slope coefficients

Quick Overview

Implement a function that fits ordinary least squares with an intercept on every column of a feature matrix and returns only the slope coefficients in column order. It tests building the design matrix, keeping track of the intercept's coefficient, and explaining why the slopes depend on fitting the intercept.

Fit OLS with an intercept and return only the slope coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fit_coefficients(X: np.ndarray, y: np.ndarray) -> np.ndarray` that fits OLS of `y` on all columns of `X` with an intercept and returns only the slopes. Any method that yields the OLS solution is accepted. - Fit OLS with an intercept term. - Return a 1-D `np.ndarray` of shape `(p,)`, one coefficient per column of `X` in column order. - Leave the intercept out of the result, so its length is `p`, not `p + 1`. ```hint Track the intercept's coefficient Work out where the intercept's coefficient ends up in whatever your solver returns, then return everything else in the column order of `X`. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` and `y` has shape `(n,)`. - The checked inputs have n = 40 and p = 3, `X` has full column rank, and there are no missing values. ### What a Strong Answer Covers - Fitting the intercept, either through a design matrix built for it or through a library that fits one by default. - Returning only the `p` slopes, in column order, as an array of shape `(p,)`. - Why full column rank makes the OLS solution unique. - How the slopes change when the intercept is left out of the fit. ### Follow-up Questions - Why does adding a ones column let `lstsq` fit an intercept? - How would you extend this to return the slopes and the intercept separately? - How do these slopes relate to a regression of centered `y` on centered `X`? - What happens to the coefficients if `X` is not full rank?

Overview: Implement a function that fits ordinary least squares with an intercept on every column of a feature matrix and returns only the slope coefficients in column order. It tests building the design matrix, keeping track of the intercept's coefficient, and explaining why the slopes depend on fitting the intercept.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def fit_coefficients(X: np.ndarray, y: np.ndarray) -> np.ndarray that fits OLS of y on all columns of X with an intercept and returns only the slopes. Any method that yields the OLS solution is accepted.

  • Fit OLS with an intercept term.
  • Return a 1-D np.ndarray of shape (p,) , one coefficient per column of X in column order.
  • Leave the intercept out of the result, so its length is p , not p + 1 .

Constraints & Assumptions

  • X has shape (n, p) and y has shape (n,) .
  • The checked inputs have n = 40 and p = 3, X has full column rank, and there are no missing values.

What a Strong Answer Covers Guidance

  • Fitting the intercept, either through a design matrix built for it or through a library that fits one by default.
  • Returning only the p slopes, in column order, as an array of shape (p,) .
  • Why full column rank makes the OLS solution unique.
  • How the slopes change when the intercept is left out of the fit.

Follow-up Questions Guidance

  • Why does adding a ones column let lstsq fit an intercept?
  • How would you extend this to return the slopes and the intercept separately?
  • How do these slopes relate to a regression of centered y on centered X ?
  • What happens to the coefficients if X is not full rank?