The bincount()
method of the NumPy module is used to find the frequency of each element in a NumPy array of positive integers. The element’s index in the frequency array or bin is stored as the element’s count. Because each bin value represents an instance of its index, we can adjust the bin size accordingly. This method is beneficial for counting vast amounts of data or records.
The length of the resulting array will always be 1 + maximum element in the array
.
The method throws an error if the input array has negative or floating point elements.
numpy.bincount(x, weights=None, minlength=0)
x
: This is a positive dimension array.weights
: The weights array has to be the same dimension as x.
minlength
: This is the minimum number of bins for the output array.This method returns the frequency array or bin.
import numpy as nparr = np.array([4, 4, 2, 1, 6, 8, 3, 3, 4, 15, 23])freq_bin = np.bincount(arr)print("Input array - ", arr)print("Frequency bin/array - ", freq_bin)print("Length of Frequency bin/array - ", len(freq_bin))
numpy
module.numpy.bin()
method.1 + 23 (maximum element in the array)
.Free Resources