numpy之bincount()

目录

一、官方代码 

二、解释

(1)参数x

(2)参数weight

(3)minlength参数


一、官方代码 

def bincount(x, weights=None, minlength=0): # real signature unknown; restored from __doc__
    """
    bincount(x, weights=None, minlength=0)
    
        Count number of occurrences of each value in array of non-negative ints.
    
        The number of bins (of size 1) is one larger than the largest value in
        `x`. If `minlength` is specified, there will be at least this number
        of bins in the output array (though it will be longer if necessary,
        depending on the contents of `x`).
        Each bin gives the number of occurrences of its index value in `x`.
        If `weights` is specified the input array is weighted by it, i.e. if a
        value ``n`` is found at position ``i``, ``out[n] += weight[i]`` instead
        of ``out[n] += 1``.
    
        Parameters
        ----------
        x : array_like, 1 dimension, nonnegative ints
            Input array.
        weights : array_like, optional
            Weights, array of the same shape as `x`.
        minlength : int, optional
            A minimum number of bins for the output array.
    
            .. versionadded:: 1.6.0
    
        Returns
        -------
        out : ndarray of ints
            The result of binning the input array.
            The length of `out` is equal to ``np.amax(x)+1``.
    
        Raises
        ------
        ValueError
            If the input is not 1-dimensional, or contains elements with negative
            values, or if `minlength` is negative.
        TypeError
            If the type of the input is float or complex.
    
        See Also
        --------
        histogram, digitize, unique
    
        Examples
        --------
        >>> np.bincount(np.arange(5))
        array([1, 1, 1, 1, 1])
        >>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
        array([1, 3, 1, 1, 0, 0, 0, 1])
    
        >>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23])
        >>> np.bincount(x).size == np.amax(x)+1
        True
    
        The input array needs to be of integer dtype, otherwise a
        TypeError is raised:
    
        >>> np.bincount(np.arange(5, dtype=float))
        Traceback (most recent call last):
          ...
        TypeError: Cannot cast array data from dtype('float64') to dtype('int64')
        according to the rule 'safe'
    
        A possible use of ``bincount`` is to perform sums over
        variable-size chunks of an array, using the ``weights`` keyword.
    
        >>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
        >>> x = np.array([0, 1, 1, 2, 2, 2])
        >>> np.bincount(x,  weights=w)
        array([ 0.3,  0.7,  1.1])
    """
    pass

官方代码的话,有能力的同学可以自行可以理解一下。

二、解释

bincount(x, weights=None, minlength=0)

(1)参数x

bincount()函数的简单作用实际上是计算非负整数数组中每个值的出现次数

实例:

from numpy import *

a = array[1, 2, 3]
count = bincount(a)

print(count)

输出:

其中 1,2,3分别出现了一次,所以count[1] = 1, count[2] = 1, count[2] = 1.

(2)参数weight

后面的weights参数即设置权重,默认是1.

实例:

from numpy import *

a = array[1, 2, 3]
count = bincount(x=a, weights=[1, 1, 2])

print(count)

 输出:

(3)minlength参数

最后面的minlength参数设置的是最大长度

实例:

from numpy import *

a = array[1, 2, 3]
count = bincount(x=a, weights=[1, 1, 2], minlength=5)

print(count)

输出:

PS: 输入数组必须为整数dtype,否则将引发TypeError

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_52242662

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值