numpy 1.22.4 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturns
bincount(x, /, weights=None, minlength=0)

The number of bins (of size 1) is one larger than the largest value in x. If :None:None:`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

Raises

ValueError

If the input is not 1-dimensional, or contains elements with negative values, or if :None:None:`minlength` is negative.

TypeError

If the type of the input is float or complex.

Returns

out : ndarray of ints

The result of binning the input array. The length of :None:None:`out` is equal to np.amax(x)+1 .

Count number of occurrences of each value in array of non-negative ints.

See Also

digitize
histogram
unique

Examples

This example is valid syntax, but we were not able to check execution
>>> np.bincount(np.arange(5))
array([1, 1, 1, 1, 1])
This example is valid syntax, but we were not able to check execution
>>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
array([1, 3, 1, 1, 0, 0, 0, 1])
This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> 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])
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : None#None
type: <class 'builtin_function_or_method'>
Commit: