How to find mode in Python using NumPy

You can find the mode in Python using NumPy with the following code. If you want to learn Python then I will highly recommend you to read This Book.

How to find mode in Python using NumPy
How to find mode in Python using NumPy

Method 1: Mode using NumPy

There is no direct method in NumPy to find the mode.

import numpy as np
a = [1,2,2,2,4,5,6,6]
values,counts = np.unique(a, return_counts=True)
mode = values[np.argmax(counts)]
print(mode)
#output 2

Method 2: Mode using SciPy

From this method, you can easily find the mode.

from scipy import stats
a = [1,2,2,2,4,5,6,6]
x = stats.mode(a)
print(x)
#output ModeResult(mode=array([2]), count=array([3]))

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Top 10 Computer Vision Books with Python

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.