【问题标题】:shuffle vs permute numpyshuffle vs permute numpy
【发布时间】:2013-03-06 15:10:19
【问题描述】:

numpy.random.shuffle(x)numpy.random.permutation(x) 有什么区别?

我已经阅读了文档页面,但是当我只想随机打乱数组的元素时,我无法理解两者之间是否有任何区别。

更准确地说,假设我有一个数组 x=[1,4,2,8]

如果我想生成x的随机排列,那么shuffle(x)permutation(x)有什么区别?

【问题讨论】:

    标签: python numpy scipy permutation shuffle


    【解决方案1】:

    np.random.permutationnp.random.shuffle有两个不同之处:

    • 如果传递一个数组,它将返回一个打乱后的数组副本np.random.shuffle 就地打乱数组
    • 如果传递一个整数,它将返回一个打乱的范围,即np.random.shuffle(np.arange(n))

    如果 x 是整数,则随机置换 np.arange(x)。如果 x 是一个数组,则复制一份并随机打乱元素。

    源代码可能有助于理解这一点:

    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
    

    【讨论】:

    • panda.Index 上使用时,只有permutation 有效,shuffle 无效。这个案例如何适合您的解释?
    • @Heisenberg permutation 将其参数强制转换为 ndarray(通过复制); pandas.Indexshuffle 无法处理它的 ndarray 有很大不同,但可以处理从它创建的 ndarray。
    【解决方案2】:

    除了@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]
    

    【讨论】:

    • 我不断收到这个:TypeError:只有整数标量数组可以转换为标量索引
    • 为了澄清@hlin117,这仅在 x 和 y 是 numpy 数组时才有效。如果您尝试使用 python 内置列表来执行此操作,则会抛出 TypeError。
    【解决方案3】:

    添加@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]]
    

    关键推断是:当x是一个数组时,numpy.random.permutation(x)和numpy.random.shuffle(x)都可以随机排列x中的元素 第一个轴。 numpy.random.permutation(x) 实际上返回一个新变量并且原始数据没有改变。其中 numpy.random.shuffle(x) 已更改原始数据并且不返回新变量。我只是试图用一个例子来展示,这样它就可以帮助别人。谢谢!!

    【讨论】:

      【解决方案4】:

      permutation() 方法返回一个重新排列的数组(并且保持原始数组不变),该方法将保持原始数组不变并返回一个打乱的数组,例如 x = [1,4,2 ,8] 是原始数组,置换方法将返回重新排列的数组(比如说 [8,4,1,2])。现在,您有两个数组,原始数组和重新排列的数组。

      另一方面,

      shuffle() 方法对原始数组进行更改,例如 x = [1,4,2,8] 是原始数组,并且 shuffle 方法将返回 shuffle 数组(假设 shuffled 数组是 [8, 4,1,2])。现在,原来的数组本身变成了 Shuffled 数组,你只剩下 shuffled 数组了。

      参考:-https://www.w3schools.com/python/numpy_random_permutation.asp

      【讨论】:

        猜你喜欢
        • 2019-12-21
        • 2020-02-03
        • 2019-01-15
        • 1970-01-01
        • 2016-10-04
        • 1970-01-01
        • 2017-02-19
        • 2015-01-18
        • 1970-01-01
        相关资源
        最近更新 更多