Find the most frequent element in NumPy array in Python

In this tutorial, we will learn how to find the most frequent elements in the NumPy array in Python. There are various ways to find the most frequent elements in Python. We will learn one by one. As we know that to perform a NumPy array, we have to import NumPy.
To learn, how to find the most frequent element in NumPy array, first you have to generate the numpy array.

Python program to find the most frequent element in NumPy array

Let’s import NumPy and generate a random NumPy array:

import numpy as np
x = np.random.randint(0, 10, 30)
print(x)

As you can see, I have given input to generate a random NumPy. In the output, it will generate an array between range 0 to 10 and the number of elements will be 30.

Output–

[9 8 3 8 6 0 8 0 9 5 1 2 9 3 4 4 9 4 5 8 6 6 6 6 9 4 8 6 2 0]
  • Using bincount( ).argmax( )  function — We can get the most frequent element in numpy array using bincount function.

Below is the next step of our Python program where we are using the bincount().argmax() function to get the most frequent item of our NumPy array:

print(np.bincount(x).argmax())

The bincount().argmax( ) return the element which has come for multiple times. As we can see in the above array, 6 has generated multiple times so it will return only 6 in the output.

6
  • Using counter function– Using counter function, you can get the most frequent element as well as counting of all the elements from where you can easily check the most frequent element from an array.

Note– The np.bincount( ) solution only works on numbers. If you have strings, Negative integers, collections. The counter solution will work for you. Below is our code:

from collections import Counter
b = Counter(x)
print (b.most_common())

As you can see, to use the counter function, We have to import collections. It will count all the elements and will return each element with counting.

Output:

[(6, 6), (9, 5), (8, 5), (4, 4), (0, 3), (3, 2), (5, 2), (2, 2), (1, 1)]

Here you can see, 6 has more no. of outcome therefore 6 is the most frequent element in above numpy array.
Here you can see, the element which has the largest no. of outcome has mention first in the output. So, by slicing you can get the most frequent element in NumPy array:

collections.Counter(x).most_common()[0][0]

In the above output at [0][0] place, we have 6. And we see that 6 is the most frequent element in the above NumPy array. So it will return only 6.

Output:

6

Also, read:

 

  • Using Mode Function– As you have learned in statistics, Mode is the most frequent element. So using mode function we can get the most frequent element.

Input:

import statistics 
from statistics import mode
print(mode(x))

If you calculate the mode of the above numpy array, you will get answer 6. Also, 6 is the most frequent element in NumPy array so it will return 6.

Output:

6

So we have done our task.

Leave a Reply

Your email address will not be published. Required fields are marked *