Numpy count Frequency of value

In this post, we are going to learn how to count an element occurrence in Numpy or count Frequency of value in Numpy array with examples by using np.count_nonzero(),np.sum(),np.where(),np.bincount(),list.count().

1. Numpy count Frequency of value using count_nonzero()


The count_nonzero() returns the count of elements of the numpy array for a condition that returns True. Where True is equal to 1 or False is equal is 0. We can pass the axis in this function to count elements along a particular axis. If we do not pass the axis in count_nonzero() returns the count of elements in the whole numpy array. To count elements rows and columns wise we pass the axis as done in the below programs.

  • To count the element column-wise in the case of the multidimensional array by passing argument axis=0 in count_nonzero() function.
  • To count the element row-wise in the case of multidimensional array We pass argument axis=1 in count_nonzero() function.
import numpy as np
nparr = np.array([[1, 2, 3, 9, 5],  
              [6, 9, 8, 9, 10], 
              [11, 9, 13, 9, 15,]])

Freqcount = np.count_nonzero(nparr == 9)
print('frequence of value 9 is:',Freqcount)

Output

frequence of value 9 in NumPy array is: 5

Frequently Asked Questions

2. np.sum() to count Frequency of value in 2D array


The np. sum() function returns the sum of numpy array elements by given axis.

  • To find the occurrence of elements column-wise in the case of the multidimensional array by using argument axis=0 in sum() function.
  • To find the occurrence of elements row-wise in the case of the multidimensional array by passing argument axis=1 in sum() function.
import numpy as np
nparr = np.array([[1, 2, 3, 9, 5],  
              [6, 9, 8, 9, 10], 
              [11, 9, 13, 9, 15,]])

Freqcount = np.sum(nparr == 9)
print('frequence of value 9 is:',Freqcount)

Freqcount_colwise= np.sum(nparr == 9,axis=0)
print('frequence of value 9 colwise',Freqcount_colwise)

Freqcount_Rowwise= np.sum(nparr == 9,axis=1)
print('frequence of value 9 rowwise:',Freqcount_Rowwise)

Output

frequence of value 9 is: 5
frequence of value 9 colwise [0 2 0 3 0]
frequence of value 9 rowwise: [1 2 2]

3.Numpy count Frequency of value using np.where()


In this Python program to count frequency of value using np. where() by passing a condition and later finding the occurrence of a value in numpy array by using result[0].size.

import numpy as np
nparr = np.array([[1, 2, 3, 9, 5],  
              [6, 9, 8, 9, 10], 
              [11, 9, 13, 9, 15,]])

result = np.where(nparr == 9)
Freqcount = result[0].size
print('occurrence of value 9 in Numpy array is:',Freqcount)

Output

occurrence of value 9 in Numpy array is : 5

4. Numpy count frequency of value in each row of 2D array


To count the element row-wise in the case of multidimensional array We pass argument axis=1 in count_nonzero() function. In this python program example, we have passed axis=1.

import numpy as np
nparr = np.array([[1, 2, 3, 9, 5],  
              [6, 9, 8, 9, 10], 
              [11, 9, 13, 9, 15,]])
 
Freqcount = np.count_nonzero(nparr==9,axis=1)

print('frequence of value 9 is:',Freqcount )

Output

frequence of value 9 each rows of NumPy array: [1 2 2]

5. Numpy frequency of value in each column of 2D array


To count the element column-wise in the case of the multidimensional array by passing argument axis=0 in count_nonzero() function.

import numpy as np
nparr = np.array([[1, 2, 3, 9, 5],  
              [6, 9, 8, 9, 10], 
              [11, 9, 13, 9, 15,]])
 
count_arr = np.count_nonzero(nparr==9,axis=0)

print('frequence of value 9 each rows of NumPy array:',count_arr)

Output

frequence of value 9 each rows of NumPy array: [0 2 0 3 0]

6. Numpy frequency of value in a 1D array


In this Python program example, we have converted the numpy array to a list and used the count() method to find the occurrence of value 9.

import numpy as np
nparr = np.array([1, 2, 3, 9, 5,6, 9, 8, 9, 10])              

Freqcount = nparr.tolist().count(9)

print('frequence of value 9 in NumPy array:',Freqcount)

Output

frequence of value 9 in NumPy array: 3

7. NumPy frequency of value in a 1D array


In this Python program example, we have used the np.bincount() to count the occurrence of numpy elements. The Freqcount[9] returns the occurrence of value 9 in the numpy array. We pass the value in brackets[] to find its occurrence for example for value 2 it would be Freqcount[2]

import numpy as np
nparr = np.array([1, 2, 3, 9, 5,6, 9, 8, 9, 10])           

Freqcount = np.bincount(nparr)
print('frequence of value 9 in NumPy array:',Freqcount[9])

Output

frequence of value 9 in NumPy array: 3