Shuffle independently within column of numpy array [duplicate]

ⅰ亾dé卋堺 提交于 2020-01-05 08:23:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!