[Numpy-discussion] Faster
Robert Kern
robert.kern@gmail....
Fri May 2 20:05:52 CDT 2008
On Fri, May 2, 2008 at 7:24 PM, Keith Goodman <kwgoodman@gmail.com> 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[0])
> 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 [8]: %timeit cut(x, 100)
10 loops, best of 3: 21.6 ms per loop
In [9]: 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 [10]: %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.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list