The NumPy bincount() function counts how many times each non-negative integer occurs in a one-dimensional array.
The index of each value in the output array represents the integer being counted. The value stored at that index represents how many times that integer appears in the input.
For example:
import numpy as np
my_array=np.array(
[0, 2, 2, 6, 5]
)
result=np.bincount(my_array)
print(result)
Output
[1 0 2 0 0 1 1]
The result tells us:
0 occurs 1 time.1 occurs 0 times.2 occurs 2 times.3 occurs 0 times.4 occurs 0 times.5 occurs 1 time.6 occurs 1 time.np.bincount(
x,
weights=None,
minlength=0
)
| Parameter | Description |
|---|---|
x |
A one-dimensional array of non-negative integer values. |
weights |
Optional array containing a weight for each corresponding value in x.
|
minlength |
Optional minimum length of the returned array. |
Without weights, the returned array contains integer counts.
When weights is supplied, the returned array contains the sum of weights for each integer value rather than simple occurrence counts.
import numpy as np
my_array=np.array(
[0, 2, 2, 6, 5]
)
result=np.bincount(my_array)
print(result)
Output
[1 0 2 0 0 1 1]
The largest value in the input is 6, so the returned array contains positions for integers from 0 through 6.
By default, the output must contain one position for every integer from 0 through the largest value in the input.
Therefore, for a non-empty input array:
output length = maximum input value + 1
For example:
import numpy as np
my_array=np.array(
[2, 4, 4, 5]
)
result=np.bincount(my_array)
print(result)
print(len(result))
Output
[0 0 1 0 2 1]
6
The largest input value is 5, so the output has 6 elements.
If an integer between 0 and the maximum value does not occur in the input, its count is 0.
import numpy as np
my_array=np.array(
[1, 1, 3, 5]
)
result=np.bincount(my_array)
print(result)
Output
[0 2 0 1 0 1]
Here:
0 is missing, so its count is 0.1 occurs twice.2 is missing.3 occurs once.4 is missing.5 occurs once.
The optional minlength argument sets the minimum length of the returned array.
import numpy as np
my_array=np.array(
[0, 1, 1, 2]
)
result=np.bincount(
my_array,
minlength=6
)
print(result)
Output
[1 2 1 0 0 0]
The highest input value is only 2, but minlength=6 ensures that the result contains at least six elements.
Extra positions are filled with zeros.
minlength does not limit the result to that size.
If the input requires a longer result, NumPy returns the longer array.
my_array=np.array(
[0, 5]
)
result=np.bincount(
my_array,
minlength=3
)
print(result)
Output
[1 0 0 0 0 1]
Because the value 5 is present, the result still needs six positions.
The weights argument changes the meaning of the result.
Instead of counting each occurrence as 1, NumPy adds the corresponding weight for each value.
import numpy as np
values=np.array(
[0, 2, 2, 6, 5]
)
weights=np.array(
[2, 2, 1, 3, 4]
)
result=np.bincount(
values,
weights=weights
)
print(result)
Output
[2. 0. 3. 0. 0. 4. 3.]
Consider the two arrays:
values = [0, 2, 2, 6, 5]
weights = [2, 2, 1, 3, 4]
For integer value 2, there are two corresponding weights:
2 + 1 = 3
Therefore, position 2 in the output contains 3.0.
The other weighted values are:
0 receives weight 2;5 receives weight 4;6 receives weight 3.This produces:
[2. 0. 3. 0. 0. 4. 3.]
import numpy as np
values=np.array(
[0, 3, 3, 5]
)
weights=np.array(
[2, 1, 4, 6]
)
result=np.bincount(
values,
weights=weights
)
print(result)
Output
[2. 0. 0. 5. 0. 6.]
The value 3 occurs twice. Its corresponding weights are 1 and 4, so:
1 + 4 = 5
An empty integer array returns an empty result when no minimum length is requested.
import numpy as np
my_array=np.array(
[],
dtype=int
)
result=np.bincount(my_array)
print(result)
Output
[]
result=np.bincount(
my_array,
minlength=4
)
print(result)
Output
[0 0 0 0]
The input values must be non-negative integers.
import numpy as np
my_array=np.array(
[1, -1, 2]
)
np.bincount(my_array)
This raises an error because -1 cannot be used as a bin index.
bincount() is designed for integer values.
my_array=np.array(
[1.0, 2.0, 2.0]
)
np.bincount(my_array)
Use an integer array when the values represent integer categories or bin numbers.
A two-dimensional array cannot be passed directly to bincount().
my_array=np.array(
[
[1, 2],
[2, 3]
]
)
np.bincount(my_array)
If appropriate for the task, flatten the array first:
result=np.bincount(
my_array.ravel()
)
print(result)
Output
[0 1 2 1]
Each value in the input array must have a corresponding weight.
Therefore, these arrays should have the same number of elements:
values = [0, 2, 2]
weights = [5, 10, 20]
numpy.bincount() counts occurrences of non-negative integer values.0.minlength can request a minimum output size.weights changes the operation from simple counting to summing weights for each integer value.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.