MCPcopy Create free account
hub / github.com/numpy/numpy / lstsq

Function lstsq

numpy/linalg/linalg.py:2192–2348  ·  view source on GitHub ↗

r""" Return the least-squares solution to a linear matrix equation. Computes the vector `x` that approximately solves the equation ``a @ x = b``. The equation may be under-, well-, or over-determined (i.e., the number of linearly independent rows of `a` can be less than, equal t

(a, b, rcond="warn")

Source from the content-addressed store, hash-verified

2190
2191@array_function_dispatch(_lstsq_dispatcher)
2192def lstsq(a, b, rcond="warn"):
2193 r"""
2194 Return the least-squares solution to a linear matrix equation.
2195
2196 Computes the vector `x` that approximately solves the equation
2197 ``a @ x = b``. The equation may be under-, well-, or over-determined
2198 (i.e., the number of linearly independent rows of `a` can be less than,
2199 equal to, or greater than its number of linearly independent columns).
2200 If `a` is square and of full rank, then `x` (but for round-off error)
2201 is the "exact" solution of the equation. Else, `x` minimizes the
2202 Euclidean 2-norm :math:`||b - ax||`. If there are multiple minimizing
2203 solutions, the one with the smallest 2-norm :math:`||x||` is returned.
2204
2205 Parameters
2206 ----------
2207 a : (M, N) array_like
2208 "Coefficient" matrix.
2209 b : {(M,), (M, K)} array_like
2210 Ordinate or "dependent variable" values. If `b` is two-dimensional,
2211 the least-squares solution is calculated for each of the `K` columns
2212 of `b`.
2213 rcond : float, optional
2214 Cut-off ratio for small singular values of `a`.
2215 For the purposes of rank determination, singular values are treated
2216 as zero if they are smaller than `rcond` times the largest singular
2217 value of `a`.
2218
2219 .. versionchanged:: 1.14.0
2220 If not set, a FutureWarning is given. The previous default
2221 of ``-1`` will use the machine precision as `rcond` parameter,
2222 the new default will use the machine precision times `max(M, N)`.
2223 To silence the warning and use the new default, use ``rcond=None``,
2224 to keep using the old behavior, use ``rcond=-1``.
2225
2226 Returns
2227 -------
2228 x : {(N,), (N, K)} ndarray
2229 Least-squares solution. If `b` is two-dimensional,
2230 the solutions are in the `K` columns of `x`.
2231 residuals : {(1,), (K,), (0,)} ndarray
2232 Sums of squared residuals: Squared Euclidean 2-norm for each column in
2233 ``b - a @ x``.
2234 If the rank of `a` is < N or M <= N, this is an empty array.
2235 If `b` is 1-dimensional, this is a (1,) shape array.
2236 Otherwise the shape is (K,).
2237 rank : int
2238 Rank of matrix `a`.
2239 s : (min(M, N),) ndarray
2240 Singular values of `a`.
2241
2242 Raises
2243 ------
2244 LinAlgError
2245 If computation does not converge.
2246
2247 See Also
2248 --------
2249 scipy.linalg.lstsq : Similar function in SciPy.

Callers 1

polyfitFunction · 0.90

Calls 15

finfoClass · 0.90
arrayFunction · 0.90
_makearrayFunction · 0.85
_assert_2dFunction · 0.85
LinAlgErrorClass · 0.85
_commonTypeFunction · 0.85
_realTypeFunction · 0.85
isComplexTypeFunction · 0.85
get_linalg_error_extobjFunction · 0.85
wrapFunction · 0.85
warnMethod · 0.80
astypeMethod · 0.80

Tested by

no test coverage detected