mindspore.scipy.linalg.lstsq
- mindspore.scipy.linalg.lstsq(A, B, rcond=None, driver=None)[source]
Computes a solution to the least squares problem of a system of linear equations \(AX = B\).
Note
lstsq is currently only used in mindscience scientific computing scenarios and dose not support other usage scenarios.
lstsq is not supported on Windows platform yet.
- Parameters
A (Tensor) – LHS input tensor of shape \((*, M, N)\), where \(*\) is zero or more batch dimensions.
B (Tensor) – RHS input tensor of shape \((*, M, K)\), where \(*\) is zero or more batch dimensions.
rcond (number.Number, optional) – Not implemented now, Default is
None
.driver (string, optional) – Which LAPACK driver is used to solve the least-squares problem. Options are
"gels"
,"gelsy"
,"gelss"
,"gelsd"
. Default isNone
("gelsy"
). if A is well-conditioned,"gels"
is a good choice for full-rank matrix, and"gelsy"
for a general matrix. if A is not well-conditioned,"gelsd"
works good,"gelss"
was used historically. It is generally slow but uses less memory.
- Returns
solution (Tensor), Least-squares solution. It has shape \((*, N, K)\), where \(*\) is same as broadcast batch dimensions.
residual (Tensor), Square of the 2-norm for each column in \(AX - B\), It has shape \((*, K)\), where \(*\) is same as broadcast batch dimensions. It is computed when driver is one of (
"gels"
,"gelss"
,"gelsd"
) and \(M > N\) , otherwise, it is an empty tensor.rank (Tensor), Effective rank of A. It has shape \((*)\), where \(*\) is same as batch dimensions of A. It is computed when driver is one of (
"gelsy"
,"gelss"
,"gelsd"
), otherwise it is an empty tensor.singular_value (Tensor), Singular values of A. It has shape \((*, min(M, N))\), where \(*\) is same as batch dimensions of A. It is computed when driver is one of (
"gelss"
,"gelsd"
), otherwise it is an empty tensor.
- Raises
TypeError – If dtype of A and B are not the same.
ValueError – If A is less than 2 dimension.
ValueError – If the shape of A and B are not matched.
ValueError – If driver is not in set {
None
,"gels"
,"gelsy"
,"gelss"
,"gelsd"
}.
- Supported Platforms:
Ascend
CPU
Examples
>>> import numpy as onp >>> import mindspore >>> from mindspore import Tensor >>> from mindspore.scipy.linalg import lstsq >>> a = Tensor(onp.array([[3, 0, 0, 0], [2, 1, 0, 0], [1, 0, 1, 0], [1, 1, 1, 1]], onp.float32)) >>> b = Tensor(onp.array([3, 1, 3, 4], onp.float32)) >>> solution, residual, rank, singular_value = lstsq(a, b) >>> print(solution) [ 1. -1. 2. 2.] >>> print(a @ solution) # Check the result [3. 1. 3. 4.]