Find the most frequent value in a NumPy array

Find the most frequent value in a NumPy array

To find the most frequent value in a NumPy array, you can use the unique function of NumPy which returns unique values and their counts, then find the value with the maximum count.

Here's an example:

import numpy as np # Sample data arr = np.array([3, 2, 3, 4, 5, 2, 2, 5, 5, 5]) # Get unique values and their counts values, counts = np.unique(arr, return_counts=True) # Get the value with the maximum count most_frequent = values[np.argmax(counts)] print(most_frequent) 

In this example, the value 5 appears 4 times in the array, which is the most frequent value. The output will be 5.


More Tags

telerik-mvc menu window.location mkdir cancellationtokensource web3js renderer forward gradle-eclipse jfrog-cli

More Programming Guides

Other Guides

More Programming Examples