numpy的随机数可以说是让人头大的东西,很多很烦。网上关于这方面的笔记已经有很多了,我这里只是记录一下自己的学习记录。
我建议numpy随机数应该和TensorFlow的联系在一起记。因为二者很像。
这里就事论事,目前只讨论numpy的随机数,一下都是我认为有必要记下来的,太偏的可以用的时候再查。
函数名 | 介绍 | 举例 |
---|---|---|
rand(d0,d1,…,dn) | 根据d0‐dn(这是维度)创建随机数数组,浮点数, [0,1),均匀分布 | a = np.random.rand(6) b=np.random.rand(2,3) |
randn(d0,d1,…,dn) | 根据d0‐dn(维度)创建随机数数组,标准正态分布 | a=np.random.randn(6) 或者b = np.random.randn(2,3) |
randint(low[,high,shape]) | 根据shape创建随机整数或整数数组,范围是[low, high) | a = np.random.randint(4) 或者 b = np.random.randint(2,3,(3,4)) |
uniform(low,high,size) | 产生具有均匀分布的数组,low起始值,high结束值,size形状 | b = np.random.uniform(2,3,(3,3)) |
normal(loc,scale,size) | 产生具有正态分布的数组,loc均值,scale标准差,size形状 | b = np.random.normal(1,2,(3,)) |
一、rand(d0,d1,…,dn)
这里的()里面其实是维度的数字,不加括号!具体看表格的举例
代码:
# !/usr/bin/python3
# -*-coding:UTF-8-*-
# By WILL
#(0,1]均匀分布
import numpy as np
a = np.random.rand(6)#6个元素的一维数组
b = np.random.rand(2,3,4)#shape为(2,3,4)的三维数组
print(a)
print(b)
输出:
[0.24903965 0.64913743 0.1737653 0.34785809 0.2170771 0.29336794]
[[[0.13402201 0.39148006 0.46136384 0.97294437]
[0.5119429 0.68646208 0.61238962 0.25009788]
[0.40213124 0.48821726 0.76121545 0.46718131]]
[[0.8197439 0.76674252 0.90323344 0.27352438]
[0.08372028 0.32380205 0.58475252 0.24506404]
[0.43177359 0.11136429 0.92542631 0.60708086]]]
这里(2,3,4)的矩阵我稍微讲一下,就是2个3*4的矩阵拼成的高阶矩阵而已。
二、randn(d0,d1,…,dn)
# !/usr/bin/python3
# -*-coding:UTF-8-*-
# By WILL
import numpy as np
a = np.random.randn(6)
b = np.random.randn(2,3)#注意括号,不是np.random.randn((2,3))
print(a)
print(b)
输出:
#标准正态分布随机数
[-1.34022921 -0.56640098 0.66925275 1.44626578 -0.71127973 0.33580923]
[[-0.45992754 -1.13502646 0.00483663]
[ 0.75163909 -1.27879153 2.6424086 ]]
三、randint(low[,high,shape])
# !/usr/bin/python3
# -*-coding:UTF-8-*-
# By WILL
import numpy as np
a = np.random.randint(4)#只写一个数字,这个代表上限,即输出0,1,2,3这4种情况
b = np.random.randint(2,3,(3,4))#生成一个全是2的3*4的矩阵,顾头不顾尾
print(a)
print(b)
输出:
1
[[2 2 2 2]
[2 2 2 2]
[2 2 2 2]]
插入:关于前面3种的size问题
我之前一直强调:
np.random.rand与randn(2,3)不是((2,3))。就是要注意下面讲的都是要加()的。再举个例子,就知道我为什么要强调这个了:
import numpy as np
c = np.random.rand(4,5)
d = np.random.randint(3,4,c.shape)
e = np.random.randn(c.shape)#会报错
输出:TypeError: 'tuple' object cannot be interpreted as an integer
应写为:
import numpy as np
c = np.random.rand(4,5)
d = np.random.randint(3,4,c.shape)#输全为3的整数(3x4)
#e = np.random.randn(c.shape)#会报错
e = np.random.randn(*c.shape)#输出4x5的服从标准正态分布的随机数
print(d)
print(e)
输出:
[[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]]
[[-0.00753196 -1.06003821 1.1917362 0.45080751 -0.49987723]
[-0.12736549 0.72831398 -0.22197463 -0.84474108 -0.34209557]
[ 1.30245695 -1.61219897 1.95850545 0.06050734 -1.08363601]
[-0.07553212 0.41066717 0.39855432 0.20106866 -0.30214129]]
四、uniform(low[,high,shape])
import numpy as np
a = np.random.uniform(4)#1-4随机数,若填1,则只输出1.若填0,则0-1
b = np.random.uniform(2,3,(3,3))
print(a)
print(b)
输出:
1.2352355899190335
[[2.18358231 2.84459801 2.66078497]
[2.73159788 2.13475238 2.05381502]
[2.385709 2.09562214 2.85188914]]
五、normal(loc,scale,size))
import numpy as np
a = np.random.normal(1,2,(3,3))#1为均值,2为标准差
b = np.random.normal(1,2,(3,))#1为均值,2为标准差
print(a)
print(b)
输出:
[[ 3.41851261 3.27833347 -0.80306351]
[ 0.6024089 0.8783569 -1.69507413]
[-0.57707056 3.63375433 0.94453682]]
[2.71113466 1.08183334 3.78544957]
(陆续更新)