lstsq#
- rlinalg.lstsq(a: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], b: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], tol: float = 1e-07, overwrite_a: bool = False, check_finite: bool = True) Tuple[ndarray, ndarray, int]#
Compute least-squares solution to equation Ax = b.
Compute a vector x such that the 2-norm
|b - A x|is minimized.- Parameters:
a ((M, N) array_like) – Left-hand side array
b ((M,) or (M, K) array_like) – Right hand side array
tol (float) – The absolute tolerance to use for QR decomposition.
overwrite_a (bool, optional) – Discard data in
a(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.
- Returns:
x ((N,) or (N, K) ndarray) – Least-squares solution. For underdetermined systems,
x[i]will be zero for alli >= rank(A).residues ((K,) ndarray or float) – Square 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 (int) – Effective rank of
a.
- Raises:
ValueError – When parameters are not compatible.
Notes
This function uses a QR decomposition rather than a SVD decomposition in the case of
numpy.linalg.lstsqorscipy.linalg.lstsq. This is the same behaviour aslm.fit.See also
numpy.linalg.lstsqThe NumPy implementation using LAPACK.
scipy.linalg.lstsqThe NumPy implementation using LAPACK.
- lm.fit
Documentation of the equivalent R function
lm.fit.
Examples
>>> import numpy >>> from rlinalg import lstsq
Suppose we have the following data:
>>> x = numpy.array([1, 2.5, 3.5, 4, 5, 7, 8.5]) >>> y = numpy.array([0.3, 1.1, 1.5, 2.0, 3.2, 6.6, 8.6])
We want to fit a quadratic polynomial of the form
y = a + b*x**2to this data. We first form the “design matrix” M, with a constant column of 1s and a column containingx**2:>>> M = x[:, numpy.newaxis]**[0, 2] >>> M array([[ 1. , 1. ], [ 1. , 6.25], [ 1. , 12.25], [ 1. , 16. ], [ 1. , 25. ], [ 1. , 49. ], [ 1. , 72.25]])
We want to find the least-squares solution to
M.dot(p) = y, wherepis a vector with length 2 that holds the parametersaandb.>>> p, res, rank = lstsq(M, y) >>> p array([0.20925829, 0.12013861])
References
Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.