I have a an array that is relatively sparse, and I would like to go through each row and shuffle only the non-zero elements.
Example Input:
[2,3,1,0] [0,0,2,1]
Example Output:
[2,1,3,0] [0,0,1,2]
Note how the zeros have not changed position.
To shuffle all elements in each row (including zeros) I can do this:
for i in range(len(X)): np.random.shuffle(X[i, :])
What I tried to do then is this:
for i in range(len(X)): np.random.shuffle(X[i, np.nonzero(X[i, :])])
But it has no effect. I've noticed that the return type of X[i, np.nonzero(X[i, :])]
is different from X[i, :]
which might be the cause.
In[30]: X[i, np.nonzero(X[i, :])] Out[30]: array([[23, 5, 29, 11, 17]]) In[31]: X[i, :] Out[31]: array([23, 5, 29, 11, 17])
You can use numpy. random. shuffle() . This function only shuffles the array along the first axis of a multi-dimensional array.
nonzero() function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(arr)] .
Modify a sequence in-place by shuffling its contents. This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.
You could use the non-inplace numpy.random.permutation
with explicit non-zero indexing:
>>> X = np.array([[2,3,1,0], [0,0,2,1]]) >>> for i in range(len(X)): ... idx = np.nonzero(X[i]) ... X[i][idx] = np.random.permutation(X[i][idx]) ... >>> X array([[3, 2, 1, 0], [0, 0, 2, 1]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With