Fit a least-squares line with np.linalg.lstsq and return the slope and intercept

Quick Overview

Fit a straight line to paired data by building a design matrix and solving the least-squares system in NumPy, returning the slope and intercept. It tests the matrix form of linear regression, how to read the solver's output, inputs with no unique line, and numerical stability.

Fit a least-squares line with np.linalg.lstsq and return the slope and intercept

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given equal-length 1-D arrays `x` and `y`, implement `fit_line(x, y)` returning `(slope, intercept)` of the least-squares line `y = slope*x + intercept`. Requirements: - Build `A = np.vstack([x, ones]).T` (columns: `x`, then ones). - Solve with `np.linalg.lstsq(A, y, rcond=None)`. The solution is its first element, ordered `[slope, intercept]`. - Return `(slope, intercept)` as Python floats or NumPy scalars. - Do not call `np.polyfit` or any higher-level fitting helper. ```python x = np.array([0, 1, 2, 3]) y = np.array([1, 3, 5, 7]) fit_line(x, y) # (2.0, 1.0) up to floating-point rounding; the points lie on y = 2*x + 1 ``` ```hint Rows of the design matrix Each row of `A` describes one point: the number the slope is multiplied by and the number the intercept is multiplied by. Then check what each element of the tuple returned by `lstsq` holds. ``` ### Constraints & Assumptions - `x` and `y` have the same length and at least two distinct `x` values. ### What a Strong Answer Covers - Writes the fit as a linear system with a design matrix whose columns are `x` and a constant, and states what `lstsq` minimizes. - Takes the coefficient vector out of the `lstsq` return value in the right order. - Explains when the problem has no unique line and how to detect it. - Discusses the numerical stability of the solver. ### Follow-up Questions - How does `lstsq` solve this compared with the normal equations $(A^\top A)^{-1} A^\top y$? - How would you extend this to fit a degree-`n` polynomial without `np.polyfit`? - How could you obtain the residual sum of squares from the same call?

Overview: Fit a straight line to paired data by building a design matrix and solving the least-squares system in NumPy, returning the slope and intercept. It tests the matrix form of linear regression, how to read the solver's output, inputs with no unique line, and numerical stability.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given equal-length 1-D arrays x and y, implement fit_line(x, y) returning (slope, intercept) of the least-squares line y = slope*x + intercept.

Requirements:

  • Build A = np.vstack([x, ones]).T (columns: x , then ones).
  • Solve with np.linalg.lstsq(A, y, rcond=None) . The solution is its first element, ordered [slope, intercept] .
  • Return (slope, intercept) as Python floats or NumPy scalars.
  • Do not call np.polyfit or any higher-level fitting helper.
x = np.array([0, 1, 2, 3])
y = np.array([1, 3, 5, 7])
fit_line(x, y)
# (2.0, 1.0) up to floating-point rounding; the points lie on y = 2*x + 1

Constraints & Assumptions

  • x and y have the same length and at least two distinct x values.

What a Strong Answer Covers Guidance

  • Writes the fit as a linear system with a design matrix whose columns are x and a constant, and states what lstsq minimizes.
  • Takes the coefficient vector out of the lstsq return value in the right order.
  • Explains when the problem has no unique line and how to detect it.
  • Discusses the numerical stability of the solver.

Follow-up Questions Guidance

  • How does lstsq solve this compared with the normal equations (A⊤A)−1A⊤y(A^\top A)^{-1} A^\top y ?
  • How would you extend this to fit a degree- n polynomial without np.polyfit ?
  • How could you obtain the residual sum of squares from the same call?