本文介绍了随机播放vs置换numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
numpy.random.shuffle(x)
和numpy.random.permutation(x)
有什么区别?
我已经阅读了文档页面,但是当我只想随机地对数组元素进行随机排列时,我不明白两者之间是否有任何区别.
I have read the doc pages but I could not understand if there was any difference between the two when I just want to randomly shuffle the elements of an array.
更准确地说,假设我有一个数组x=[1,4,2,8]
.
To be more precise suppose I have an array x=[1,4,2,8]
.
如果我想生成x的随机排列,那么shuffle(x)
和permutation(x)
有什么区别?
If I want to generate random permutations of x, then what is the difference between shuffle(x)
and permutation(x)
?
推荐答案
np.random.permutation
与np.random.shuffle
有两个区别:
- 如果传递了一个数组,它将返回该数组的改组的副本;
np.random.shuffle
将数组按顺序随机播放 - 如果传递整数,它将返回经过改组的范围,即
np.random.shuffle(np.arange(n))
- if passed an array, it will return a shuffled copy of the array;
np.random.shuffle
shuffles the array inplace - if passed an integer, it will return a shuffled range i.e.
np.random.shuffle(np.arange(n))
源代码可能有助于理解这一点:
The source code might help to understand this:
3280 def permutation(self, object x):
...
3307 if isinstance(x, (int, np.integer)):
3308 arr = np.arange(x)
3309 else:
3310 arr = np.array(x)
3311 self.shuffle(arr)
3312 return arr
这篇关于随机播放vs置换numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!