问题
I have a numpy array of the format
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
Each column represents a data channel, and I need to shuffle the contents of each column within that column independently of the other channels. I understand that numpy.random.shuffle only shuffles along the first axis of the array ie. shuffles the order of rows within the array. What is the best way to carry out an independent shuffle within each column?
回答1:
We could generate unique row indices for each column and index into the the input array with advanced-indexing. To generate the unique indices, we would use random float generation + sort trick, thus giving us a vectorized solution, like so -
idx = np.random.rand(*a.shape).argsort(0)
out = a[idx, np.arange(a.shape[1])]
Generic version
We could generalize it to cover generic n-dim arrays and along generic axes with np.take_along_axis and end up something as listed in this post.
来源:https://stackoverflow.com/questions/49426584/shuffle-independently-within-column-of-numpy-array