zoukankan      html  css  js  c++  java
  • np.random.shuffle(x)与np.random.permutation(x)

    来自:https://blog.csdn.net/brucewong0516/article/details/79012233

    将数组打乱随机排列 两种方法:

    1. np.random.shuffle(x):在原数组上进行,改变自身序列,无返回值。
    2. np.random.permutation(x):不在原数组上进行,返回新的数组,不改变自身数组。

    1. np.random.shuffle(x)

    (1)、一维数组

    import numpy as np
    
    arr = np.arange(10)
    print(arr)
    
    np.random.shuffle(arr)
    print(arr)

    (2)、对多维数组进行打乱排列时,默认是列维度。

    arr = np.arange(12).reshape(3,4)
    print(arr)
    
    np.random.shuffle(arr)
    print(arr)

    2. np.random.permutation(x)

    (1)、可直接生成一个随机排列的数组

    np.random.permutation(10)

    (2)、一维数组

    np.random.permutation([1, 4, 9, 12, 15])

    (3)、多维数组

    arr = np.arange(9).reshape((3, 3))
    print(arr)
    
    arr2 = np.random.permutation(arr)
    print(arr)
    print(arr2)

    3. 区别

    从代码可以看出,np.random.shuffle(x)改变自身数组,np.random.permutation(x)不改变自身数组。

  • 相关阅读:
    MySQL系列(三) MySQL的约束
    mysql 下载 国内 镜像
    ckeditor
    比较时间
    远程获取文件
    多线程一例
    requests
    json传递对象字典
    pymysql和mysqldb的区别
    sql
  • 原文地址:https://www.cnblogs.com/keye/p/10794322.html
Copyright © 2011-2022 走看看