Python Numpy随机数总结——numpy.random.rand/randn/randint/random/uniform/seed

在学习一些算法的时候,经常会使用一些随机数来做实验,或者说用随机数来添加一些噪声。

下面就总结我平常用到的几个numpy.random库中的随机数和seed函数。

目录

1. rand基本用法

2. randn基本用法

3. 指定数学期望和方差的正态分布

4. random基本用法及和rand的辨析

5. randint基本用法

6. uniform基本用法

7. seed基本用法


1. rand基本用法

numpy.random.rand(d0, d1, …, dn),产生[0,1)之间均匀分布的随机浮点数,其中d0,d1....表示传入的数组形状。

示例1

import numpy as np
#产生形状为(2,)的数组,也就是相当于有2个元素的一维数组。
temp=np.random.rand(2)
print(temp)  #[0.70284298 0.40041697]
print(type(temp)) # 查看数据类型,<class 'numpy.ndarray'>
print(temp[0])  #查看第一个数
print(type(temp[0])) #查看具体元素的数据类型,<class 'numpy.float64'>

'''
结果如下:
[0.70284298 0.40041697]
<class 'numpy.ndarray'>
0.7028429826756175
<class 'numpy.float64'>
'''

示例2 

#产生一个2*4的数组,数组中的每个元素是[0,1)之间均匀分布的随机浮点数
temp=np.random.rand(2,4)
print(temp)
#查看一下数据类型
print(type(temp))


'''
结果如下所示:

[[0.39921792 0.42280677 0.32486705 0.02898902]
 [0.78987787 0.93125733 0.30446905 0.27728128]]
<class 'numpy.ndarray'>

'''

示例3

在rand()里面也可以没有参数,返回一个[0,1)之间的随机浮点数。

#产生一个[0,1)之间的随机数
temp=np.random.rand() #0.6143086490875544
print(temp) #<class 'float'>
print(type(temp)) # 查看数据类型,<<class 'float'>

 

2. randn基本用法

numpy.random.randn(d0, d1, …, dn)从标准正态分布中返回一个或多个样本值。 参数表示样本的形状。所谓标准正态分布就是指这个函数产生的随机数,服从均值为0,方差为1的分布,使用方法和rand()类似。

arr1=np.random.randn(2,4)  #二行四列,或者说一维大小为2,二维大小为4
#均值为0,方差为1
print(arr1)
print(type(arr1)) #<class 'numpy.ndarray'>

arr2=np.random.rand()
print(arr2) #0.37338593251088137
print(type(arr2))  #<class 'float'>


'''
结果如下:

[[ 0.56538481  0.41791992  0.73515441  1.73895318]
 [ 2.27590795 -1.17933538 -1.02008043  0.15744222]]
<class 'numpy.ndarray'>
0.37338593251088137
<class 'float'>

'''

 

3. 指定数学期望和方差的正态分布

很多时候,我们不满足于仅仅产生服从标准正态分布的一组随机数,而是希望能够灵活的指定均值和方差,可用如下方法实现:

For random samples from N(mu, sigma^2), use:

sigma * np.random.randn(...) + mu

如果希望产生的一组随机数,服从均值为mu,方差为sigma^2的正态分布,可以用上述公式完成。

#Two-by-four array of samples from N(3, 6.25):
arr3=2.5 * np.random.randn(2,4)+3  #2.5是标准差,3是期望
print(arr3)


"""
结果如下:

[[ 2.58150052  6.20108311  1.58737197  9.64447208]
 [ 2.68126136  0.63854145 -1.34499681  1.68725191]]

"""

 

4. random基本用法及和rand的辨析

numpy.random.random()方法返回随机生成的一个实数(浮点数),它在[0,1)范围内。

示例:

import numpy as np

x1=np.random.random()
print(x1)  #0.14775128911185142
print(type(x1))  #<class 'float'>

x2=np.random.random((3,3))
print(x2)
'''
[[0.07151945 0.00156449 0.66673237]
 [0.89764384 0.68630955 0.21589147]
 [0.50561697 0.27617754 0.5553978 ]]
'''
print(type(x2))  #<class 'numpy.ndarray'>
print(x2[1,1])  #0.68630955

注意:

这边需要注意的是这个函数的参数,只有一个参数“size”,有三种取值,None,int型整数,或者int型元组。

而在之前的numpy.random.rand()中可以有多个参数。

比方说,如果我们要产生一个2*4的随机数组(不考虑服从什么分布),那么在rand中的写法是:numpy.random.rand(2,4),而在random中的写法是numpy.random.random( (2,4) ),这里面是个元组,是有小括弧的。

temp1=np.random.random()
print(temp1)
print(type(temp1))

"""
参数为None,返回一个随机数。
0.06708973062154777
<class 'float'>
"""
#int整数和只有一维的int元组,效果一致

temp2=np.random.random(5)
print(temp2)
print(type(temp2))

temp3=np.random.random((5,))
print(temp3)
print(type(temp3))

'''
[0.69630427 0.35781327 0.17793115 0.24212922 0.51985133]
<class 'numpy.ndarray'>

[0.37211634 0.12352443 0.59535439 0.63269044 0.21921476]
<class 'numpy.ndarray'>
'''
temp4=np.random.random((2,4))
print(temp4)

'''
[[0.06215506 0.30826882 0.28453145 0.34466641]
 [0.84119912 0.81675337 0.27229602 0.21637321]]
'''

当然元组可以是多维的,不止二维,但是用的相对较少。

这个函数基础是产生[0,1)之间的随机数,但是我们可以通过其他方式,改变这个范围。

