Is there an idiom or API for synchronizing shuffling Python arrays?

Is there an API in NumPy (or maybe TensorFlow) to do synchronized shuffling of multiple arrays (with the same first dimension)?

For example, if I have two arrays with sizes (N, A) and (N, B), and I want to randomize the ordering of N elements of each of them, preserving the relationship between the elements of the first array and the second.

Is there an API or Python idiom to accomplish this?


Please note that combining them into a single array of N tuples, which are then shuffled with random.shuffle, may be an option that I would take as an answer, but I cannot make it work: return the original arrays (as far as I managed), since combined_array[:,0]will have a dimension (N) with objects as elements, and not a dimension (N, A), unless it is manually rebuilt with something like [x for x in combined_array[:,0]

+1
source share
2 answers
permutation = numpy.random.permutation(N)

arr1_shuffled = arr1[permutation]
arr2_shuffled = arr2[permutation]

Select one permutation and use it for both arrays.

+4
source

, , - , . () ( ) .

Numpy.

- , , , :

from random import shuffle

# These would be your input lists (or arrays, doesn’t really matter)
list1 = […]
list2 = […]

# generate a list of all indexes
indexes = list(range(len(list1)))

# shuffle the indexes
shuffle(indexes)

for i in indexes:
    print(list1[i], list2[i])

, / .

0
source

Source: https://habr.com/ru/post/1682478/


All Articles