[Numpy-discussion] Efficient orthogonalisation with scipy/numpy

Gael Varoquaux gael.varoquaux at normalesup.org
Tue Jan 19 16:37:54 EST 2010

On Tue, Jan 19, 2010 at 03:16:59PM -0600, Robert Kern wrote:
> On Tue, Jan 19, 2010 at 15:12, Gael Varoquaux
> <gael.varoquaux at normalesup.org> wrote:
> > On Tue, Jan 19, 2010 at 02:58:32PM -0600, Robert Kern wrote:
> >> > I am not sure that what I am doing is optimal.

> >> If confounds is orthonormal, then there is no need to use lstsq().

> >>   y = y - np.dot(np.dot(confounds, y), confounds)

> > Unfortunately, confounds is not orthonormal, and as it is different at
> > each call, I cannot orthogonalise it as a preprocessing.

> Ah, then you shouldn't have said "Yes" when I asked if they were
> orthonormal. :-)

> However, you can orthonormalize inside the function and reuse that for
> both x and y. Using the QR decomposition is likely cheaper than the
> SVD that lstsq() does.

> ortho_confounds = linalg.qr(confounds.T)[0].T

Indeed! I wasn't aware that lstsq did an SVD. I thought it did the QR.
Though calculating the QR once for both vector is anyhow a gain.

I got another 20% speed gain in my total run time. Thanks!

For the google-completness of this thread, to get a speed gain, one needs
to use the 'econ=True' flag to qr.


More information about the NumPy-Discussion mailing list