 # mode() function in Python statistics module

## mode() function in Python statistics module

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.

Example :

```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.

 `# Python code to demonstrate the` `# use of mode() function` `# mode() function a sub-set of the statistics module` `# We need to import the statistics module before doing any work` `import` `statistics` `# declaring a simple data-set consisting of real valued` `# positive integers.` `set1 ``=``[``1``, ``2``, ``3``, ``3``, ``4``, ``4``, ``4``, ``5``, ``5``, ``6``]` `# In the given data-set` `# Count of 1 is 1` `# Count of 2 is 1` `# Count of 3 is 2` `# Count of 4 is 3` `# Count of 5 is 2` `# Count of 6 is 1` `# We can infer that 4 has the highest population distribution` `# So mode of set1 is 4` `# Printing out mode of given data-set` `print``(``"Mode of given data set is % s"` `%` `(statistics.mode(set1)))`

Output

`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.

 `# Python code to demonstrate the` `# working of mode() function` `# on a various range of data types` `# Importing the statistics module` `from` `statistics ``import` `mode` `# Importing fractions module as fr` `# Enables to calculate harmonic_mean of a` `# set in Fraction` `from` `fractions ``import` `Fraction as fr` `# tuple of positive integer numbers` `data1 ``=` `(``2``, ``3``, ``3``, ``4``, ``5``, ``5``, ``5``, ``5``, ``6``, ``6``, ``6``, ``7``)` `# tuple of a set of floating point values` `data2 ``=` `(``2.4``, ``1.3``, ``1.3``, ``1.3``, ``2.4``, ``4.6``)` `# tuple of a set of fractional numbers` `data3 ``=` `(fr(``1``, ``2``), fr(``1``, ``2``), fr(``10``, ``3``), fr(``2``, ``3``))` `# tuple of a set of negative integers` `data4 ``=` `(``-``1``, ``-``2``, ``-``2``, ``-``2``, ``-``7``, ``-``7``, ``-``9``)` `# tuple of strings` `data5 ``=` `(``"red"``, ``"blue"``, ``"black"``, ``"blue"``, ``"black"``, ``"black"``, ``"brown"``)` `# Printing out the mode of the above data-sets` `print``(``"Mode of data set 1 is % s"` `%` `(mode(data1)))` `print``(``"Mode of data set 2 is % s"` `%` `(mode(data2)))` `print``(``"Mode of data set 3 is % s"` `%` `(mode(data3)))` `print``(``"Mode of data set 4 is % s"` `%` `(mode(data4)))` `print``(``"Mode of data set 5 is % s"` `%` `(mode(data5)))`

Output

```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

 `# Python code to demonstrate the ` `# statistics error in mode function` `  ` `'''` `StatisticsError is raised while using mode when there are` `two equal modes present in a data set and when the data set` `is empty or null` `'''` `  ` `# importing statistics module` `import` `statistics` `  ` `# creating a data set consisting of two equal data-sets` `data1 ``=``[``1``, ``1``, ``1``, ``-``1``, ``-``1``, ``-``1``]` `  ` `# In the above data set` `# Count of 1 is 3` `# Count of -1 is also 3` `# StatisticsError will be raised` `  ` `print``(statistics.mode(data1))`

Output

```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.

Last Updated on November 13, 2021 by admin

## Python – os.system() methodPython – os.system() method

Python | os.system() method The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.system() method execute the command

## Ways to sort list of dictionaries by values in Python – Using lambda functionWays to sort list of dictionaries by values in Python – Using lambda function

Ways to sort list of dictionaries by values in Python – Using lambda function Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g.

## Linear RegressionLinear Regression

Machine Learning – Linear Regression Linear Regression is a machine learning algorithm based on supervised learning. It performs a regression task. Regression models a target prediction value based on independent variables. It is mostly used for finding out the relationship between variables and

## Python – Pandas Series.to_frame()Python – Pandas Series.to_frame()

Pandas Series.to_frame()   Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing

## Pandas Groupby – Sort within groupsPandas Groupby – Sort within groups

Pandas Groupby – Sort within groups Pandas Groupby is used in situations where we want to split data and set into groups so that we can do various operations on those groups like – Aggregation of data, Transformation through some

## How to get file extension in Python?How to get file extension in Python?

In Python, we can extract the file extension using either of the two different approaches discussed below – Method 1: Using Python os module splitext() function This function splits the file path string into file name and file extension into

## What does the if __name__ == “__main__”: do?What does the if __name__ == “__main__”: do?

What does the if __name__ == “__main__”: do? Before executing code, Python interpreter reads source file and define few special variables/global variables. If the python interpreter is running that module (the source file) as the main program, it sets the

## Convert Text file to JSON in PythonConvert Text file to JSON in Python

Convert Text file to JSON in Python In Python, converting a text file to JSON (JavaScript Object Notation) format is a common task for transmitting data between web applications and servers. Luckily, the process can be made easy with the