numpy.linalg.lstsq

numpy.linalg.lstsq(a, b, rcond=-1)[source]

Return the least-squares solution to a linear matrix equation.

Solves the equation a x = b by computing a vector x that minimizes the Euclidean 2-norm || b - a x ||^2. The equation may be under-, well-, or over- determined (i.e., the number of linearly independent rows of a can be less than, equal to, or greater than its number of linearly independent columns). If a is square and of full rank, then x (but for round-off error) is the “exact” solution of the equation.

Parameters :

a : (M, N) array_like

“Coefficient” matrix.

b : {(M,), (M, K)} array_like

Ordinate or “dependent variable” values. If b is two-dimensional, the least-squares solution is calculated for each of the K columns of b.

rcond : float, optional

Cut-off ratio for small singular values of a. Singular values are set to zero if they are smaller than rcond times the largest singular value of a.

Returns :

x : {(M,), (M, K)} ndarray

Least-squares solution. The shape of x depends on the shape of b.

residuals : {(), (1,), (K,)} ndarray

Sums of residuals; squared Euclidean 2-norm for each column in b - a*x. If the rank of a is < N or > M, this is an empty array. If b is 1-dimensional, this is a (1,) shape array. Otherwise the shape is (K,).

rank : int

Rank of matrix a.

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

Singular values of a.

Raises :

LinAlgError :

If computation does not converge.

Notes

If b is a matrix, then all array results are returned as matrices.

Examples

Previous topic

numpy.linalg.tensorsolve

Next topic

numpy.linalg.inv

This Page