ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

详述numpy中的np.random各个函数的用法

2026/9/10 5:20:53 拓冰建站 浏览量
详述numpy中的np.random各个函数的用法

文章目录

  • 引言
  • np.random.rand()
  • np.random.randn()
  • np.random.randint(low,high,size,dtype)
  • np.random.uniform(low,high,size)
  • 参考文献

引言

在机器学习还有深度学习中,经常会用到这几个函数,为了便于以后熟练使用,现在对这几个函数进行总结。

np.random.rand()

该函数括号内的参数指定的是返回结果的形状,如果不指定,那么生成的是一个浮点型的数;如果指定一个数,那么生成的是一个numpy.ndarray类型的数组;如果指定两个数字,那么生成的是一个二维的numpy.ndarray类型的数组。如果是两个以上的数组,那么返回的维度就和指定的参数的数量个数一样。其返回结果中的每一个元素是服从0~1均匀分布的随机样本值,也就是返回的结果中的每一个元素值在0-1之间

import numpy as npmat = np.random.rand()
print(mat)
print(type(mat))mat = np.random.rand(2)
print(mat)
print(type(mat))mat = np.random.rand(3, 2)
print(mat)
print(type(mat))0.3302456019848581
<class 'float'>
[0.29082465 0.06080064]
<class 'numpy.ndarray'>
[[0.97332433 0.26962929][0.14822891 0.48075266][0.51435701 0.6777654 ]]
<class 'numpy.ndarray'>

np.random.randn()

该函数和rand()函数比较类似,只不过运用该函数之后返回的结果是服从均值为0,方差为1的标准正态分布,而不是局限在0-1之间,也可以为负值,因为标准正态分布的曲线是关于x轴对阵的
其括号内的参数如果不指定,那么生成的是一个浮点型的数;如果指定一个数,那么生成的是一个numpy.ndarray类型的数组;如果指定两个数字,那么生成的是一个二维的numpy.ndarray类型的数组。和rand()相比,除了元素值不一样,其他的性质是一样的。

举例说明:

import numpy as npmat = np.random.randn()
print(mat)
print(type(mat))mat = np.random.randn(2)
print(mat)
print(type(mat))mat = np.random.randn(3, 2)
print(mat)
print(type(mat))-0.21972623884742504
<class 'float'>
[-0.44100389 -0.60992345]
<class 'numpy.ndarray'>
[[ 1.33705353 -2.1104243 ][ 0.1205673  -1.17654688][-0.71155393  0.89750309]]
<class 'numpy.ndarray'>

np.random.randint(low,high,size,dtype)

  • low:生成的元素值的最小值,即下限,如果没有指定high这个参数,则low为生成的元素值的最大值。
  • high:生成的元素值的最大值,即上限。
  • size:指定生成元素值的形状,也就是数组维度的大小。
  • dtype:指定生成的元素值的类型,如果不指定,默认为整数型

返回结果:返回值是一个大小为size的数组,如果指定了low和high这两个参数,那么生成的元素值的范围为[low,high),不包括high;如果不指定high这个参数,则生成的元素值的范围为[0,low)。如果不指定size这个参数,那么生成的元素值的个数只有一个。

import numpy as np
# 指定一个参数low
mat = np.random.randint(low=1)
print(mat)
print(type(mat))# 指定low和high,生成一个[low,high)的元素值
mat = np.random.randint(low=1, high=5)
print(mat)
print(type(mat))# 指定size大小,生成一个三行三列的二维数组,元素个数为3x3=9个
mat = np.random.randint(low=2, high=10, size=(3, 3))
print(mat)
# 查看默认元素值的类型
print(type(mat[0][0]))mat = np.random.randint(low=2, high=10, size=(3, 3), dtype=np.uint8)
print(mat)
print(type(mat[0][0]))0
<class 'int'>
2
<class 'int'>
[[9 7 9][2 7 9][4 6 3]]
<class 'numpy.int32'>
[[3 6 8][8 2 4][9 7 7]]
<class 'numpy.uint8'>

np.random.uniform(low,high,size)

  • low:生成元素值的下界,float类型,默认值为0
  • high:生成元素值的上界,float类型,默认值为1
  • size:输出样本的数目,可以指定一个值,也可指指定大于等于两个值
  • 返回对象:ndarray类型,形状为size中的数值指定,其元素个数为size指定的参数的乘积

我们前面已经说过了rand()这个函数,它返回的元素值是服从0-1的均匀分布,那如果不想要生成的是0-1范围内的均匀分布,想要其它范围内的均匀分布怎么办呢。

uniform()实现了这个功能,它可以生成服从指定范围内的均匀分布的元素。其返回值的元素类型为浮点型。需注意的是元素值的范围包含low,不包含high。

import numpy as np
# 指定一个参数low
mat = np.random.uniform()
print(mat)
print(type(mat))# 指定low和high,生成一个[low,high)的元素值
mat = np.random.uniform(low=5, high=10)
print(mat)
print(type(mat))# 指定size大小,生成一个三行三列的二维数组,元素个数为3x3=9个
mat = np.random.uniform(low=2, high=10, size=(3, 3))
print(mat)
# 查看默认元素值的类型
print(type(mat[0][0]))mat = np.random.uniform(low=2, high=10, size=(3, 3, 2))
print(mat)
print(type(mat[0][0][0]))0.30890343025056644
<class 'float'>
6.179533175254239
<class 'float'>
[[4.78938242 2.6997556  9.57992305][4.54539736 2.29297774 5.67460754][2.16260941 9.79336582 3.88221078]]
<class 'numpy.float64'>
[[[6.95629018 9.09524542][5.5167461  8.58762047][9.96385597 4.94498637]][[7.39177209 6.52374891][3.58829809 3.81779054][3.03570073 8.53538727]][[4.08470882 5.27155178][3.49804102 4.4184242 ][5.27197221 4.7202293 ]]]
<class 'numpy.float64'>

参考文献

  • https://blog.csdn.net/BaoITcore/article/details/125273828