nlcpy.bincount

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

Counts 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
xarray_like, 1 dimension, nonnegative ints

Input array.

weightsarray_like, optional

Weights, array of the same shape as x.

minlengthint, optional

A minimum number of bins for the output array.

Returns
outndarray of ints

The result of binning the input array. The length of out is equal to vp.amax(x)+1.

See also

histogram

Computes the histogram of a set of data.

digitize

Return the indices of the bins to which each value in input array belongs.

Restriction

This function is the wrapper function to utilize numpy.bincount(). Calculations during this function perform on only Vector Host(Linux/x86).

Examples

>>> import nlcpy as vp
>>> vp.bincount(vp.arange(5))
array([1, 1, 1, 1, 1])
>>> vp.bincount(vp.array([0, 1, 1, 3, 2, 1, 7]))
array([1, 3, 1, 1, 0, 0, 0, 1])
>>> x = vp.array([0, 1, 1, 3, 2, 1, 7, 23])
>>> vp.bincount(x).size == vp.amax(x)+1
array(True)

The input array needs to be of integer dtype, otherwise a TypeError is raised:

>>> vp.bincount(vp.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 = vp.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
>>> x = vp.array([0, 1, 1, 2, 2, 2])
>>> vp.bincount(x,  weights=w)
array([0.3, 0.7, 1.1])