bundles / scipy latest / scipy / linalg / _basic / lstsq
function
scipy.linalg._basic:lstsq
source: /scipy/linalg/_basic.py :1606
Signature
def lstsq ( a , b , cond = None , overwrite_a = False , overwrite_b = False , check_finite = True , lapack_driver = None ) Summary
Compute least-squares solution to the equation a @ x = b.
Extended Summary
Compute a vector x such that the 2-norm |b - A x| is minimized.
The documentation is written assuming array arguments are of specified "core" shapes. However, array argument(s) of this function may have additional "batch" dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see linalg_batch for details. Note that calls with zero-size batches are unsupported and will raise a ValueError.
Parameters
a: (M, N) array_likeLeft-hand side array
b: (M,) or (M, K) array_likeRight hand side array
cond: float, optionalCutoff for 'small' singular values; used to determine effective rank of a. Singular values smaller than
cond * largest_singular_valueare considered zero.overwrite_a: bool, optionalDiscard data in
a(may enhance performance). Default is False.overwrite_b: bool, optionalDiscard data in
b(may enhance performance). Default is False.check_finite: bool, optionalWhether 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, optionalWhich 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.
Returns
x: (N,) or (N, K) ndarrayLeast-squares solution.
residues: (K,) ndarray or floatSquare of the 2-norm for each column in
b - a x, ifM > Nandrank(A) == n(returns a scalar ifbis 1-D). Otherwise a (0,)-shaped array is returned.rank: intEffective rank of
a.s: (min(M, N),) ndarray or NoneSingular values of
a. The condition number ofaiss[0] / s[-1].
Raises
: LinAlgErrorIf computation does not converge.
: ValueErrorWhen parameters are not compatible.
Notes
When 'gelsy' is used as a driver, residues is set to a (0,)-shaped array and s is always None.
Examples
import numpy as np from scipy.linalg import lstsq import matplotlib.pyplot as plt✓
x = np.array([1, 2.5, 3.5, 4, 5, 7, 8.5]) y = np.array([0.3, 1.1, 1.5, 2.0, 3.2, 6.6, 8.6])✓
M = x[:, np.newaxis]**[0, 2]
✓M
✗p, res, rnk, s = lstsq(M, y)
✓p
✗plt.plot(x, y, 'o', label='data')
✗xx = np.linspace(0, 9, 101) yy = p[0] + p[1]*xx**2✓
plt.plot(xx, yy, label='least squares fit, $y = a + bx^2$') plt.xlabel('x') plt.ylabel('y') plt.legend(framealpha=1, shadow=True)✗
plt.grid(alpha=0.25) plt.show()✓

See also
- scipy.optimize.nnls
linear least squares with non-negativity constraint
Aliases
-
scipy.linalg.lstsq