Split OLS Intercept and Slopes

Quick Overview

Write a NumPy function that fits ordinary least squares with an intercept and returns the intercept as a Python float and the slopes as an array of shape (p,). It tests building the design matrix, solving least squares stably, and handling return types and shape pitfalls.

Split OLS Intercept and Slopes

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `intercept_and_slopes(X, y)`, which fits OLS with an intercept (prepend a ones column) and returns the parameters split into the intercept and the slopes. `X` is a `float64` array of shape `(n, p)` without a ones column, and `y` is a `float64` array of shape `(n,)`. Requirements: - Return the 2-tuple `(intercept, slopes)`. - `intercept` is a Python `float`, the coefficient on the ones column. - `slopes` is an `np.ndarray` of shape `(p,)` (not `(p+1,)`) holding the coefficients on the columns of `X` in their original order. Example: ```python X = np.array([[1.0], [2.0], [3.0]]) # shape (3, 1) y = np.array([3.0, 5.0, 7.0]) # y = 1 + 2x intercept, slopes = intercept_and_slopes(X, y) # intercept == 1.0 (python float) # slopes == np.array([2.0]) # shape (1,), not (2,) ``` ```hint Check types and shapes Look closely at what you return: a NumPy scalar is not exactly a Python `float`, and the slopes must not include the intercept, even when `p` is 1. ``` ### Constraints & Assumptions - The design matrix with the ones column has full column rank, so the least-squares solution is unique. - Either `np.linalg.lstsq` or the normal equations is acceptable. ### What a Strong Answer Covers - Building the design matrix with the ones column first so the coefficient positions are known. - Solving for the full coefficient vector with a stable least-squares routine. - Splitting the vector into a Python `float` intercept and a slopes array of shape `(p,)` in the original column order. - Shape pitfalls such as `p = 1` and a `y` passed as a column vector. ### Follow-up Questions - Why prefer `np.linalg.lstsq` over inverting `XᵀX`? - How do the intercept and slopes change if you center every column of `X` before fitting? - What happens to the split if one column of `X` is constant, and how would you detect it?

Overview: Write a NumPy function that fits ordinary least squares with an intercept and returns the intercept as a Python float and the slopes as an array of shape (p,). It tests building the design matrix, solving least squares stably, and handling return types and shape pitfalls.

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

Implement intercept_and_slopes(X, y), which fits OLS with an intercept (prepend a ones column) and returns the parameters split into the intercept and the slopes. X is a float64 array of shape (n, p) without a ones column, and y is a float64 array of shape (n,).

Requirements:

  • Return the 2-tuple (intercept, slopes) .
  • intercept is a Python float , the coefficient on the ones column.
  • slopes is an np.ndarray of shape (p,) (not (p+1,) ) holding the coefficients on the columns of X in their original order.

Example:

X = np.array([[1.0], [2.0], [3.0]])   # shape (3, 1)
y = np.array([3.0, 5.0, 7.0])         # y = 1 + 2x
intercept, slopes = intercept_and_slopes(X, y)
# intercept == 1.0  (python float)
# slopes == np.array([2.0])   # shape (1,), not (2,)

Constraints & Assumptions

  • The design matrix with the ones column has full column rank, so the least-squares solution is unique.
  • Either np.linalg.lstsq or the normal equations is acceptable.

What a Strong Answer Covers Guidance

  • Building the design matrix with the ones column first so the coefficient positions are known.
  • Solving for the full coefficient vector with a stable least-squares routine.
  • Splitting the vector into a Python float intercept and a slopes array of shape (p,) in the original column order.
  • Shape pitfalls such as p = 1 and a y passed as a column vector.

Follow-up Questions Guidance

  • Why prefer np.linalg.lstsq over inverting XᵀX ?
  • How do the intercept and slopes change if you center every column of X before fitting?
  • What happens to the split if one column of X is constant, and how would you detect it?