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))
如果x是整数,则随机置换np.arange(x).如果x是一个数组,请复制并随机洗净元素.
If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the elements randomly.
源代码可能有助于理解这一点:
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
发布评论