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?