Python 洗牌 vs 置换 numpy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15474159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
shuffle vs permute numpy
提问by DotPi
What is the difference between numpy.random.shuffle(x)and numpy.random.permutation(x)?
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.
我已经阅读了文档页面,但是当我只想随机打乱数组的元素时,我无法理解两者之间是否有任何区别。
To be more precise suppose I have an array x=[1,4,2,8].
更准确地说,假设我有一个数组x=[1,4,2,8]。
If I want to generate random permutations of x, then what is the difference between shuffle(x)and permutation(x)?
如果我想生成 x 的随机排列,那么shuffle(x)和之间有什么区别permutation(x)?
采纳答案by ecatmur
np.random.permutationhas two differences from np.random.shuffle:
np.random.permutation有两个不同之处np.random.shuffle:
- if passed an array, it will return a shuffled copyof the array;
np.random.shuffleshuffles the array inplace - if passed an integer, it will return a shuffled range i.e.
np.random.shuffle(np.arange(n))
- 如果传递一个数组,它将返回该数组的无序副本;
np.random.shuffle原地打乱数组 - 如果传递一个整数,它将返回一个混洗的范围,即
np.random.shuffle(np.arange(n))
If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the elements randomly.
如果 x 是整数,则随机置换 np.arange(x)。如果 x 是一个数组,则复制并随机打乱元素。
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
回答by hlin117
Adding on to what @ecatmur said, np.random.permutationis useful when you need to shuffle ordered pairs, especially for classification:
添加@ecatmur 所说的,np.random.permutation当您需要打乱有序对时很有用,尤其是对于分类:
from np.random import permutation
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
# Data is currently unshuffled; we should shuffle
# each X[i] with its corresponding y[i]
perm = permutation(len(X))
X = X[perm]
y = y[perm]
回答by Mohanrac
Adding on @ecatmur, Here is a brief explanation. To start with I have created an array which is of shape 3,3 and has numbers from 0 to 8
添加@ecatmur,这里是一个简短的解释。首先,我创建了一个形状为 3,3 的数组,其数字从 0 到 8
import numpy as np
x1 = np.array(np.arange(0,9)).reshape(3,3) #array with shape 3,3 and have numbers from 0 to 8
#step1: using np.random.permutation
x_per = np.random.permutation(x1)
print('x_per:', x_per)
print('x_1:', x_1)
#Inference: x1 is not changed and x_per has its rows randomly changed
#The outcome will be
x1: [[0 1 2]
[3 4 5]
[6 7 8]]
x_per:[[3 4 5]
[0 1 2]
[6 7 8]]
#Lets apply shuffling
x2 = np.array(range(9)).reshape(3,3)
x2_shuffle = np.random.shuffle(x2)
print('x2_shuffle:', x2_shuffle)
print('x2', x2)
#Outcome:
x2_shuffle: None
x2 [[3 4 5]
[0 1 2]
[6 7 8]]
Key inference is: When x is an array, both numpy.random.permutation(x) and numpy.random.shuffle(x) can permute the elements in x randomly along the first axis. numpy.random.permutation(x) actually returns a new variable and the original data is not changed. Where as numpy.random.shuffle(x) has changed original data and does not return a new variable. I just tried to show with an example so it can help others. Thanks!!
关键推断是:当 x 是一个数组时, numpy.random.permutation(x) 和 numpy.random.shuffle(x) 都可以沿第一轴随机排列 x 中的元素。numpy.random.permutation(x) 实际上返回一个新变量并且原始数据没有改变。其中 numpy.random.shuffle(x) 已更改原始数据并且不返回新变量。我只是试图用一个例子来展示它可以帮助其他人。谢谢!!

