np.prod() -- numpy



np.prod() 函数用来计算所有元素的乘积,对于有多个维度的数组可以指定轴,如 axis=1指定计算每一行的乘积。

prod(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
    Return the product of array elements over a given axis.

    Parameters
    ----------
    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.

        .. versionadded:: 1.7.0

        If axis is a tuple of ints, a product is performed on all of the
        axes specified in the tuple instead of a single axis or all the
        axes as before.
    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 `~numpy.ufunc.reduce` for details.

        .. versionadded:: 1.15.0

    where : array_like of bool, optional
        Elements to include in the product. See `~numpy.ufunc.reduce` for details.



参数           描述    
a            输入 arrays 数组
axis         可选,指定求积的维度。默认值 axis=None 将计算输入数组中所有元素的乘积。如果轴为负数,则从最后一个轴计算到第一个轴。
dtype        可选,返回数组的类型,以及相乘元素的累加器的类型。
out        ndarray,可选。用于放置结果的可选输出数组。它必须具有与预期输出相同的形状,但是输出值的类型将在必要时进行转换。
keepdims     可选,保持维度,不缩减
initial    可选,起始数,即返回的矩阵会在元素乘积上再乘起始数
where        array_like of bool, optional


返回值: 一个形状为但已删除指定轴的数组。如果指定,返回对out的引用。


示例

默认情况下,计算所有元素的乘积:

In [4]: np.prod([1, 2])
Out[4]: 2

空数组的乘积是 1:

In [5]: np.prod([])
Out[5]: 1.0

如果输入数组为二维的:

In [6]: np.prod([[1,2],[3,4]])
Out[6]: 24

可以指定要相乘的轴:

n [8]: np.prod([[1,2],[3,4]], axis=1)
Out[8]: array([ 2, 12])
In [9]: np.prod([[1,2],[3,4]], axis=0)
Out[9]: array([3, 8])

如果要选择特定的元素相乘:

In [10]: np.prod([1, np.nan, 3], where=[True, False, True])
Out[10]: 3.0
可以设定一个初始值,与输入数组中的元素相乘:
In [11]: np.prod([1,2], initial=5)
Out[11]: 10



reference

https://blog.csdn.net/weixin_40522801/article/details/106578775