Fit OLS with numpy.linalg.lstsq and unpack all four return values

Quick Overview

Fit ordinary least squares with an intercept using numpy.linalg.lstsq and return the coefficients, residual sum of squares, rank and singular values with the right types. It tests knowing what lstsq returns, when its residuals array is empty, and how singular values relate to conditioning.

Fit OLS with numpy.linalg.lstsq and unpack all four return values

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `lstsq_report(X, y)` for a float64 `X` of shape `(n, p)` with no ones column and a float64 `y` of shape `(n,)`. Form `Xd = [ones, X]`, call `np.linalg.lstsq(Xd, y, rcond=None)`, and return `(beta, rss, rank, s)`: - `beta`: ndarray of shape `(p+1,)`, intercept first. - `rss`: Python `float`, the scalar from the shape-`(1,)` residuals array that `lstsq` returns (`float(residuals[0])`). - `rank`: Python `int`, the rank of `Xd` (here `p+1`). - `s`: ndarray of shape `(p+1,)`, the singular values of `Xd` in descending order. The returned values should satisfy: ```python beta, rss, rank, s = lstsq_report(X, y) beta.shape # (p+1,) type(rss) # <class 'float'> type(rank) # <class 'int'> s.shape # (p+1,) ``` ```hint Types Check the type and shape of each of the four values `lstsq` returns. Not all of them are the plain Python types the task asks for. ``` ### Constraints & Assumptions - `n > p+1` and `Xd` has full column rank, so the residuals array is non-empty. - `X` and `y` are float64. ### What a Strong Answer Covers - Prepends a ones column so the intercept is the first coefficient. - Unpacks the four `lstsq` outputs in order: solution, residuals, rank, singular values. - Turns the shape-`(1,)` residuals array into a scalar `float` and the rank into a Python `int`. - Knows when the residuals array would be empty and why the constraints rule that out here. ### Follow-up Questions - When is the residuals array from `lstsq` empty, and how would you compute `rss` then? - What does `rcond` control, and why pass `rcond=None`? - How do the singular values in `s` relate to conditioning?

Overview: Fit ordinary least squares with an intercept using numpy.linalg.lstsq and return the coefficients, residual sum of squares, rank and singular values with the right types. It tests knowing what lstsq returns, when its residuals array is empty, and how singular values relate to conditioning.

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

Implement lstsq_report(X, y) for a float64 X of shape (n, p) with no ones column and a float64 y of shape (n,). Form Xd = [ones, X], call np.linalg.lstsq(Xd, y, rcond=None), and return (beta, rss, rank, s):

  • beta : ndarray of shape (p+1,) , intercept first.
  • rss : Python float , the scalar from the shape- (1,) residuals array that lstsq returns ( float(residuals[0]) ).
  • rank : Python int , the rank of Xd (here p+1 ).
  • s : ndarray of shape (p+1,) , the singular values of Xd in descending order.

The returned values should satisfy:

beta, rss, rank, s = lstsq_report(X, y)
beta.shape   # (p+1,)
type(rss)    # <class 'float'>
type(rank)   # <class 'int'>
s.shape      # (p+1,)

Constraints & Assumptions

  • n > p+1 and Xd has full column rank, so the residuals array is non-empty.
  • X and y are float64.

What a Strong Answer Covers Guidance

  • Prepends a ones column so the intercept is the first coefficient.
  • Unpacks the four lstsq outputs in order: solution, residuals, rank, singular values.
  • Turns the shape- (1,) residuals array into a scalar float and the rank into a Python int .
  • Knows when the residuals array would be empty and why the constraints rule that out here.

Follow-up Questions Guidance

  • When is the residuals array from lstsq empty, and how would you compute rss then?
  • What does rcond control, and why pass rcond=None ?
  • How do the singular values in s relate to conditioning?