question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.linalg.lstsq gives empty residual

See original GitHub issue

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.]))

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Mar 18, 2020

Agreed, this behavior is ridiculous, and makes #8720 impossible (https://github.com/numpy/numpy/issues/8720#issuecomment-383198263)

0reactions
opensdhcommented, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found