Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.scipy.linalg.lstsq

View Source On Gitee
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 is None ("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 AXB, 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.]