Fit OLS regression through the origin (no intercept) in NumPy

Quick Overview

Implement ordinary least squares through the origin in NumPy, fitting the given feature matrix with no intercept column and returning one coefficient per feature. It tests the normal equations, numerically stable solvers, and how removing the constant term changes coefficients, residuals and R-squared.

Fit OLS regression through the origin (no intercept) in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `no_intercept_ols(X, y)`, which fits ordinary least squares through the origin (no intercept). Solve `beta = (X^T X)^-1 X^T y` on `X` exactly as given and return the coefficient vector. ```python def no_intercept_ols(X: np.ndarray, y: np.ndarray) -> np.ndarray: ... ``` Requirements: - Do not add a column of ones. No constant term is estimated. - Return an `np.ndarray` of shape `(p,)`, not `(p + 1,)`. - Element `0` is the coefficient on the first column of `X`, not an intercept. ```hint Defaults and solvers Check what your tools do about a constant term by default, and look for a routine that solves the least-squares problem without computing a matrix inverse. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n, p)`, has no column of ones, and has full column rank, so `X^T X` is invertible. - `y` is float64 with shape `(n,)`. ### What a Strong Answer Covers - Fitting on `X` as given and returning a vector of shape `(p,)` in column order. - Solving the problem without an explicit inverse, with the numerical reason for that choice. - Why full column rank makes the solution unique. - How the coefficients and residuals differ from a fit that includes an intercept. - Time and space complexity. ### Follow-up Questions - Why prefer `np.linalg.solve` or `np.linalg.lstsq` over forming the inverse explicitly? - Why do the residuals sum to zero when the model has an intercept, but not here? - Why is R² hard to compare between a model through the origin and one with an intercept?

Overview: Implement ordinary least squares through the origin in NumPy, fitting the given feature matrix with no intercept column and returning one coefficient per feature. It tests the normal equations, numerically stable solvers, and how removing the constant term changes coefficients, residuals and R-squared.

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

Implement no_intercept_ols(X, y), which fits ordinary least squares through the origin (no intercept). Solve beta = (X^T X)^-1 X^T y on X exactly as given and return the coefficient vector.

def no_intercept_ols(X: np.ndarray, y: np.ndarray) -> np.ndarray:
    ...

Requirements:

  • Do not add a column of ones. No constant term is estimated.
  • Return an np.ndarray of shape (p,) , not (p + 1,) .
  • Element 0 is the coefficient on the first column of X , not an intercept.

Constraints & Assumptions

  • X is float64 with shape (n, p) , has no column of ones, and has full column rank, so X^T X is invertible.
  • y is float64 with shape (n,) .

What a Strong Answer Covers Guidance

  • Fitting on X as given and returning a vector of shape (p,) in column order.
  • Solving the problem without an explicit inverse, with the numerical reason for that choice.
  • Why full column rank makes the solution unique.
  • How the coefficients and residuals differ from a fit that includes an intercept.
  • Time and space complexity.

Follow-up Questions Guidance

  • Why prefer np.linalg.solve or np.linalg.lstsq over forming the inverse explicitly?
  • Why do the residuals sum to zero when the model has an intercept, but not here?
  • Why is R² hard to compare between a model through the origin and one with an intercept?