Fit OLS regression through the origin and return the slope coefficients

Quick Overview

Implement ordinary least squares regression through the origin, fitting a response on three predictors with no intercept and returning the slope vector. It tests the normal equations without a ones column, why the slopes shift when the intercept is dropped, and why R-squared can mislead for such a model.

Fit OLS regression through the origin and return the slope coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_without_intercept(X, y)` that fits OLS of `y` on the columns of `X` with no intercept (regression through the origin) and returns the slope coefficients. - No intercept: do not add a ones column and do not center `X` or `y`. - Return a 1-D `np.ndarray` of shape `(p,)` in the column order of `X`. - Any technique is accepted, for example `LinearRegression(fit_intercept=False)` or `np.linalg.lstsq(X, y)`. ```hint Use X exactly as given Write the normal equations for `X` with nothing added or subtracted, then check the shape your solver returns when `y` is 1-D. ``` ### Constraints & Assumptions - `X` has shape `(40, 3)` and `y` has shape `(40,)`, both floats, so the result has shape `(3,)`. - The columns of `X` are not mean-centered and `y` does not have mean zero. ### What a Strong Answer Covers - Solving least squares on `X` as given, with no ones column and no centering. - Returning a 1-D array of shape `(p,)` in the column order of `X`. - Explaining why the slopes differ from those of a model with an intercept when the means are nonzero. - Choosing a numerically stable solver and knowing what it does when `X` is close to rank deficient. ### Follow-up Questions - When is regression through the origin actually justified? - Why can R-squared be misleading for a no-intercept model? - Do the residuals of a no-intercept fit still sum to zero, and which property do they keep? - How would you check from the data whether the intercept can be dropped?

Overview: Implement ordinary least squares regression through the origin, fitting a response on three predictors with no intercept and returning the slope vector. It tests the normal equations without a ones column, why the slopes shift when the intercept is dropped, and why R-squared can mislead for such a model.

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

Implement fit_without_intercept(X, y) that fits OLS of y on the columns of X with no intercept (regression through the origin) and returns the slope coefficients.

  • No intercept: do not add a ones column and do not center X or y .
  • Return a 1-D np.ndarray of shape (p,) in the column order of X .
  • Any technique is accepted, for example LinearRegression(fit_intercept=False) or np.linalg.lstsq(X, y) .

Constraints & Assumptions

  • X has shape (40, 3) and y has shape (40,) , both floats, so the result has shape (3,) .
  • The columns of X are not mean-centered and y does not have mean zero.

What a Strong Answer Covers Guidance

  • Solving least squares on X as given, with no ones column and no centering.
  • Returning a 1-D array of shape (p,) in the column order of X .
  • Explaining why the slopes differ from those of a model with an intercept when the means are nonzero.
  • Choosing a numerically stable solver and knowing what it does when X is close to rank deficient.

Follow-up Questions Guidance

  • When is regression through the origin actually justified?
  • Why can R-squared be misleading for a no-intercept model?
  • Do the residuals of a no-intercept fit still sum to zero, and which property do they keep?
  • How would you check from the data whether the intercept can be dropped?