Issue
Say I have an array
A = np.linspace(1,5,5)
A = np.array([1,2,3,4,5])
and I want to tile it 5 times, each time randomising the order, to give me for example
B = myfunc(A)
B = np.array([1,2,3,4,5,1,5,2,3,4,3,4,5,1,2,3,5,1,2,4,2,3,1,4,5])
How would I best go about this?
Note, the first tile doesn't have to be in order, nor does it have to be randomised, either works, whatever's easiest :)
Thanks
Solution
Simple implementation
import numpy as np
A = np.array([1,2,3,4,5])
def sampleNew(arr):
out = arr.copy() # Copy the array to avoid overwrite
np.random.shuffle(out) # Shuffle the copy
return out
random_samples = np.concatenate([ sampleNew(A) for _ in range(5) ])
# %timeit returns 13.1 µs ± 68.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
I'm sure there is a quicker way to do this.
Answered By - Diego
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.