Search code examples
pythonarraysnumpypermutation

Array of permutations without Python for-loop


Say I have the array

import numpy as np

arr = np.array([1, 1, 2, 2, 3, 3, 4, 4])

I would like to an array with lots of random permutations of arr as its rows. Here's a solution that works:

np.array([np.random.permutation(arr) for _ in range(100_000)])

However, this is slower than generating an array of random integers of the same shape, which doesn't involve a Python for-loop:

In [15]: %%timeit
    ...: np.random.randint(1, 5, size=(100_000, 8))
    ...: 
    ...: 
10 ms ± 102 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [16]: %%timeit
    ...: np.array([np.random.permutation(arr) for _ in range(100_000)])
    ...: 
    ...: 
1.06 s ± 9.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Is there a way to vectorize this, so it doesn't slow down too much as I increase the number of permutations I'd like to generate?


Solution

  • I think numpy.random.Generator.permuted solves your issue:

    import numpy as np
    arr = np.array([1, 1, 2, 2, 3, 3, 4, 4])
    n = 10
    arr = np.tile(arr, (n, 1))
    rng = np.random.default_rng()
    rng.permuted(arr, axis=1)
    

    output:

    array([[4, 2, 3, 4, 3, 1, 2, 1],
           [3, 2, 4, 1, 1, 3, 4, 2],
           [2, 3, 3, 4, 1, 4, 2, 1],
           [2, 3, 1, 3, 1, 4, 4, 2],
           [2, 1, 2, 1, 4, 4, 3, 3],
           [3, 2, 3, 4, 2, 1, 1, 4],
           [2, 2, 3, 1, 4, 3, 1, 4],
           [1, 3, 3, 4, 1, 2, 2, 4],
           [4, 2, 3, 3, 1, 1, 2, 4],
           [1, 1, 2, 3, 4, 4, 2, 3]])