Fit OLS with an intercept for multiple targets in a single NumPy solve

Quick Overview

Implement ordinary least squares with an intercept for several response columns in NumPy, fitting every target with one least-squares solve. Tests how the design matrix is built, why a matrix right-hand side splits into independent per-target fits, and how to deal with ill-conditioned inputs.

Fit OLS with an intercept for multiple targets in a single NumPy solve

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def multi_target_ols(X: np.ndarray, Y: np.ndarray) -> np.ndarray` that fits OLS with an intercept for `k` targets in a single solve. `X` is float64 `(n, p)` without a ones column, `Y` is float64 `(n, k)`. The function should: - Prepend a ones column to form `Xd` of shape `(n, p+1)`. - Solve once with the 2-D right-hand side (`np.linalg.lstsq` solves each column independently). Do not loop over columns. - Return a single `np.ndarray` of shape `(p+1, k)`: column j is the coefficients for `Y[:, j]`, rows ordered `[intercept, b1, ..., bp]`. Example of the expected shapes: ```python X.shape == (100, 3) # n=100, p=3 Y.shape == (100, 2) # k=2 targets B = multi_target_ols(X, Y) B.shape == (4, 2) # rows: [intercept, b1, b2, b3], cols: target 0, target 1 ``` ```hint Shape of the right-hand side `np.linalg.lstsq` accepts `b` as a matrix as well as a vector. Work out what shape its first return value has when `b` is `(n, k)`, and which row the intercept lands in. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` and `Y` has shape `(n, k)`, both float64, with rows aligned. - `Xd = [ones, X]` is full column rank, so each target has a unique coefficient vector. - All `k` targets are fitted in one solver call, with no Python loop over the columns of `Y`. ### What a Strong Answer Covers - Building the design matrix with the ones column first, so row 0 of the result holds the intercepts. - Passing the whole `(n, k)` matrix as the right-hand side and returning the `(p+1, k)` coefficient matrix in the required layout. - Why a least-squares problem with a matrix right-hand side splits into `k` independent fits that match `k` separate solves. - What one solve saves: the design matrix is factored once instead of once per target. - Numerical choices, such as using a least-squares solver instead of explicitly inverting $X_d^\top X_d$. ### Follow-up Questions - Why does one `lstsq` call handle all `k` targets correctly? - How would you handle a rank-deficient or ill-conditioned `Xd`? - How would you add ridge regularization to all targets at once without penalizing the intercept?

Overview: Implement ordinary least squares with an intercept for several response columns in NumPy, fitting every target with one least-squares solve. Tests how the design matrix is built, why a matrix right-hand side splits into independent per-target fits, and how to deal with ill-conditioned inputs.

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

Implement def multi_target_ols(X: np.ndarray, Y: np.ndarray) -> np.ndarray that fits OLS with an intercept for k targets in a single solve. X is float64 (n, p) without a ones column, Y is float64 (n, k).

The function should:

  • Prepend a ones column to form Xd of shape (n, p+1) .
  • Solve once with the 2-D right-hand side ( np.linalg.lstsq solves each column independently). Do not loop over columns.
  • Return a single np.ndarray of shape (p+1, k) : column j is the coefficients for Y[:, j] , rows ordered [intercept, b1, ..., bp] .

Example of the expected shapes:

X.shape == (100, 3)   # n=100, p=3
Y.shape == (100, 2)   # k=2 targets

B = multi_target_ols(X, Y)
B.shape == (4, 2)     # rows: [intercept, b1, b2, b3], cols: target 0, target 1

Constraints & Assumptions

  • X has shape (n, p) and Y has shape (n, k) , both float64, with rows aligned.
  • Xd = [ones, X] is full column rank, so each target has a unique coefficient vector.
  • All k targets are fitted in one solver call, with no Python loop over the columns of Y .

What a Strong Answer Covers Guidance

  • Building the design matrix with the ones column first, so row 0 of the result holds the intercepts.
  • Passing the whole (n, k) matrix as the right-hand side and returning the (p+1, k) coefficient matrix in the required layout.
  • Why a least-squares problem with a matrix right-hand side splits into k independent fits that match k separate solves.
  • What one solve saves: the design matrix is factored once instead of once per target.
  • Numerical choices, such as using a least-squares solver instead of explicitly inverting Xd⊤XdX_d^\top X_d .

Follow-up Questions Guidance

  • Why does one lstsq call handle all k targets correctly?
  • How would you handle a rank-deficient or ill-conditioned Xd ?
  • How would you add ridge regularization to all targets at once without penalizing the intercept?