[Numpy-discussion] Linear least squares

Nathaniel Smith njs@pobox....
Tue Jan 8 17:47:06 CST 2013

On Tue, Jan 8, 2013 at 6:17 PM, Till Stensitz <mail.till@gmx.de> wrote:
> Hi,
> i did some profiling and testing of my data-fitting code.
> One of its core parts is doing some linear least squares,
> until now i used np.linalg.lstsq. Most of time the size
> a is (250, 7) and of b is (250, 800).
> Today i compared it to using pinv manually,
> to my surprise, it is much faster. I taught,
> both are svd based?

np.linalg.lstsq is written in Python (calling LAPACK for the SVD), so
you could run the line_profiler over it and see where the slowdown is.

An obvious thing is that it always computes residuals, which could be
costly; if your pinv code isn't doing that then it's not really
comparable. (Though might still be well-suited for your actual

Depending on how well-conditioned your problems are, and how much
speed you need, there are faster ways than pinv as well. (Going via qr
might or might not, going via cholesky almost certainly will be.)


More information about the NumPy-Discussion mailing list