韦忆 发表于 2023-5-12 00:18:47

numpy中的随机打乱数据方法np.random.shuffle解读

numpy随机打乱数据方法np.random.shuffle

import numpy as np
#实验可得每次shuffle后数据都被打乱,这个方法可以在机器学习训练
#的时候在每个epoch结束后将数据重新洗牌进入下一个epoch的学习
num = np.arange(20)
print(num)
np.random.shuffle(num)
print(num)
num1 = np.arange(20)
print(num1)
np.random.shuffle(num1)
print(num1)
np.random.shuffle(num1)
print(num1)#打印输出:[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19][ 1  5 19  9 14  2 12  3  6 18  4  8 16  0 10 17 13  7 15 11][ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19][ 2  4 13 14 11 17  9 19  5 12 15  7 18 16  3 10  1  8  0  6][ 8 11 13  6 19  7  9 12  4  3 10 14 15  2  1  0 17 18 16  5]
numpy随机生成数据问题


用numpy.random模块来生成随机数组

1.np.random.rand 用于生成[0.0, 1.0)之间的随机浮点数, 当没有参数时,返回一个随机浮点数,当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数建议是整数型。
import numpy as np
np.random.rand(8)output [ 0.55958421 0.97358761 0.77753246 0.28072869 0.18467794 0.857553360.03976048 0.08161713]2、np.random.randn这个函数返回一个样本,具有标准正态分布。
np.random.randn(8)output [ 0.5512808 1.32780895 -0.95738756 0.93710414 -2.0854875 -0.5100787 n 0.40982079 -1.235186 ]3、np.random.randint(low[, high, size]) 返回随机的整数,位于半开区间 [low, high)。
np.random.randint(10,size=10)output 4、random_integers(low[, high, size]) 返回随机的整数,位于闭区间 。
np.random.random_integers(5)output = 45、 np.random.shuffle(x) 类似洗牌,打乱顺序;np.random.permutation(x)返回一个随机排列.
arr = np.arange(10)
np.random.shuffle(arr)
print(arr)
np.random.permutation(10)outputarray()
用random模块自己构造

1、random.randint(low, hight) -> 返回一个位于之间的整数。
该函数接受两个参数,这两个参数必须是整数(或者小数位是0的浮点数),并且第一个参数必须不大于第二个参数
import random
random.randint(1,10)
random.randint(1.0,10.0)2、random.random() -> 不接受参数,返回一个[0.0, 1.0)之间的浮点数
random.random()3、random.randrange(start, stop, step) -> 返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),若start大于stop时 ,setp必须为负数.step不能是0
random.randrange(1,100,2)  #返回之间的奇数
random.randrange(100,1,-2)  #返回之间的偶数
总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

来源:https://www.jb51.net/article/283833.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: numpy中的随机打乱数据方法np.random.shuffle解读