Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy shuffle multidimensional array by row only, keep column order unchanged

How can I shuffle a multidimensional array by row only in Python (so do not shuffle the columns).

I am looking for the most efficient solution, because my matrix is very huge. Is it also possible to do this highly efficient on the original array (to save memory)?

Example:

import numpy as np X = np.random.random((6, 2)) print(X) Y = ???shuffle by row only not colls??? print(Y) 

What I expect now is original matrix:

[[ 0.48252164  0.12013048]  [ 0.77254355  0.74382174]  [ 0.45174186  0.8782033 ]  [ 0.75623083  0.71763107]  [ 0.26809253  0.75144034]  [ 0.23442518  0.39031414]] 

Output shuffle the rows not cols e.g.:

[[ 0.45174186  0.8782033 ]  [ 0.48252164  0.12013048]  [ 0.77254355  0.74382174]  [ 0.75623083  0.71763107]  [ 0.23442518  0.39031414]  [ 0.26809253  0.75144034]] 
like image 592
robert Avatar asked Feb 26 '16 08:02

robert


People also ask

How do I shuffle a NumPy array row?

You can use numpy. random. shuffle() . This function only shuffles the array along the first axis of a multi-dimensional array.

What does .all do in NumPy?

all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.

How do you sort a multidimensional NumPy array?

NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value.

How does NumPy shuffle work?

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.


1 Answers

You can use numpy.random.shuffle().

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.

In [2]: import numpy as np                                                                                                                                                                                    In [3]:                                                                                                                                                                                                       In [3]: X = np.random.random((6, 2))                                                                                                                                                                          In [4]: X                                                                                                                                                                                                    Out[4]:  array([[0.71935047, 0.25796155],        [0.4621708 , 0.55140423],        [0.22605866, 0.61581771],        [0.47264172, 0.79307633],        [0.22701656, 0.11927993],        [0.20117207, 0.2754544 ]])  In [5]: np.random.shuffle(X)                                                                                                                                                                                  In [6]: X                                                                                                                                                                                                    Out[6]:  array([[0.71935047, 0.25796155],        [0.47264172, 0.79307633],        [0.4621708 , 0.55140423],        [0.22701656, 0.11927993],        [0.20117207, 0.2754544 ],        [0.22605866, 0.61581771]]) 

For other functionalities you can also check out the following functions:

  • random.Generator.shuffle

  • random.Generator.permutation

  • random.Generator.permuted

The function random.Generator.permuted is introduced in Numpy's 1.20.0 Release.

The new function differs from shuffle and permutation in that the subarrays indexed by an axis are permuted rather than the axis being treated as a separate 1-D array for every combination of the other indexes. For example, it is now possible to permute the rows or columns of a 2-D array.

like image 156
Mazdak Avatar answered Oct 21 '22 15:10

Mazdak