https://blog.csdn.net/Jinyindao243052/article/details/107220951

numpy.prod(a, axis=None, dtype=None, out=None, keepdims=, initial=, where=)

Return the product of array elements over a given axis.

官方链接

a [array_like]
Input data.

axis [None or int or tuple of ints, optional]
Axis or axes along which a product is performed. The default, axis=None, will calculate the product of all the elements in the input array. If axis is negative it counts from the last to the first axis.

dtype [dtype, optional]
The type of the returned array, as well as of the accumulator in which the elements are multiplied. The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.

out [ndarray, optional]
Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary.

keepdims [bool, optional]
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

If the default value is passed, then keepdims will not be passed through to the prod method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised.

initial [scalar, optional]
The starting value for this product. See reduce for details.

where [array_like of bool, optional]
Elements to include in the product. See reduce for details.

若指定了axis,把数组a沿着维度axis进行切片,并把切片后得到的一系列数组每个对应位置的元素相乘(element-wise product),如果axis未指定,则对数组进行扁平化后计算所有元素的乘积.

示例1. 把向量或矩阵中所有元素相乘

import numpy as np
mat= np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
print(np.prod(mat))

 
 

    结果:

    216
    
     
     

      示例2. 指定 axis,实现某一维度的元素相乘

      例如对于二维数组,指定axis=0,则按照行进行切片,然后对应位置相乘(element-wise product),得到的是每一列元素相乘的乘积。
      相当于np.multiply.reduce(mat,axis=0)

      import numpy as np
      mat= np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
      print(np.prod(mat,axis=0))
      print(np.multiply.reduce(mat,axis=0))
      
       
       

        结果:

        [ 1  8 27]
        [ 1  8 27]
        
         
         

          示例3. 指定where,实现只对某些index的元素进行相乘

          如果要计算 [1,2,3,4,5,6] 中index为 [0,2,4] 三个元素的乘积

          np.prod([1., np.nan, 3.], where=[True, False, True])
          
           
           

            结果

            3.0
            
             
             
              • 0
                点赞
              • 0
                收藏
                觉得还不错? 一键收藏
              • 0
                评论

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

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

              请填写红包祝福语或标题

              红包个数最小为10个

              红包金额最低5元

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

              抵扣说明:

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

              余额充值