0

I have a 3D numpy array and I want to shuffle it block wise in a particular axis while keeping the data in that block in it's original state. For instance I have an np array of shape (50, 140, 23) and I want to shuffle by making blocks of (50, 1, 23) on axis=1. So 140 blocks will be created and blocks should be shuffled on axis=1 while maintaining the data in blocks in it's original order. I read documentation about np.random.shuffle(x) but this only shuffles in first axis and we can't provide a block size to it. Is there any function in numpy or a quick way to do this?

2 Answers 2

3

You can use a random permutation:

A = sum(np.ogrid[0:0:50j,:140,0:0:23j])
rng = np.random.default_rng()
Ashuff = A[:,rng.permutation(140),:]
Sign up to request clarification or add additional context in comments.

Comments

2

Perhaps swapping axis, shuffling and swapping back might do the trick for you?

a = np.random.random((50,140,23))
b = np.swapaxes(a, 0, 1)
np.random.shuffle(b)
c = np.swapaxes(b, 0, 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.