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=None)
| 2416 | |
| 2417 | @array_function_dispatch(_lstsq_dispatcher) |
| 2418 | def lstsq(a, b, rcond=None): |
| 2419 | r""" |
| 2420 | Return the least-squares solution to a linear matrix equation. |
| 2421 | |
| 2422 | Computes the vector `x` that approximately solves the equation |
| 2423 | ``a @ x = b``. The equation may be under-, well-, or over-determined |
| 2424 | (i.e., the number of linearly independent rows of `a` can be less than, |
| 2425 | equal to, or greater than its number of linearly independent columns). |
| 2426 | If `a` is square and of full rank, then `x` (but for round-off error) |
| 2427 | is the "exact" solution of the equation. Else, `x` minimizes the |
| 2428 | Euclidean 2-norm :math:`||b - ax||`. If there are multiple minimizing |
| 2429 | solutions, the one with the smallest 2-norm :math:`||x||` is returned. |
| 2430 | |
| 2431 | Parameters |
| 2432 | ---------- |
| 2433 | a : (M, N) array_like |
| 2434 | "Coefficient" matrix. |
| 2435 | b : {(M,), (M, K)} array_like |
| 2436 | Ordinate or "dependent variable" values. If `b` is two-dimensional, |
| 2437 | the least-squares solution is calculated for each of the `K` columns |
| 2438 | of `b`. |
| 2439 | rcond : float, optional |
| 2440 | Cut-off ratio for small singular values of `a`. |
| 2441 | For the purposes of rank determination, singular values are treated |
| 2442 | as zero if they are smaller than `rcond` times the largest singular |
| 2443 | value of `a`. |
| 2444 | The default uses the machine precision times ``max(M, N)``. Passing |
| 2445 | ``-1`` will use machine precision. |
| 2446 | |
| 2447 | .. versionchanged:: 2.0 |
| 2448 | Previously, the default was ``-1``, but a warning was given that |
| 2449 | this would change. |
| 2450 | |
| 2451 | Returns |
| 2452 | ------- |
| 2453 | x : {(N,), (N, K)} ndarray |
| 2454 | Least-squares solution. If `b` is two-dimensional, |
| 2455 | the solutions are in the `K` columns of `x`. |
| 2456 | residuals : {(1,), (K,), (0,)} ndarray |
| 2457 | Sums of squared residuals: Squared Euclidean 2-norm for each column in |
| 2458 | ``b - a @ x``. |
| 2459 | If the rank of `a` is < N or M <= N, this is an empty array. |
| 2460 | If `b` is 1-dimensional, this is a (1,) shape array. |
| 2461 | Otherwise the shape is (K,). |
| 2462 | rank : int |
| 2463 | Rank of matrix `a`. |
| 2464 | s : (min(M, N),) ndarray |
| 2465 | Singular values of `a`. |
| 2466 | |
| 2467 | Raises |
| 2468 | ------ |
| 2469 | LinAlgError |
| 2470 | If computation does not converge. |
| 2471 | |
| 2472 | See Also |
| 2473 | -------- |
| 2474 | scipy.linalg.lstsq : Similar function in SciPy. |
| 2475 |
no test coverage detected
searching dependent graphs…