AppDividend
Latest Code Tutorials

np.bincount: What is Numpy bincount() Method in Python

What is the use of the bincount() method from numpy? In an array of positive(+ve) integers, the numpy.bincount() method counts the occurrence of each element.

np.bincount

The np.bincount() is a numpy library method used to obtain the frequency of each element provided inside a numpy array. The numpy bincount() method takes arr_name, weights, and minlength as arguments and returns the ndarray of integers.

To count the number of occurrences of each value in the array of non-negative ints, use the numpy.bincount() function.

All the elements inside the array must be of integer data type, and also, they should be non-negative; otherwise, a TypeError is raised. The count of the element is stored as its index in the frequency array/bin.

Hence, we can conclude that each bin value corresponds to the occurrence of its index. Thus we can set the bin size accordingly. The size of the bin will always be equal to the largest number in the given array + 1.

For Ex: If the array is [1,6,6,4,2,8,5], then the size of the bin will be 9 because 8 is the largest element in the array. If the weight array is provided, then the input array is weighted by it.

Syntax

numpy.bincount(arr_name, weights = None, minlength=0)

Parameters

The bincount() function takes up to three primary parameters:

  1. arr_name: This is the input array in which frequency elements are to be counted.
  2. weights: an additional array of the same shape as the input array.
  3. minlength: It specifies the minimum number of bins in the output array.

Return Value

The bincount() function returns a ndarray of ints.

The output array consists of several occurrences of the index value of the bin in the input array.

The size of the output array is +1 greater than the maximum element present inside the input array.

Programming Example

Program to show the working of numpy.bincount()

# importing the numpy module
import numpy as np

# Input array of non-negative integers
inp_arr = np.array([1, 2, 5, 6, 2, 1, 8, 8, 8])

x = np.bincount(inp_arr)
print("Output of bincount is: ", x)

# printing the size of output bin
print("\nSize of output bin: ", len(x))

Output

Output of bincount is:  [0 2 2 0 0 1 1 0 3]

Size of output bin:  9

Explanation

In the program bincount1.py, we have taken a numpy array named inp_arr and have stored multiple non-negative elements inside the array whose frequency is to be counted.

Then, we have passed the complete array as a parameter inside the np.bincount() method and stored its return value inside a variable named x.

Afterward, we have printed the output by displaying the elements of the newly obtained bincount array using x; what we can conclude is, in np.bincount(), each bin value corresponds to the occurrence of its index.

See the image shown below for a better understanding.

np bincount in Python

Program to understand the working of np.bincount() method when an additional weight array is given as a parameter.

See the following code.

# importing the numpy module
import numpy as np

# Input array of non-negative integers
inp_arr = np.array([1, 2, 5, 6, 2, 1, 8, 8, 8])

# Weighted Array
weights = np.array([1, 1, 1, 0.5, 1, 1, 1, 1, 2])
output_bin = np.bincount(inp_arr, weights)
print("Output of bincount is: ", output_bin)

# printing the size of output bin
print("\nSize of output bin: ", len(output_bin))

Output

Output of bincount is:  [0 2 2 0 0 1 1 0 3]

Size of output bin:  9

Explanation

In this code example named bincount2.py.The weight parameter can be used to perform element-wise addition. Therefore, the element corresponding to the index will be added element-wise. Therefore the elements in the different index are given as:

Index 0 corresponding element =0, Index 1 corresponding element = 0, at Index 1 corresponding elements = 1+1= 2, at Index 2 corresponding elements = 1+1= 2, at Index 3 and Index 4 corresponding element =0, at Index 5 corresponding element = 1, at Index 6 corresponding element = 0.5, at.

Index 7 corresponding element = 0, at Index 8, corresponding element = 1 + 1 + 2 = 4. 

See the image shown below for a better understanding.

Numpy bincount() Example

That’s it for this tutorial.

See also

Convert Numpy float to Int Array

Numpy random randn()

Numpy isposinf()

Numpy isneginf()

Numpy isnan()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.