Finding mode in python

Find Mode of List in Python

In this tutorial, we will look at how to calculate the mode of a list in Python with the help of some exmaples.

What is mode?

Mode in a list of five values

Mode is a descriptive statistic that is used as a measure of central tendency of a distribution. It is equal to value that occurs the most frequently. Note that it’s possible for a set of values to have more than one mode. Mode is also used to impute missing value in categorical variables.

To calculate the mode of a list of values –

  1. Count the frequency of each value in the list.
  2. The value with the highest frequency is the mode.
Читайте также:  Php session close handler

For example, calculate the mode of the following values –

a bunch of numbers with 2 present three times and 5 present two times and 3, 4 and 6 present one time each.

If we count how many times each value occurs in the list, you can see that 2 occurs three times, 5 occurs two times and 3, 4, and 6 occur one time each. From this we can say that the mode of these numbers is 2.

Let’s look at another example.

a bunch of numbers with 2 and 5 present two times and 1, 3, 4, and 6 present one time each.

2 and 5 occur two times and 1, 3, 4, and 6 occur once. Here, both 2 and 5 are the modes as they both have the highest frequency of occurrence. A distribution with two modes is called a bimodal distribution.

Mode of Python List

To compute the mode of a list of values in Python, you can write your own custom function or use methods available in other libraries such as scipy , statistics , etc. Let’s look at these methods with the help of some examples.

1. From scratch implementation of mode in Python

We already know the logic to compute the mode. Let’s now implement that logic in a custom Python function.

def mode(ls): # dictionary to keep count of each value counts = <> # iterate through the list for item in ls: if item in counts: counts[item] += 1 else: counts[item] = 1 # get the keys with the max counts return Finding mode in python == max(counts.values())] # use the function on a list of values mode([2,2,4,5,6,2,3,5])

Here, you can see that the custom function gives the correct mode for the list of values passed. Note that the function returns a list of all the modes instead of a scaler value.

Let’s now pass a list of values that has two modes.

# two values with max frequency mode([2,2,4,5,6,1,3,5])

You can see that it returns both the modes as a list. We can modify the function to return a scaler value, for example, the smallest mode or the largest mode depending upon the requirement.

Note that the above implementation may not be the most optimized version. (For instance, you can use Counter from the collections module to count frequency of values in a list, etc.)

2. Using statistics library

You can also use the statistics standard library in Python to get the mode of a list of values. Pass the list as an argument to the statistics.mode() function.

import statistics # calculate the mode statistics.mode([2,2,4,5,6,2,3,5])

We get the scaler value 2 as the mode which is correct.

This method gives a StatisticsError if there are more than one mode present in the data. For example –

# calculate the mode statistics.mode([2,2,4,5,6,1,3,5])
--------------------------------------------------------------------------- StatisticsError Traceback (most recent call last) in 1 # calculate the mode ----> 2 statistics.mode([2,2,4,5,6,1,3,5]) ~\anaconda3\envs\dsp\lib\statistics.py in mode(data) 505 elif table: 506 raise StatisticsError( --> 507 'no unique mode; found %d equally common values' % len(table) 508 ) 509 else: StatisticsError: no unique mode; found 2 equally common values

3. Using scipy library

You can also use the mode() function available in the scipy.stats module to calculate the mode in a list. For example –

from scipy.stats import mode # calculate the mode mode([2,2,4,5,6,2,3,5])
ModeResult(mode=array([2]), count=array([3]))

We get the correct result.

Note that this method gives the smallest mode if there are multiple modes present in the data.

from scipy.stats import mode # calculate the mode mode([2,2,4,5,6,1,3,5])
ModeResult(mode=array([2]), count=array([2]))

The data actually has two modes, 2 and 5, with both occurring two times but we get 2 as the result because it’s the smallest of the modes.

You can use methods similar to the ones described in this tutorial to calculate the median of a list in Python.

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Источник

How to Find Mode in Python using Numpy array with Examples

Install Numpy in Pycharm and Linux

Numpy is the best python package for doing complex mathematical calculations. It has many functions for array creation and manipulation. In this entire tutorial, you will know how to find a mode of a NumPy array in python using various examples.

What is a Mode?

A mode is generally used to find the most occurrences of the data points in a dataset. Datasets can have one mode, two-mode, or no mode at all.

Examples to Find mode for Numpy array

In this section, you will know the various examples of how to find a mode of an array. However you can use your own numeric datasets, but for simplicity, I am finding mode in a sample NumPy array. Make sure you must have properly installed NumPy in your system.

Example 1: Find mode on 1 D Numpy array

In this example, I will find mode on a single-dimensional NumPy array. First I will create a Single dimension NumPy array and then import the mode() function from scipy. Execute the below lines of code to calculate the mode of 1d array.

