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.
The numpy linalg lstsq() function returns the least-squares solution to a linear matrix equation. For example, it solves the equation ax = b by computing a vector x that minimizes the Euclidean 2-norm || b – ax ||^2.
Advertisements. SciPy is built using the optimized ATLAS LAPACK and BLAS libraries. It has very fast linear algebra capabilities. All of these linear algebra routines expect an object that can be converted into a two-dimensional array.
lstsq. Return the least-squares solution to a linear matrix equation.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With