Numpy 的 random 函数总结

numpy的random函数

import numpy as np

numpy.random.rand()

  • numpy.random.rand(d0,d1,…,dn)
  • rand 函数根据给定维度生成 [0,1) 之间的数据,包含 0,不包含 1
  • dn 表格每个维度
  • 返回值为指定维度的 array
np.random.rand(4,2,3)#创建423列的数据
out:
array([[[0.98328983, 0.89976007, 0.45187069],
        [0.7636354 , 0.1266366 , 0.59737245]],

       [[0.23233784, 0.72466596, 0.62792478],
        [0.65601457, 0.39634565, 0.67872993]],

       [[0.94510396, 0.1209695 , 0.51040255],
        [0.48820327, 0.25108958, 0.39326264]],

       [[0.93581577, 0.7946682 , 0.22548727],
        [0.46295792, 0.62925333, 0.93611731]]])

numpy.random.randn()

  • numpy.random.rand(d0,d1,…,dn)
- randn 函数返回一个或一组具有标准正态分布样本,。
- dn 表格每个维度
- 返回值为指定维度的 array
np.random.randn() #当没有参数,返回单个数据
out:
0.9978221054194687
np.random.randn(2,4)
out:
array([[-0.91464903, -1.34407418,  0.27328322,  1.48655677],
       [-0.73953291,  1.14596789, -0.37372058,  0.87601889]])
np.random.randn(4,2,4)
out:
array([[[ 0.49249151, -0.18689611, -0.7061262 ,  0.45240466],
        [-0.83259504,  2.71097971, -1.39491662,  2.08274104]],

       [[ 1.45643729, -1.47484262,  0.05107497, -1.06490762],
        [ 0.59100238, -0.01911552, -0.19523674, -0.26440994]],

       [[ 0.78185713,  0.67089298,  1.0351736 ,  0.65612953],
        [ 0.47037743, -0.7347524 ,  0.73128454, -0.26023019]],

       [[-0.27894992,  0.03703866, -0.32102327,  0.47836213],
        [ 1.3069379 , -0.24945773, -2.07441152, -0.72807191]]])
  • 标准正态分布介绍
  • 标准正态分布—standard normal distribution
  • 标准正态分布又称为 u 分布,是以 0 为均值、以 1 为标准差的正态分布,记为 N(0,1)。

numpy.random.randint()

  • numpy.random.randint(low, high=None, size=None, dtype=‘l’)
  • 返回随机整数,范围区间为 [low,high),包含 low,不包含 high
  • 参数:low 为最小值,high 为最大值,size 为数组维度大小,dtype 为数据类型,默认的数据类型是 np.int
  • high 没有填写时,默认生成随机数的范围是 [0,low)
np.random.randint(1,size=6)#返回[0,1)的整数,所以只有0
out:
array([0, 0, 0, 0, 0, 0])
np.random.randint(2,9,size=7)
out:
array([5, 2, 4, 3, 2, 8, 2])
np.random.randint(1,5) # 返 回 1 个 [1,5) 时 间 的 随 机 整 数
out:
4
np.random.randint(-5,5,size=(2,2))   
array([[ 2, -4],
       [-4,  0]])

numpy.random.choice()

  • numpy.random.choice(a, size=None, replace=True, p=None)
  • 从给定的一维数组中生成随机数
  • 参数:a 为一维数组类似数据或整数;size 为数组维度;p 为数组中的数据出现的概率
  • a 为整数时,对应的一维数组为 np.arange(a)
  • replace = False 随机数不能重复
np.random.choice(5,size=3)
out:
array([4, 1, 1])
a=np.array([1,3,5,7])
np.random.choice(a,size=4)
array([3, 5, 1, 1])
np.random.choice(5,size=(2,3))
array([[4, 4, 0],
       [4, 3, 2]])
demo_list=['iphone','huawei','xiaomi','sansumg']
np.random.choice(demo_list,size=(2,2))
array([['iphone', 'xiaomi'],
       ['sansumg', 'huawei']], dtype='<U7')
  • 参数 p 的长度与参数 a 的长度需要一致;
  • 参数 p 为概率,p 里的数据之和应为 1
np.random.choice(demo_list,size=(2,2),p=[0.2,0.3,0.3,0.2])
array([['sansumg', 'sansumg'],
       ['huawei', 'huawei']], dtype='<U7')

np.random.seed()

  • np.random.seed() 的作用:使得随机数据可预测。
  • 当我们设置相同的 seed,每次生成的随机数相同。如果不设置 seed,则每次会生成不同的随机数

```c
 np.random.seed(0)   #这个数没有要求,一个种子生成的随机数是一样的
np.random.rand(2)
array([0.5488135 , 0.71518937])
np.random.seed(0)   #一个种子生成的随机数是一样的
np.random.rand(2)
array([0.5488135 , 0.71518937])

np.random.seed(1555)
np.random.rand(4)
array([0.06316912, 0.76071872, 0.59230514, 0.8572013 ])
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值