OLS Coefficients via np.linalg.lstsq

Quick Overview

Implement a NumPy function that fits ordinary least squares with an intercept by calling np.linalg.lstsq on a design matrix with a prepended ones column. It tests design matrix construction, reading the lstsq return tuple, and the numerical stability trade-off against the normal equations.

OLS Coefficients via np.linalg.lstsq

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `lstsq_coefficients`, which fits ordinary least squares (OLS) with an intercept using `np.linalg.lstsq` and returns the coefficient vector. ```python def lstsq_coefficients(X: np.ndarray, y: np.ndarray) -> np.ndarray: ... ``` `X` is a `float64` array of shape `(n, p)` without a ones column, and `y` is a `float64` array of shape `(n,)`. Requirements: - Prepend a ones column to `X` (as the first column, so index `0` is the intercept). - Call `np.linalg.lstsq` with an explicit `rcond=None`. Do not form `(XᵀX)⁻¹`. - Take index `[0]` of the returned 4-tuple `(x, residuals, rank, s)`. Returning the raw tuple is incorrect. - Return an `np.ndarray` of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. Example: ```python X = np.array([[1.0], [2.0], [3.0]]) # shape (3, 1), no ones column y = np.array([3.0, 5.0, 7.0]) beta = lstsq_coefficients(X, y) # beta -> array([1.0, 2.0]) # [intercept, slope] ``` ```hint Watch the shapes The ones column has to be two-dimensional with `n` rows to stack next to `X`, and it has to go on the left so the intercept comes out first. ``` ### Constraints & Assumptions - `n > p+1`, so the system is overdetermined. - `X` has full column rank. Assume the design matrix with the ones column prepended does too, so the least-squares solution is unique. ### What a Strong Answer Covers - Building the design matrix with the ones column in position 0 and the correct shape. - Calling `np.linalg.lstsq` with `rcond=None` and extracting the coefficient vector from the returned tuple. - Returning a one-dimensional array of length `p+1` in the required order. - Why solving the least-squares problem directly is numerically safer than inverting `XᵀX`. - Edge cases such as a rank-deficient design matrix or a 1-D `X`. ### Follow-up Questions - Why is `lstsq` preferable to inverting `XᵀX`? - What does `rcond` control, and why pass `rcond=None`? - How would you also recover the residual sum of squares?

Overview: Implement a NumPy function that fits ordinary least squares with an intercept by calling np.linalg.lstsq on a design matrix with a prepended ones column. It tests design matrix construction, reading the lstsq return tuple, and the numerical stability trade-off against the normal equations.

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

Implement lstsq_coefficients, which fits ordinary least squares (OLS) with an intercept using np.linalg.lstsq and returns the coefficient vector.

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

X is a float64 array of shape (n, p) without a ones column, and y is a float64 array of shape (n,).

Requirements:

  • Prepend a ones column to X (as the first column, so index 0 is the intercept).
  • Call np.linalg.lstsq with an explicit rcond=None . Do not form (XᵀX)⁻¹ .
  • Take index [0] of the returned 4-tuple (x, residuals, rank, s) . Returning the raw tuple is incorrect.
  • Return an np.ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] .

Example:

X = np.array([[1.0], [2.0], [3.0]])   # shape (3, 1), no ones column
y = np.array([3.0, 5.0, 7.0])

beta = lstsq_coefficients(X, y)
# beta -> array([1.0, 2.0])   # [intercept, slope]

Constraints & Assumptions

  • n > p+1 , so the system is overdetermined.
  • X has full column rank. Assume the design matrix with the ones column prepended does too, so the least-squares solution is unique.

What a Strong Answer Covers Guidance

  • Building the design matrix with the ones column in position 0 and the correct shape.
  • Calling np.linalg.lstsq with rcond=None and extracting the coefficient vector from the returned tuple.
  • Returning a one-dimensional array of length p+1 in the required order.
  • Why solving the least-squares problem directly is numerically safer than inverting XᵀX .
  • Edge cases such as a rank-deficient design matrix or a 1-D X .

Follow-up Questions Guidance

  • Why is lstsq preferable to inverting XᵀX ?
  • What does rcond control, and why pass rcond=None ?
  • How would you also recover the residual sum of squares?