[Numpy-discussion] Permutations in Simulations`
Keith Goodman
kwgoodman@gmail....
Tue Feb 10 14:28:24 CST 2009
On Tue, Feb 10, 2009 at 12:18 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Tue, Feb 10, 2009 at 11:29 AM, Mark Janikas <mjanikas@esri.com> wrote:
>> I want to create an array that contains a column of permutations for each
>> simulation:
>>
>> import numpy as NUM
>>
>> import numpy.random as RAND
>>
>> x = NUM.arange(4.)
>>
>> res = NUM.zeros((4,100))
>>
>>
>> for sim in range(100):
>>
>> res[:,sim] = RAND.permutation(x)
>>
>>
>> Is there a way to do this without a loop? Thanks so much ahead of time…
>
> Does this work? Might not be faster but it does avoid the loop.
>
> import numpy as np
>
> def weirdshuffle(nx, ny):
> x = np.ones((nx,ny)).cumsum(0, dtype=np.int) - 1
> yidx = np.ones((nx,ny)).cumsum(1, dtype=np.int) - 1
> xidx = np.random.rand(nx,ny).argsort(0).argsort(0)
> return x[xidx, yidx]
Hey, it is faster for nx=4, ny=100
def baseshuffle(nx, ny):
x = np.arange(nx)
res = np.zeros((nx,ny))
for sim in range(ny):
res[:,sim] = np.random.permutation(x)
return res
>> timeit baseshuffle(4,100)
1000 loops, best of 3: 1.11 ms per loop
>> timeit weirdshuffle(4,100)
10000 loops, best of 3: 127 µs per loop
OK, who can cut that time in half? My first try looks clunky.
More information about the Numpy-discussion
mailing list