是否有用于Python数组同步改组的习惯用法或API? [英] Is there an idiom or API for synchronized shuffling of Python arrays?
问题描述
NumPy(或TensorFlow)中是否有一个API用于执行多个数组的同步改组(具有相同的第一维)?
Is there an API in NumPy (or perhaps TensorFlow) for performing a synchronized shuffling of several arrays (with the same first dimension)?
例如,如果我将两个数组的维度分别为(N,A)和(N,B),并且我想随机化每个数组的N个元素的顺序,同时保持第一个数组的元素与第二个.
For example, if I two arrays with dimensions (N, A) and (N, B), and I want to randomize the ordering of the N elements of each, while maintaining the association between the elements of the first array and the second.
是否有实现此目的的API或Python习惯用法?
Is there an API or Python idiom for accomplishing this?
请注意,将它们组合成N个元组的单个数组,然后用random.shuffle
进行混洗,这可能是我可以接受的一个选项,但我无法使它起作用:将原始数组取回来因为combined_array[:,0]
将对象具有维度(N,)作为元素,而不是维度(N,A),所以混乱(正如我所管理的那样),除非它是
Note that combining these into a single array of N tuples which are then shuffled with random.shuffle
might be an option that I'd accept as an answer, but I can't get that to work: getting the original arrays back is (as near as I've managed) messy since combined_array[:,0]
will have dimension (N,) with objects as elements, rather than dimension (N, A), unless it is manually rebuilt with something like [x for x in combined_array[:,0]
推荐答案
permutation = numpy.random.permutation(N)
arr1_shuffled = arr1[permutation]
arr2_shuffled = arr2[permutation]
选择一个排列并将其用于两个数组.
Pick one permutation and use it for both arrays.
这篇关于是否有用于Python数组同步改组的习惯用法或API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!