比如,我们要产生[-5,0)之间的随机数,可以这样:5 * numpy.random.random( (3,2) )-5。乘5后范围变成了[0,5),再减5范围变成了[-5,0)。

temp5=5 * np.random.random_sample((3, 2)) - 5
print(temp5)

'''
[[-0.6397764  -3.58968428]
 [-2.7789179  -4.74474732]
 [-1.7913781  -1.78819213]]
'''

和rand()的辨析。

一个是参数类型的不同,前文已经提到。

然后参看官方的帮助文档;

random:Return random floats in the half-open interval [0.0, 1.0).

rand:Create an array of the given shape and populate it with  random samples from a uniform distribution  over ``[0, 1)``.

英文这种东西,只可意会。不可翻译。

 

5. randint基本用法

用于生成指定范围内的整数。

具体函数:randint(low, high=None, size=None, dtype='l')

其中low是整型元素,表示范围的下限,可以取到。high表示范围的上限,不能取到。也就是左闭右开区间。

high没有填写时,默认生成随机数的范围是[0,low)

size可以是int整数,或者int型的元组,表示产生随机数的个数,或者随机数组的形状。

dtype表示具体随机数的类型,默认是int,可以指定成int64。

示例1:

#产生一个[0,10)之间的随机整数
temp1=np.random.randint(10)
print(temp1)
print(type(temp1))  #<class 'int'>

'''
5
<class 'int'>
'''

可以看出默认类型是int型,可以指定成int64.

示例2:

temp2=np.random.randint(10,dtype="int64")
print(type(temp2))

'''
<class 'numpy.int64'>
'''

示例3:

#产生[0,10)之间的随机整数8个,以数组的形式返回
temp3=np.random.randint(10,size=8)
print(temp3)
'''
[6 6 0 3 4 2 5 3]
'''

temp4=np.random.randint(10,size=(2,4))
print(temp4)
'''
[[7 5 4 5]
 [5 2 7 6]]
'''

示例4:

temp5=np.random.randint(5,10,size=(2,4))
print(temp5)

'''
[[8 5 8 6]
 [9 8 6 9]]
'''

 

6. uniform基本用法

从指定范围内产生均匀分布的随机浮点数。

函数:uniform(low=0.0, high=1.0, size=None)

low表示范围的下限,float型,或float型数组,默认为0.0.

high表示范围的上限,float型,或float型数组,默认为1.0.

size表示“形状”或“个数”,int型,或int型元组,默认为None。

示例1:

#默认产生一个[0,1)之间随机浮点数
temp=np.random.uniform()
print(temp) #0.9520851072880187

示例2:

temp=np.random.uniform(1,5,size=5)
print(temp)
'''
[4.94143282 1.56775945 3.74670851 4.06208558 1.25997891]
'''

temp=np.random.uniform(1,5,size=(2,4))
print(temp)
'''
[[2.3823538  3.09401949 3.92113219 1.0436445 ]
 [1.28329041 1.17418269 2.68699106 4.94529039]]
'''

拓展1

我想到一个问题,如果表示范围的数只有一个是什么意思?

help查看文档,low和size都是optional,那只有一个数应该表示为上限,但是实验结果并非如此:

temp=np.random.uniform(1.0001,size=10000000)
print(temp)
print(np.all(temp>= 1))
print(np.all(temp<1.0001))

'''
[1.00002898 1.00007222 1.00000062 ... 1.00008765 1.00002739 1.00004577]
True
True
'''

temp=np.random.uniform(0.99,size=10000000)
print(temp)
print(np.all(temp>= 0.99))
print(np.all(temp<1))

'''
[0.9928493  0.99711935 0.99146187 ... 0.9945594  0.99915136 0.99305246]
True
True
'''

temp=np.random.uniform(1,size=10000000)
print(temp)
print(np.all(temp==1))

'''
[1. 1. 1. ... 1. 1. 1.]
True
'''

由上面代码,得出结果,如果范围只有一个参数num,

如果num小于1,那么随机数的范围是[0,num)

如果num大于1,那么随机数的范围是[1,num)

如果num等于1,那么产生的随机数全是1。

拓展2

help显示low和high还可以是一个浮点数类型的数组。

就探究了一下到底是什么回事。

temp=np.random.uniform(low=[1.1, 2.1, 3.1, 4.1],high=[1.2, 2.2, 3.2, 4.2])  
print(temp)

'''
[1.17724259 2.19548916 3.16775407 4.12345112]
'''

可以看出第一个数的范围1.1和1.2之间,第二个数在2.1到2.2之间,…………

不出意外应该也是左闭右开。

在这种情况下,添加size,只能是一维的,并且大小要对应。

在上面的这段代码中,size=4 和 size=(4,) 和 size=(1,4)是正确的。size=(2,2),size=(4,1)提示错误。

 

7. seed基本用法

np.random.seed(),使得随机数据可预测。

如果在seed()中传入的数字相同,那么接下来生成的随机数序列都是相同的,仅作用于最接近的那句随机数产生语句。

np.random.seed(10)
temp1=np.random.rand(4)
print(temp1)
np.random.seed(10)
temp2=np.random.rand(4)
print(temp2)

#这句就不一样的,因为仅作用于最接近的那句随机数产生语句
temp3=np.random.rand(4)
print(temp3)

'''
[0.77132064 0.02075195 0.63364823 0.74880388]
[0.77132064 0.02075195 0.63364823 0.74880388]
[0.49850701 0.22479665 0.19806286 0.76053071]
'''

 

暂时就用到这些。之后用到了再更新。

行笔匆忙,欢迎指错。

  • 18
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值