scipy.linalg.lstsq(a, b, cond=None, overwrite_a=False, overwrite_b=False, check_finite=True, lapack_driver='gelsd')[source]

Compute least-squares solution to equation Ax = b.

Compute a vector x such that the 2-norm |b - A x| is minimized.


a : (M, N) array_like

Left hand side matrix (2-D array).

b : (M,) or (M, K) array_like

Right hand side matrix or vector (1-D or 2-D array).

cond : float, optional

Cutoff for ‘small’ singular values; used to determine effective rank of a. Singular values smaller than rcond * largest_singular_value are considered zero.

overwrite_a : bool, optional

Discard data in a (may enhance performance). Default is False.

overwrite_b : bool, optional

Discard data in b (may enhance performance). Default is False.

check_finite : bool, optional

Whether to check that the input matrices contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.

lapack_driver: str, optional

Which LAPACK driver is used to solve the least-squares problem. Options are 'gelsd', 'gelsy', 'gelss'. Default ('gelsd') is a good choice. However, 'gelsy' can be slightly faster on many problems. 'gelss' was used historically. It is generally slow but uses less memory.

New in version 0.17.0.


x : (N,) or (N, K) ndarray

Least-squares solution. Return shape matches shape of b.

residues : () or (1,) or (K,) ndarray

Sums of residues, squared 2-norm for each column in b - a x. If rank of matrix a is < N or > M, or 'gelsy' is used, this is an empty array. If b was 1-D, this is an (1,) shape array, otherwise the shape is (K,).

rank : int

Effective rank of matrix a.

s : (min(M,N),) ndarray or None

Singular values of a. The condition number of a is abs(s[0] / s[-1]). None is returned when 'gelsy' is used.


LinAlgError :

If computation does not converge.

ValueError :

When parameters are wrong.

See also

linear least squares with non-negativity constraint

Previous topic


Next topic