-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOC: add missing details to linalg.lstsq docstring #18062
Conversation
numpy/linalg/linalg.py
Outdated
``a @ x = b``, where `a` is an `m`-by-`n` matrix and `b` is a `m` element | ||
vector. When the system is overdetermined (``m >= n`` and ``rank(a) = n``), | ||
a solution `x` that minimizes the Euclidean 2-norm ``||b-ax||`` is found. | ||
Else, when the system is underdetermined (``m < n`` and ``rank(A) = m``) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "If there are multiple minimizing solutions, the one with the smallest norm is returned". Note that the norm of the solution depends on column scaling (units) and choice of variables, so the smallest norm solution is seldom meaningful when the solution is not unique.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds better - I've updated my commit to reflect the suggestion, thanks!
The reason I stumbled on this smallest norm matter was that I was specifically looking for that solution in a rank-deficient problem. I was intrigued I got exactly what I wanted for free, so I did some digging and found why :)
It turns out that lstsq also minimizes the 2-norm of x when a is rank-deficient. I found that by searching the documentation for the LAPACK library, which is the current implementation of lstsq (as of Dec 2020). Ref: https://www.netlib.org/lapack/lug/node27.html
15f52e3
to
c755c91
Compare
numpy/linalg/linalg.py
Outdated
``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 to, or greater than its number of linearly independent columns). | ||
If `a` is square and of full rank, then `x` (but for round-off error) | ||
is the "exact" solution of the equation. Else, `x` minimizes the | ||
Euclidean 2-norm :math:`|| b - a x ||`. | ||
Euclidean 2-norm :math:`||b-ax||`. If there are multiple minimizing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep spaces around -
for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed that.
[ci skip]
Thanks @krnilo . |
Worth pointing out that this is the behavior of the pseudoinverse? |
It turns out that
linalg.lstsq
also minimizes the 2-norm ofx
whena
is rank-deficient. I found that by searching the documentation for the LAPACK library, which is the current implementation oflstsq
.This PR rewrites the docstring for
linalg.lstsq
such that it contains details for all cases.