![]() |
VOOZH | about |
The mode of a set of data values is the value that appears most frequently. In statistics, it’s often used to understand the most common or popular value within a set. 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.
A dataset can have:
Python’s built-in statistics module provides a convenient mode() function to compute this directly, for example:
Mode: 4
Explanation: mode() method returns 4 from the list because it is the first value with the highest frequency.
statistics.mode(data)
Parameters:
Return Type: It returns the most frequent value from the dataset.
Raises: StatisticsError if the dataset is empty.
In Python versions prior to 3.8, statistics.mode() raises a StatisticsError if there is more than one most common value (no unique mode). From Python 3.8 onwards, statistics.mode() returns the first mode encountered in case of multiple values with equal highest frequency. For reliable multi-mode analysis, use statistics.multimode().
In this example, we will use mode() method over iterables containing various range of datatypes:
Mode 1: 5 Mode 2: 1.3 Mode 3: 1/2 Mode 4: -2 Mode 5: black
Explanation:
Mode method throws a StatisticsError if appliead over an empty iterable.
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 5, in <module>
print(statistics.mode(a))
~~~~~~~~~~~~~~~^^^
File "/usr/local/lib/python3.13/statistics.py", line 785, in mode
raise StatisticsError('no mode for empty data') from None
statistics.StatisticsError: no mode for empty data
In this example, mode() is used to determine the most popular subject among students:
Most liked subject is: Math
Explanation:
Also read: probability density function, local maximum value,statistics module