Linear regression
Fit coefficients in a linear model when observations outnumber unknowns.
SciPy · Function
Computes a least-squares solution to a @ x = b by minimizing the residual 2-norm. It also returns residuals, effective rank, and singular values.
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.
scipy.linalg.lstsq(a, b, cond=None, overwrite_a=False, overwrite_b=False, check_finite=True, lapack_driver=None)| Name | Type | Description |
|---|---|---|
a | array_like | Left-hand-side matrix, with shape (M, N). |
b | array_like | Right-hand-side vector or matrix, with shape (M,) or (M, K). |
cond | float, optional | Cutoff used to determine the effective rank from singular values. |
overwrite_a | bool, optional | Whether to overwrite a; defaults to False. |
overwrite_b | bool, optional | Whether to overwrite b; defaults to False. |
check_finite | bool, optional | Whether to verify that inputs contain only finite values; defaults to True. |
lapack_driver | str, optional | LAPACK driver: 'gelsd', 'gelsy', or 'gelss'. |
Returns x (least-squares solution), residues (squared residual norms when applicable), rank (effective rank of a), and s (singular values of a).
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.
Fit coefficients in a linear model when observations outnumber unknowns.
Solve systems with multiple right-hand-side columns in one call.
Diagnose near-singular or rank-deficient matrices using rank and singular values.
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.
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.
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.pinvscipy.linalg.solvescipy.linalg.lstsq