lstsq
tries to solve Ax=b
minimizing |b - Ax|
. Both scipy and numpy provide a linalg.lstsq
function with a very similar interface. The documentation does not mention which kind of algorithm is used, neither for scipy.linalg.lstsq nor for numpy.linalg.lstsq, but it seems to do pretty much the same.
The implementation seems to be different for scipy.linalg.lstsq and numpy.linalg.lstsq. Both seem to use LAPACK, both algorithms seem to use a SVD.
Where is the difference? Which one should I use?
Note: do not confuse linalg.lstsq
with scipy.optimize.leastsq
which can solve also non-linear optimization problems.
If I read the source code right (Numpy 1.8.2, Scipy 0.14.1
), numpy.linalg.lstsq()
uses the LAPACK routine xGELSD
and scipy.linalg.lstsq()
usesxGELSS
.
The LAPACK Manual Sec. 2.4 states
The subroutine xGELSD is significantly faster than its older counterpart xGELSS, especially for large problems, but may require somewhat more workspace depending on the matrix dimensions.
That means that Numpy is faster but uses more memory.
Update August 2017:
Scipy now uses xGELSD by default https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.lstsq.html