numpy bincount 用法

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``.
import numpy as np

x = np.array([1, 1, 3, 2, 1, 7])
print(x)
print(np.bincount(x))
print(np.bincount(x, minlength=20))

# weights参数被指定,那么x会被它加权,也就是说,
# 如果值n发现在位置i,那么 out[n] += weight[i] 而不是 out[n] += 1.
# weights 与 x 要大小相同
w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6])
print(np.bincount(x, weights=w))
# 0     0
# 1     0.3+0.5+1=1.8
# 2     0.7
# 3     0.2
# 4     0
# 5     0
# 6     0
# 7     -0.6
[1 1 3 2 1 7]
[0 3 1 1 0 0 0 1]
[0 3 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0.   1.8  0.7  0.2  0.   0.   0.  -0.6]

Process finished with exit code 0

你可能感兴趣的:(numpy bincount 用法)