lstsq
tries to solve Ax=b
minimizing |b - Ax|
. Both scipy and numpy provide a linalg.lstsq
function with a very similar inter
As of Numpy 1.13 and Scipy 0.19, both scipy.linalg.lstsq() and numpy.linalg.lstsq() call by default the same LAPACK code DSGELD (see LAPACK documentation).
However, a current important difference between the two function is in the adopted default RCOND LAPACK parameter (called rcond
by Numpy and cond
by Scipy), which defines the threshold for singular values.
Scipy uses a good and robust default threshold RCOND=eps*max(A.shape)*S[0]
, where S[0]
is the largest singular value of A
, while Numpy uses a default threshold RCOND=-1
, which corresponds to setting in LAPACK the threshold equal to the machine precision, regardless of the values of A
.
Numpy's default approach is basically useless in realistic applications and will generally result in a very degenerate solution when A
is nearly rank deficient, wasting the accuracy of the singular value decomposition SVD used by DSGELD. This implies that in Numpy the optional parameter rcond
should be always used.
I reported the incorrect default of rcond
(see above Section) in numpy.linalg.lstsq() and the function now raises a FutureWarning
in Numpy 1.14 (see Future Changes).
The future behaviour will be identical both in scipy.linalg.lstsq() and in numpy.linalg.lstsq(). In other words, Scipy and Numpy will not only use the same LAPACK code, but also use the same defaults.
To start using the proper (i.e. future) default in Numpy 1.14, one should call numpy.linalg.lstsq() with an explicit rcond=None
.