Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

linalg.lstsq

Computes a least-squares solution to a @ x = b by minimizing the residual 2-norm. It also returns residuals, effective rank, and singular values.

How it works

lstsq finds the vector x that minimizes the 2-norm of the difference between b and a @ x. It can also report residuals, effective matrix rank, and singular values, while optional controls determine rank sensitivity, input validation, memory overwriting, and the LAPACK solver.

Syntax

scipy.linalg.lstsq(a, b, cond=None, overwrite_a=False, overwrite_b=False, check_finite=True, lapack_driver=None)

Parameters

NameTypeDescription
aarray_likeLeft-hand-side matrix, with shape (M, N).
barray_likeRight-hand-side vector or matrix, with shape (M,) or (M, K).
condfloat, optionalCutoff used to determine the effective rank from singular values.
overwrite_abool, optionalWhether to overwrite a; defaults to False.
overwrite_bbool, optionalWhether to overwrite b; defaults to False.
check_finitebool, optionalWhether to verify that inputs contain only finite values; defaults to True.
lapack_driverstr, optionalLAPACK driver: 'gelsd', 'gelsy', or 'gelss'.

Returns

Returns x (least-squares solution), residues (squared residual norms when applicable), rank (effective rank of a), and s (singular values of a).

Key points

  • Use lstsq when an exact solution is unavailable or when fitting a vector to a linear system.
  • The optimization objective is the Euclidean norm of the residual between b and the predicted product a @ x.
  • cond controls effective-rank detection by treating sufficiently small singular values as zero.
  • Choose among the gelsd, gelsy, and gelss LAPACK drivers with lapack_driver.
  • The solution shape follows b: a vector right-hand side yields a vector solution, while multiple right-hand sides yield corresponding columns.
  • The default LAPACK driver is gelsd and is documented as a sound general-purpose option.

Example: Solve an overdetermined system

import numpy as np
from scipy.linalg import lstsq

A = np.array([[1.0, 1.0], [1.0, 2.0], [1.0, 3.0]])
b = np.array([1.0, 2.0, 2.5])
x, residues, rank, singular_values = lstsq(A, b)
print(x)
print(rank)

Solves an overdetermined system and reports the fitted coefficients, residual information, effective rank, and singular values.

When to use it

Linear regression

Fit coefficients in a linear model when observations outnumber unknowns.

Multiple outputs

Solve systems with multiple right-hand-side columns in one call.

Rank analysis

Diagnose near-singular or rank-deficient matrices using rank and singular values.

Interview questions

QWhat problem does scipy.linalg.lstsq solve?

It computes a least-squares solution x for a @ x = b by minimizing the 2-norm of b - a @ x. The result is useful for overdetermined systems where an exact solution may not exist.

Follow-up: How is rank handled? The cond cutoff determines which singular values count as zero, so it affects the effective rank and solution behavior for ill-conditioned inputs.

QWhat does lstsq return?

The function returns x, residues, rank, and s. x is the solution, residues contains squared residual norms when applicable, rank is the effective rank, and s contains the singular values of a.

Follow-up: When can residues be empty? They are empty when the matrix is rank deficient or when its row/column dimensions do not satisfy the documented conditions.

QHow can the numerical solver and validation behavior be configured?

lapack_driver selects the LAPACK implementation: gelsd, gelsy, or gelss. gelsd is the default and is generally a good choice, while gelsy may be faster for many problems.

Follow-up: What trade-off does check_finite expose? Turning it off can improve speed, but invalid numeric inputs can lead to crashes or non-termination.

  • scipy.linalg.pinv
  • scipy.linalg.solve
  • scipy.linalg.lstsq

Source: Lstsq. Checked 2026-10-15.