Calculate Mode of a distribution using Python

The mode function in python finds the most occurring value of a distribution by going through supplied data parameter which is a sequence or iterator

Statistics Updated 12 Jan 2017 1 min read

Mode of a Distribution

The most commonly occurring value in a distribution is called the modal value or simply the mode. It is possible that a distribution has no mode at all or more than one mode.

A distribution that has one most occurring value is called unimodal.

A distribution with two most occurring value has two modes and is called bimodal and one with three modes is called trimodal.

Mode is one of the measures of central tendancy.

Method Name

mode(data)

Method Overview

The Python mode() function takes data from any sequence or iterator type and returns the most occurring value in the data.

Exceptions

  • The mode function will return the modal value only if the distribution has a unique mode. If the distribution has multiple modes, python raises StatisticsError; For Example, the mode() function will report “no unique mode; found 2 equally common values” when it is supplied of a bimodal distribution.
  • If the parameter supplied to the mode function is found empty python raises a StatisticsError stating “statistics.StatisticsError: no mode for empty data”

Example Python Program to find Mode of a distribution

python
#import the python statistics module

import statistics

# Define data points

dataPoints  = [1,2,3,4,5,5,5,6,7,8]

# Find mode - the most occurring value in a distribution

mode        = statistics.mode(dataPoints)

print("Mode of the distribution is {}".format(mode))

Output of the Python Program that finds the Mode of a distribution

Output
Mode of the distribution is 5