1、rand
根据给定的形状,随机生成
[0, 1)
之间的数字,若没有给定参数,则只返回一个数字。
原型:np.random.rand(d0, d1, …, dn)
参数
:d0, d1, …, dn : int, optional
指返回数据的维度,参数必须为非负数,可以不指定此参数,此时将只返回一个数字
示例:
不给定参数:
>>> np.random.rand()
0.9177148719428896
给定参数,此处生成3行4列的数据:
>>> np.random.rand(3,4)
array([[0.09354481, 0.92966221, 0.07354093, 0.62198543],
[0.80354065, 0.08066927, 0.05077123, 0.23747131],
[0.786092 , 0.06543784, 0.27048621, 0.62963905]])
2、randn
根据给定的形状,从
均值为0,方差为1的高斯分布
中随机采样
浮点型
数据,作为返回结果,若没有给定参数,则只随机采样一个数据返回。
原型:np.random.randn(d0, d1, …, dn)
参数:
d0, d1, …, dn : int, optional
指返回数据的维度,参数必须为非负数,可以不指定此参数,此时将只返回一个数字
示例:
没有给定参数:
>>> np.random.randn()
-1.1039987197992454
给定参数,此处生成3行4列的数据:
>>> np.random.randn(3,4)
array([[-0.48341558, 1.02303556, -0.02776572, -0.76110079],
[-0.10921833, 1.92420365, -0.20852098, -0.11236415],
[-0.26738315, 0.20381277, 0.52988541, -0.56896098]])
若要随机生成以μ为均值,σ²为方差的高斯分布数据,则可以使用:
σ * np.random.randn(…) + μ
如:生成 N(3, 6.25) 分布的随机数据[2, 4]
>>> 3 + 2.5 * np.random.randn(2, 4)
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677],
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]])
3、randint
根据给定的形状,随机生成
[low, high)
之间的整数,若没有给定high参数,则随机生成
[0, low)
之间的整数,若size没有给定,则只返回一个整数。
原型:np.random.randint(low, high=None, size=None, dtype=‘l’)
参数:
low:
int or array-like of ints
返回数据中的下限值,当high为None时,此参数为返回数据中的上限值,也可以是一个列表
high:
int or array-like of ints, optional
返回数据中的上限值,可以不指定,也可以是一个列表
size :
int or tuple of ints, optional
指定返回数据的形状,若不指定,则只返回一个整数
dtype :
dtype, optional
指定返回数据的类型,默认为np.int
示例:
只指定数据的上限:
>>> np.random.randint(6)
4
指定数据上下限,并给定形状:
>>> np.random.randint(2,6,size=(3,4))
array([[2, 3, 5, 4],
[3, 5, 5, 4],
[4, 5, 5, 5]])
给定不同的上限:(1.17版本以上支持)
>>> np.random.randint(1, [3, 5, 10])
array([2, 2, 9])
给定不同的下限:(1.17版本以上支持)
>>> np.random.randint([1, 5, 7], 10)
array([9, 8, 7])
4、choice
从给定的数据中选取给定形状的数据。
原型:np.random.choice(a, size=None, replace=True, p=None)
参数:
a :
1-D array-like or int
给定的数据或一个整型数据。若为ndarray,则从给定的数据中随机选取数据,若为一个整型数据,则从np.arange(a)中随机选取数据
size :
int or tuple of ints, optional
返回数据的形状,不指定时只返回一个数据
replace :
boolean, optional
replace=True时表示有放回的取数,replace=False时表示无放回的取数,通俗的讲就是为True时一个元素可以多次选取,为False时每个元素只能选取一次。默认为True
p :
1-D array-like, optional
和a的形状相同,表示a中每个数据的概率
示例:
a为一个数:
>>> np.random.choice(5,3)
array([4, 0, 2])
a为ndarray:
>>> np.random.choice([2,5,3,7,8], size=[2,2])
array([[2, 3],
[8, 7]])
当replace为False时,size的大小不能大于a的数据个数:
# 当size大小(6)不大于a的大小(6)时,正常:
>>> np.random.choice([1,2,3,4,5,6], size=[2,3], replace=False)
array([[5, 2, 6],
[4, 1, 3]])
# 当size大小(9)大于a的大小(6)时,报错:
>>> np.random.choice([1,2,3,4,5,6], size=[3,3], replace=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mtrand.pyx", line 1168, in mtrand.RandomState.choice
ValueError: Cannot take a larger sample than population when 'replace=False'
5、shuffle
将给定的数据进行随机排序,使数据无序;若为多维数组,则只对第一维进行洗牌,其余维不变。
原型:np.random…shuffle(x)
参数:
x :
array_like
需要洗牌的数组或列表
示例:
对于一维数组或列表:
>>> a = [10, 29, 49, 20, 58]
>>> np.random.shuffle(a)
>>> a
[58, 10, 29, 20, 49]
对于二维数组:
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> np.random.shuffle(a)
>>> a
[[4, 5, 6], [7, 8, 9], [1, 2, 3]]
6、random
根据给定的形状,随机生成
[0.0, 1.0)
之间的
浮点型
数据。
原型:np.random.random(size=None, dtype=‘d’, out=None)
参数:
size :
int or tuple of ints, optional
返回数据的形状,默认时只返回一个数据
dtype :
{str, dtype}, optional
返回数据的数据类型,默认为’d’即float64
out :
ndarray, optional
示例:
不给定任何参数:
>>> np.random.random()
0.994857946703592
指定size的值:
>>> np.random.random(size=(2,3))
array([[0.63147329, 0.17752398, 0.68163829],
[0.9173325 , 0.87270742, 0.39761573]])
7、uniform
原型:np.random.uniform(low=0.0, high=1.0, size=None)
参数:
low :
float or array_like of floats, optional
输出数据的下限值,默认为0.0
high :
float or array_like of floats
输出数据的上限值,默认为1.0
size :
int or tuple of ints, optional
输出数据的形状。若为指定,则只输出一个数据;若size没有指定,而low或hige指定为非常量(比如列表),则返回np.broadcast(low, high).size形状的数据,如low=1,high=[2,3,4,5],则返回[1,4]形状的数据
示例:
未指定任何参数,默认返回[0.0, 1.0)之间的一个数据:
>>> np.random.uniform()
0.5158813519405029
指定输出数据的形状为2行3列:
>>> np.random.uniform(size=[2,3])
array([[0.22826952, 0.3800571 , 0.42333292],
[0.39361266, 0.42119074, 0.96617646]])
未指定size时,指定high为一个非常量,输出数据的形状由low和hige的形状而定:
# 指定hige为一个非常量
>>> np.random.uniform(low=0, high=[1,2,3,4])
array([0.77303588, 1.48993969, 1.19249964, 1.51544523])
# 指定一个非一维的非常量high
>>> np.random.uniform(high=[[2,4],[6,8]])
array([[1.73997403, 0.74716176],
[3.87522426, 7.9060629 ]])
# 指定low为一个非常量
>>> np.random.uniform(low=[0,1,2,3], high=9)
array([2.14857954, 4.35577815, 5.43381644, 4.37369653])
# 同时指定low和high为非常量,此时low和high的形状必须相同
>>> np.random.uniform(low=[1,3,5,7], high=[2,4,6,8])
array([1.9812424 , 3.51672234, 5.63879449, 7.2477922 ])
# 同时指定low和hige为非常量,且都不是一维的
>>> np.random.uniform(low=[[1,3],[5,7]], high=[[2,4],[6,8]])
array([[1.26686063, 3.99613498],
[5.16915922, 7.32881325]])
注:若同时指定low和high为非常量,则low和hige的形状必须一致,否则报错