Mode of a Single Dimensional Numpy array

Here you can see the occurrence of 5 is more than any other elements. That’s why this array has mode 5.

Example 2: Finding mode on 2 D Numpy array

In the next example, I will create two dimensional NumPy array and use the stats.mode() method on that array. There are two ways you can find mode on a 2D Numpy array. One is finding mode for each row-wise and the other is finding mode on entire array. Let’s explore each of them.

Finding mode rowwise

To find mode rowise you have to set the axis as zero value. It will find the array of modes for each column. Run the below lines of code and see the output.

Mode of a Multi Dimensional Numpy array

Output

Finding Overall Mode

In the same way, you can find mode for the entire array. To do so you have to set the axis value as None. Just execute the below lines of code and see the output.

Finding overall mode of a Multi Dimensional Numpy array

Output

Conclusion

Mode is very useful for finding the measure of the central tendency. You can use it for finding the standard deviation of the dataset. These are the basic example for finding a mode of the array in python. I hope you have liked this tutorial. If you have any questions then you can contact us for more help. In the meantime, you can subscribe to us for quick updates directly in your inbox.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Источник

Find Mode of a List in Python

Find Mode of a List in Python

  1. Use the max() Function and a Key to Find the Mode of a List in Python
  2. Use the Counter Class in the Collections Package to Find the Mode of a List in Python
  3. Use the mode() Function From the statistics Module to Find the Mode of a List in Python
  4. Use the multimode() Function From the Statistics Module to Find a List of Modes in Python

A list is one of the most powerful data structures used in Python to preserve the sequence of data and iterate over it. It can contain different data types like numbers, strings, and more.

In a given data set, a mode is a value or element that appears with the highest frequency. There can be one mode, more than one mode, or no mode at all. There will be no mode if all the elements are unique.

In this tutorial, we will discuss how to find the mode of a list in Python.

Use the max() Function and a Key to Find the Mode of a List in Python

The max() function can return the maximum value of the given data set. The key argument with the count() method compares and returns the number of times each element is present in the data set.
Therefore, the function max(set(list_name), key = list_name.count) will return the element that occurs the maximum times in the given list that is the required mode of the list.

A = [10, 30, 50, 10, 50, 80, 50] print("Mode of List A is % s" % (max(set(A), key = A.count))) B = ['Hi', 10, 50, 'Hi', 100, 10, 'Hi'] print("Mode of List B is % s" % (max(set(B), key = B.count))) 
Mode of List A is 50 Mode of List B is Hi 

This function will return the smallest mode when there are multiple modes present in the data set.

C = [10, 30, 'Hello', 30, 10, 'Hello', 30, 10] print("Mode of List C is % s" % (max(set(C), key = C.count))) 

Use the Counter Class in the Collections Package to Find the Mode of a List in Python

The Counter class in the collections package is used to count the number of occurrences of each element present in the given data set.

The .most_common() method of the Counter class returns a list containing two-items tuples with each unique element and its frequency.

from collections import Counter A = [10, 10, 30, 10, 50, 30, 60] Elements_with_frequency = Counter(A) print(Elements_with_frequency.most_common()) 

The Counter(list_name).most_common(1)[0][0] function will return the required mode of the list. When there are multiple modes present in the list, it will return the smallest mode.

from collections import Counter A = [10, 10, 30, 10, 50, 30, 60] print("Mode of List A is % s" % (Counter(A).most_common(1)[0][0])) 

Use the mode() Function From the statistics Module to Find the Mode of a List in Python

The mode() function in the python statistics module takes some dataset as a parameter and returns its mode value.

from statistics import mode A = [10, 20, 20, 30, 30 ,30] print("Mode of List A is % s" % (mode(A))) B = ['Yes', 'Yes', 'Yes', 'No', 'No'] print("Mode of List B is % s" % (mode(B))) 
Mode of List A is 30 Mode of List B is Yes 

This function will raise the StatisticsError when the data set is empty or when more than one mode is present. However, in the newer versions of Python, the smallest element will be considered the mode when there are multiple modes of a sequence.

Use the multimode() Function From the Statistics Module to Find a List of Modes in Python

The multimode() function in the statistics module takes some data set as a parameter and returns a list of modes. We can use this function when more than one modal value is present in a given data set.

from statistics import multimode A = [10, 20, 20, 30, 30 ,30, 20] print("Mode of List A is % s" % (multimode(A))) B = ['Yes', 'Yes', 'Yes', 'No', 'No', 'No', 'Maybe', 'Maybe'] print("Mode of List B is % s" % (multimode(B))) 
Mode of List A is [20, 30] Mode of List B is ['Yes', 'No'] 

Related Article — Python List

Источник

Оцените статью