1. np.prod() : prod函数用于求数组元素的乘积。
Note: 数组没有元素,即为空时,得到的值为 1.0 In [2]: np.prod([]) Out[2]: 1.0 In [4]: np.prod([[],[]]) Out[4]: 1.0
示例: >>> np.prod([1.,2.]) 2.0 >>> np.prod([[1.,2.],[3.,4.]]) 24.0 >>> np.prod([[1.,2.],[3.,4.]], axis=1) array([ 2., 12.])
了解更加详细参数相关信息,参看numpy.prod()
2. np.pad() : 根据需要将数组填充成指定的形状
这里只介绍卷积过程中为防止图片信息丢失使用的 0 填充模式
#二维数组 >>> a=np.arange(1,5).reshape(2,2) >>> a [[1 2] [3 4]] # a是二维数组, 中间的两个(2,3)分别针对 axis=0 和 axis=1 来填充 # mode="constant" 是默认填充值为0 >>> np.pad(a,((2,3),(2,3)),mode="constant") >>> array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 0, 0, 0], [0, 0, 3, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) # costant 有值时 按给出的值填充 In [15]: np.pad(a,((2,3),(2,3)),mode="constant",constant_values=(6,8)) Out[15]: array([[6, 6, 6, 6, 8, 8, 8], [6, 6, 6, 6, 8, 8, 8], [6, 6, 1, 2, 8, 8, 8], [6, 6, 3, 4, 8, 8, 8], [6, 6, 8, 8, 8, 8, 8], [6, 6, 8, 8, 8, 8, 8], [6, 6, 8, 8, 8, 8, 8]])
具体函数信息参看:numpy.pad()
3. np.random.randn() : 返回一个服从正态分布的数组
标准形式 :
: sigma * np.random.randn(...) + mu
#我们一般常用返回为服从N(0,1)正态分布数组 In [28]: np.random.randn(3,3) Out[28]: array([[ 0.77832362, -0.26915292, -0.0997728 ], [ 1.38962369, -0.4438962 , -1.00224716], [-1.06567318, 1.42600225, -0.6901084 ]])
具体参数信息参看 :https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html
4. np.random.rand() : 返回一个给定shape的(0,1)之间的均匀分布
# 返回一个 4*4 数值 In [29]: np.random.rand(4,4) Out[29]: array([[0.10359531, 0.88317262, 0.50667337, 0.38014409], [0.59356012, 0.54673751, 0.68416049, 0.89578659], [0.52713305, 0.20503638, 0.79684913, 0.73882077], [0.11927379, 0.00325085, 0.65378211, 0.94322903]])
具体函数信息参看: https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html
5. np.linspace()
在指定的间隔内返回均匀间隔的数字。返回number均匀分布的样本,在[start, stop]。这个区间的端点可以任意的被排除在外。
>>> >>> np.linspace(2.0, 3.0, num=5) array([ 2. , 2.25, 2.5 , 2.75, 3. ]) >>> np.linspace(2.0, 3.0, num=5, endpoint=False) array([ 2. , 2.2, 2.4, 2.6, 2.8]) >>> np.linspace(2.0, 3.0, num=5, retstep=True) (array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
函数具体信息参看:np.linspace()