The mode of a set of data values is the value that appears most often. It is the value at which the data is most likely to be sampled. A mode of a continuous probability distribution is often considered to be any value x at which its probability density function has a local maximum value, so any peak is a mode.
Python is very robust when it comes to statistics and working with a set of a large range of values. The statistics module has a very large number of functions to work with very large data-sets. The mode() function is one of such methods. This function returns the robust measure of a central data point in a given range of data-sets.
Given data-set is : [1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 7, 7, 8] The mode of the given data-set is 4 Logic: 4 is the most occurring/ most common element from the given list
Syntax : mode([data-set]) Parameters : [data-set] which is a tuple, list or a iterator of real valued numbers as well as Strings. Return type : Returns the most-common data point from discrete or nominal data. Errors and Exceptions : Raises StatisticsError when data set is empty.
Code #1 : This piece will demonstrate mode() function through a simple example.
Mode of given data set is 4
Code #2 : In this code we will be demonstrating the mode() function a various range of data-sets.
Mode of data set 1 is 5 Mode of data set 2 is 1.3 Mode of data set 3 is 1/2 Mode of data set 4 is -2 Mode of data set 5 is black
Code #3 : In this piece of code will demonstrate when StatisticsError is raised
Traceback (most recent call last): File "/home/38fbe95fe09d5f65aaa038e37aac20fa.py", line 20, in print(statistics.mode(data1)) File "/usr/lib/python3.5/statistics.py", line 474, in mode raise StatisticsError('no mode for empty data') from None statistics.StatisticsError: no mode for empty data
NOTE: In newer versions of Python, like Python 3.8, the actual mathematical concept will be applied when there are multiple modes for a sequence, where, the smallest element is considered as a mode.
Say, for the above code, the frequencies of -1 and 1 are the same, however, -1 will be the mode, because of its smaller value.
Applications : The mode() is a statistics function and mostly used in Financial Sectors to compare values/prices with past details, calculate/predict probable future prices from a price distribution set. mean() is not used separately but along with two other pillars of statistics mean and median creates a very powerful tool that can be used to reveal any aspect of your data.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- median_grouped() function in Python statistics module
- median_low() function in Python statistics module
- median_high() function in Python statistics module
- median() function in Python statistics module
- stdev() method in Python statistics module
- scipy stats.mode() function | Python
- Wand mode() function - Python
- turtle.mode() function in Python
- Python statistics | mean() function
- Python - Power-Function Distribution in Statistics
- fmean() function in Python statistics
- Python | Play a video in reverse mode using OpenCV
- Finding Mean, Median, Mode in Python without libraries
- Python | Pandas dataframe.mode()
- Python | Pandas Series.mode()
- Python statistics | pvariance()
- Use Pandas to Calculate Statistics in Python
- Python statistics | variance()
- Python - Moyal Distribution in Statistics
- Python - Maxwell Distribution in Statistics
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.