Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between numpy.linalg.lstsq and scipy.linalg.lstsq?

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.

like image 390
lumbric Avatar asked Mar 31 '15 15:03

lumbric


People also ask

What does numpy Linalg Lstsq do?

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.

What is SciPy Linalg?

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.

What does NP Linalg Lstsq return?

lstsq. Return the least-squares solution to a linear matrix equation.


1 Answers

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

like image 199
Dietrich Avatar answered Sep 28 '22 16:09

Dietrich