Open In App

Find the most frequent value in a NumPy array

Last Updated : 29 Aug, 2020
Like Article

In this article, let’s discuss how to find the most frequent value in the NumPy array. 

Steps to find the most frequency value in a NumPy array:

  • Create a NumPy array.
  • Apply bincount() method of NumPy to get the count of occurrences of each element in the array.
  • The n, apply argmax() method to get the value having a maximum number of occurrences(frequency).

Example 1:


import numpy as np
# create array
x = np.array([1,2,3,4,5,1,2,1,1,1])
print("Original array:")
print("Most frequent value in the above array:")



This code will generate a single output only, it will not work fine if the array contains more than one element having the maximum number of frequency.

Example 2: If the array has more than one element having maximum frequency


import numpy as np
x = np.array([1, 1, 1, 2, 3, 4, 2, 4, 3, 3, ])
print("Original array:")
print("Most frequent value in above array")
y = np.bincount(x)
maximum = max(y)
for i in range(len(y)):
    if y[i] == maximum:
        print(i, end=" ")


1 3

Similar Reads

How to display most frequent value in a Pandas series?
In this article, our basic task is to print the most frequent value in a series. We can find the number of occurrences of elements using the value_counts() method. From that the most frequent element can be accessed by using the mode() method. Example 1 : # importing the module import pandas as pd # creating the series series = pd.Series(['g', 'e',
1 min read
Find the k most frequent words from data set in Python
Given the data set, we can find k number of most frequent words. The solution of this problem already present as Find the k most frequent words from a file. But we can solve this problem very efficiently in Python with the help of some high performance modules. In order to do this, we'll use a high performance data type module, which is collections
2 min read
Python | Find most frequent element in a list
Given a list, find the most frequent element in it. If there are multiple elements that appear maximum number of times, print any one of them. Examples: Input : [2, 1, 2, 2, 1, 3] Output : 2 Input : ['Dog', 'Cat', 'Dog'] Output : Dog Approach #1 : Naive ApproachThis is a brute force approach in which we make use of for loop to count the frequency o
4 min read
NumPy ndarray.__abs__() | Find Absolute Value of Elements in NumPy Array
The ndarray.__abs__() method returns the absolute value of every element in the NumPy array. It is automatically invoked when we use Python's built-in method abs() on a NumPy array. Example C/C++ Code import numpy as np gfg = np.array([1.45, 2.32, 3.98, 4.41, 5.55, 6.12]) print(gfg.__abs__()) Output[ 1 2 3 4 5 6] SyntaxSyntax: ndarray.__abs__() Ret
1 min read
Kth most frequent Character in a given String
Given a string str and an integer K, the task is to find the K-th most frequent character in the string. If there are multiple characters that can account as K-th most frequent character then, print any one of them.Examples: Input: str = "GeeksforGeeks", K = 3 Output: f Explanation: K = 3, here 'e' appears 4 times & 'g', 'k', 's' appears 2 time
6 min read
Python program for most frequent word in Strings List
Given Strings List, write a Python program to get word with most number of occurrences. Example: Input : test_list = ["gfg is best for geeks", "geeks love gfg", "gfg is best"] Output : gfg Explanation : gfg occurs 3 times, most in strings in total. Input : test_list = ["geeks love gfg", "geeks are best"] Output : geeks Explanation : geeks occurs 2
6 min read
NumPy ndarray.transpose() Method | Find Transpose of the NumPy Array
The ndarray.transpose() function returns a view of the array with axes transposed. For a 1-D array, this has no effect, as a transposed vector is simply the same vector.For a 2-D array, this is a standard matrix transpose.For an n-D array, if axes are given, their order indicates how the axes are permuted. If axes are not provided and arr.shape = (
2 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
Python | Find top K frequent elements from a list of tuples
Given a list of tuples with word as first element and its frequency as second element, the task is to find top k frequent element. Below are some ways to above achieve the above task. Method #1: Using defaultdict C/C++ Code # Python code to find top 'k' frequent element # Importing import collections from operator import itemgetter from itertools i
5 min read
Python Program for Least frequent element in an array
Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1} Output : 3 3 appears minimum number of times in given array. Input : arr[] = {10, 20, 30} Output : 10 or 20 or 30 Method 1:A simple solution is to run two
3 min read
Practice Tags :