numpy.linalg.lstsq gives empty residual
See original GitHub issuepython 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.]))
Issue Analytics
- State:
- Created 10 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
No Residuals With Numpy's Least Squares - Stack Overflow
According to the numpy documentation: ... residuals will be empty when the equations are under-determined or well-determined but return values ...
Read more >numpy.linalg.lstsq — NumPy v1.24 Manual
Sums of squared residuals: Squared Euclidean 2-norm for each column in b - a @ x . If the rank of a is...
Read more >numpy.linalg.lstsq() - JAX documentation - Read the Docs
In np.linalg.lstsq the returned residuals are empty for low-rank or over-determined solutions. Here, the residuals are returned in all cases, ...
Read more >A problem with numpy.linalg.lstsq : r/learnprogramming - Reddit
Everything is fine until I try to print the residuals (which should be the second return of the algorithm) and it prints an...
Read more >torch.linalg.lstsq — PyTorch 1.13 documentation
Default: None . Returns: A named tuple (solution, residuals, rank, singular_values) . Examples:.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Agreed, this behavior is ridiculous, and makes #8720 impossible (https://github.com/numpy/numpy/issues/8720#issuecomment-383198263)
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.