I have one-hot encoded data of undefined shape within an array of ndim = 3
, e.g.,:
import numpy as np
arr = np.array([ # Axis 0
[ # Axis 1
[0, 1, 0], # Axis 2
[1, 0, 0],
],
[
[0, 0, 1],
[0, 1, 0],
],
])
What I want is to shuffle values for a known fraction of sub-arrays along axis=2
.
If this fraction is 0.25
, then the result could be:
arr = np.array([
[
[1, 0, 0], # Shuffling happened here
[1, 0, 0],
],
[
[0, 0, 1],
[0, 1, 0],
],
])
I know how to do that using iterative methods like:
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
if np.random.choice([0, 1, 2, 3]) == 0:
np.random.shuffle(arr[i][j])
But this is extremely inefficient.
Edit: as suggested in the comments, the random selection of a known fraction should follow an uniform law.