Fri May 2 20:16:05 CDT 2008
On Fri, May 2, 2008 at 6:05 PM, Robert Kern <email@example.com> wrote:
> On Fri, May 2, 2008 at 7:24 PM, Keith Goodman <firstname.lastname@example.org> wrote:
> > How can I make this function faster? It removes the i-th row and
> > column from an array.
> > def cut(x, i):
> > idx = range(x.shape)
> > idx.remove(i)
> > y = x[idx,:]
> > y = y[:,idx]
> > return y
> > >> import numpy as np
> > >> x = np.random.rand(500,500)
> > >> timeit cut(x, 100)
> > 100 loops, best of 3: 16.8 ms per loop
> I can get a ~20% improvement with the following:
> In : %timeit cut(x, 100)
> 10 loops, best of 3: 21.6 ms per loop
> In : def mycut(x, i):
> ...: A = x[:i,:i]
> ...: B = x[:i,i+1:]
> ...: C = x[i+1:,:i]
> ...: D = x[i+1:,i+1:]
> ...: return hstack([vstack([A,C]),vstack([B,D])])
> In : %timeit mycut(x, 100)
> 10 loops, best of 3: 17.3 ms per loop
> The hstack(vstack, vstack) seems to be somewhat better than
> vstack(hstack, hstack), at least for these sizes.
Wow. I never would have come up with that. And I probably never will.
n = 500 took 5.28 seconds
n = 500 took 3.52 seconds
Much more than a 20% improvement when used in the larger program. Thank you.
More information about the Numpy-discussion