8000 numpy.linalg.lstsq gives empty residual · Issue #4438 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

numpy.linalg.lstsq gives empty residual #4438

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

Closed
qnzhou opened this issue Mar 5, 2014 · 7 comments
Closed

numpy.linalg.lstsq gives empty residual #4438

qnzhou opened this issue Mar 5, 2014 · 7 comments

Comments

@qnzhou
Copy link
qnzhou commented Mar 5, 2014

python version: 2.7.6
numpy version: 1.8.0

Here is a short example:

import numpy as np
from numpy.linalg import lstsq

a = np.array([[1, 0], [0, 1]]);
b = np.array([1,2]);

print(lstsq(a,b));

Since a is 2x2 matrix of rank 2, I expect the residual to be 0 instead of empty list according to the doc (http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html). But here is what I get:

(array([ 1.,  2.]), array([], dtype=float64), 2, array([ 1.,  1.]))
@seberg
Copy link
Member
seberg commented Mar 5, 2014

Isn't this an issue of the documentation and not the function?

@cimarronm
Copy link
Contributor

It is just an issue with documentation. It will only return a non-empty residuals when the equations are over-determined. I put in a PR #4457 to fix the documentation

@qnzhou
Copy link
Author
qnzhou commented Mar 7, 2014

Maybe there is a good reason for returning an empty list as residual error for well determined equations, but won't returning [0] be a more intuitive behavior in such cases? It matches the mathematical definition of residual error.

@cimarronm
Copy link
Contributor

@qnzhou The reason is that for under-determined or well-determined equation sets there really is no meaning of residual error (there is no residual to compute). Only with over-determined equations could there exist a residual error.

@seberg
Copy link
Member
seberg commented Mar 7, 2014

Doc updated by gh-4457.

@seberg seberg closed this as completed Mar 7, 2014
@opensdh
Copy link
opensdh commented Mar 18, 2020

It may be far too late to change anything here (especially since it has now been documented as implemented for so long), but it's not helpful to have the shape of a return value depend on the values of the input. If I provide a calculated a that might be rank-deficient (and might or might not be judged as such for numerical reasons), why should I have to examine the returned rank to find out whether I got residuals or not? Making them 0 (as was once documented) would make more sense there; anyone who "statically" knows that their system is never overdetermined would just ignore it anyway.

@eric-wieser
Copy link
Member
eric-wieser commented Mar 18, 2020

Agreed, this behavior is ridiculous, and makes #8720 impossible (#8720 (comment))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0