随机播放vs置换numpy

编程入门 行业动态 更新时间:2024-10-27 12:34:25
本文介绍了随机播放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))

如果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

本文发布于:2023-10-28 08:15:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!