Как найти моду списка в python

Mode of a data set is/are the member(s) that occur(s) most frequently in the set. If there are two members that appear most often with same number of times, then the data has two modes. This is called bimodal.

If there are more than 2 modes, then the data would be called multimodal. If all the members in the data set appear the same number of times, then the data set has no mode at all.

Following function modes() can work to find mode(s) in a given list of data:

import numpy as np; import pandas as pd

def modes(arr):
    df = pd.DataFrame(arr, columns=['Values'])
    dat = pd.crosstab(df['Values'], columns=['Freq'])
    if len(np.unique((dat['Freq']))) > 1:
        mode = list(dat.index[np.array(dat['Freq'] == max(dat['Freq']))])
        return mode
    else:
        print("There is NO mode in the data set")

Output:

# For a list of numbers in x as
In [1]: x = [2, 3, 4, 5, 7, 9, 8, 12, 2, 1, 1, 1, 3, 3, 2, 6, 12, 3, 7, 8, 9, 7, 12, 10, 10, 11, 12, 2]
In [2]: modes(x)
Out[2]: [2, 3, 12]
# For a list of repeated numbers in y as
In [3]: y = [2, 2, 3, 3, 4, 4, 10, 10]
In [4]: modes(y)
Out[4]: There is NO mode in the data set
# For a list of strings/characters in z as
In [5]: z = ['a', 'b', 'b', 'b', 'e', 'e', 'e', 'd', 'g', 'g', 'c', 'g', 'g', 'a', 'a', 'c', 'a']
In [6]: modes(z)
Out[6]: ['a', 'g']

If we do not want to import numpy or pandas to call any function from these packages, then to get this same output, modes() function can be written as:

def modes(arr):
    cnt = []
    for i in arr:
        cnt.append(arr.count(i))
    uniq_cnt = []
    for i in cnt:
        if i not in uniq_cnt:
            uniq_cnt.append(i)
    if len(uniq_cnt) > 1:
        m = []
        for i in list(range(len(cnt))):
            if cnt[i] == max(uniq_cnt):
                m.append(arr[i])
        mode = []
        for i in m:
            if i not in mode:
                mode.append(i)
        return mode
    else:
        print("There is NO mode in the data set")

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.

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.

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 [key for key in counts.keys() if counts[key] == max(counts.values())]

# use the function on a list of values
mode([2,2,4,5,6,2,3,5])

Output:

[2]

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])

Output:

[2, 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])

Output:

2

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])

Output:

---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-20-83fc446343a0> in <module>
      1 # calculate the mode
----> 2 statistics.mode([2,2,4,5,6,1,3,5])

~anaconda3envsdsplibstatistics.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])

Output:

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])

Output:

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.

  • Piyush Raj

    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

Введение Когда мы пытаемся описать и обобщить выборку данных, мы, вероятно, начинаем с нахождения среднего [https://en.wikipedia.org/wiki/Mean] (или среднего), медианы [https: // en .wikipedia.org / wiki / Median] и режим [https://en.wikipedia.org/wiki/Mode_(statistics)] данных. Это центральная тенденция [https://en.wikipedia.org/wiki/Central_tendency] меры и часто первый взгляд на набор данных. В этом руководстве мы узнаем, как найти или вычислить среднее значение, медиану,

Вступление

Когда мы пытаемся описать и обобщить выборку данных, мы, вероятно,
начинаем с нахождения среднего
(или среднего), медианы и
режима данных. Это
основные меры
тенденций,
которые
часто являются нашим первым взглядом на набор данных.

В этом руководстве мы узнаем, как найти или вычислить среднее значение,
медиану и режим в Python. Сначала мы закодируем функцию Python для
каждой меры, а затем воспользуемся
statistics
Python для выполнения той же задачи.

Обладая этими знаниями, мы сможем быстро взглянуть на наши наборы данных
и получить представление об общей тенденции данных.

Оглавление

  • Вычисление среднего значения выборки
    • Расчет среднего с помощью
      Python
    • Использование Python mean ()
  • Нахождение медианы выборки
    • Поиск медианы с помощью Python
    • Использование медианы Python ()
  • Нахождение моды образца
    • Поиск режима с помощью Python
    • Использование режима Python ()

Расчет среднего значения выборки

Если у нас есть выборка числовых значений, то ее среднее или среднее

  • это общая сумма значений (или наблюдений), деленная на количество
    значений.

Допустим, у нас есть образец [4, 8, 6, 5, 3, 2, 8, 9, 2, 5] . Мы можем
вычислить его среднее значение, выполнив операцию:

(4 + 8 + 6 + 5 + 3 + 2 + 8 + 9 + 2 + 5) / 10 = 5,2

Среднее арифметическое — это общее описание наших данных. Предположим,
вы купили 10 фунтов помидоров. Если пересчитать дома помидоры, получится
25 помидоров. В этом случае вы можете сказать, что средний вес помидора
составляет 0,4 фунта. Это было бы хорошее описание ваших помидоров.

Среднее также может быть плохим описанием выборки данных. Допустим, вы
анализируете группу собак. Если вы возьмете совокупный вес всех собак и
разделите его на количество собак, то это, вероятно, будет плохим
описанием веса отдельной собаки, поскольку разные породы собак могут
иметь очень разные размеры и вес.

Насколько хорошо или плохо среднее значение описывает выборку, зависит
от того, насколько разбросаны данные. В случае помидоров, они почти
одинакового веса, и среднее значение является хорошим их описанием. В
случае с собаками нет актуальных собак. Они могут варьироваться от
крошечного чихуахуа до гигантского немецкого мастифа. Итак, среднее само
по себе в данном случае не очень хорошее описание.

Теперь пора приступить к делу и узнать, как вычислить среднее значение с
помощью Python.

Расчет среднего с помощью Python

Чтобы вычислить среднее значение выборки числовых данных, мы будем
использовать две встроенные функции Python. Один для вычисления общей
суммы значений, а другой для вычисления длины выборки.

Первая функция — это
sum() . Эта
встроенная функция принимает итерацию числовых значений и возвращает их
общую сумму.

Вторая функция —
len() . Эта
встроенная функция возвращает длину объекта. len() может принимать в
качестве аргумента последовательности (строка, байты, кортеж, список или
диапазон) или коллекции (словарь, набор или замороженный набор).

Вот как мы можем вычислить среднее значение:

 >>> def my_mean(sample): 
 ... return sum(sample) / len(sample) 
 ... 
 
 >>> my_mean([4, 8, 6, 5, 3, 2, 8, 9, 2, 5]) 
 5.2 

Сначала мы суммируем значения в sample используя sum() . Затем мы
делим эту сумму на длину sample , которая является результирующим
значением len(sample) .

Использование Python mean ()

Поскольку вычисление среднего — это обычная операция, Python включает
эту функцию в модуль statistics Он предоставляет некоторые функции для
расчета базовой статистики по наборам данных. Функция
statistics.mean()
берет образец числовых данных (любых итерируемых) и возвращает их
среднее значение.

Вот как работает функция mean() Python:

 >>> import statistics 
 
 >>> statistics.mean([4, 8, 6, 5, 3, 2, 8, 9, 2, 5]) 
 5.2 

Нам просто нужно
импортировать statistics
а затем вызвать mean() с нашим образцом в качестве аргумента. Это
вернет среднее значение выборки. Это быстрый способ найти среднее
значение с помощью Python.

Нахождение медианы выборки

Медиана выборки числовых данных — это значение, которое находится
посередине при сортировке данных. Данные могут быть отсортированы по
возрастанию или убыванию, медиана остается прежней.

Чтобы найти медиану, нам необходимо:

  1. Отсортировать образец
  2. Найдите значение в середине отсортированного образца

При нахождении числа в центре отсортированной выборки мы можем
столкнуться с двумя типами ситуаций:

  1. Если в выборке есть нечетное количество наблюдений , то среднее
    значение в отсортированной выборке — это медиана.
  2. Если в выборке есть четное количество наблюдений , нам нужно
    вычислить среднее из двух средних значений в отсортированной
    выборке.

Если у нас есть выборка [3, 5, 1, 4, 2] и мы хотим найти ее медиану,
то сначала мы сортируем выборку по [1, 2, 3, 4, 5] . Медиана будет
равна 3 поскольку это значение посередине.

С другой стороны, если у нас есть выборка [1, 2, 3, 4, 5, 6] , то ее
медиана будет (3 + 4) / 2 = 3.5 .

Давайте посмотрим, как мы можем использовать Python для вычисления
медианы.

Поиск медианы с помощью Python

Чтобы найти медиану, нам сначала нужно отсортировать значения в нашей
выборке . Этого можно
добиться с помощью встроенной функции
sorted()
sorted() принимает итерацию и возвращает отсортированный list
содержащий те же значения, что и исходная итерация.

Второй шаг — найти значение, которое находится в середине
отсортированной выборки. Чтобы найти это значение в выборке с нечетным
количеством наблюдений, мы можем разделить количество наблюдений на 2.
Результатом будет индекс значения в середине отсортированной выборки.

Поскольку оператор деления ( / ) возвращает число с плавающей запятой,
нам нужно использовать оператор деления этажа ( //
),
чтобы получить целое число. Итак, мы можем использовать его как индекс в
операции индексации ( [] ).

Если в выборке есть четное количество наблюдений, нам нужно найти два
средних значения. Скажем, у нас есть образец [1, 2, 3, 4, 5, 6] . Если
мы разделим его длину ( 6 ) на 2 с помощью деления пола, то получим
3 . Это индекс нашего верхнего среднего значения ( 4 ). Чтобы найти
индекс нашего нижнего среднего значения ( 3 ), мы можем уменьшить
индекс верхнего среднего значения на 1 .

Давайте объединим все это в функцию, которая вычисляет медиану выборки.
Вот возможная реализация:

 >>> def my_median(sample): 
 ... n = len(sample) 
 ... index = n // 2 
 ... # Sample with an odd number of observations 
 ... if n % 2: 
 ... return sorted(sample)[index] 
 ... # Sample with an even number of observations 
 ... return sum(sorted(sample)[index - 1:index + 1]) / 2 
 ... 
 
 >>> my_median([3, 5, 1, 4, 2]) 
 3 
 
 >>> my_median([3, 5, 1, 4, 2, 6]) 
 3.5 

Эта функция берет образец числовых значений и возвращает их медиану.
Сначала мы находим длину образца n . Затем мы вычисляем индекс
среднего значения (или верхнего среднего значения) путем деления n на
2 .

Оператор if проверяет, есть ли в имеющейся выборке нечетное количество
наблюдений. Если да, то медиана — это значение index .

Окончательный return выполняется, если в выборке есть четное
количество наблюдений. В этом случае мы находим медиану, вычисляя
среднее из двух средних значений.

Обратите внимание, что операция
нарезки
[index - 1:index + 1] получает два значения. Значение в index - 1 и
значение в index поскольку операции нарезки исключают значение в
конечном индексе ( index + 1 ).

Использование медианы Python ()

Функция Python statistics.median() берет выборку данных и возвращает
ее медиану. Вот как работает метод:

 >>> import statistics 
 
 >>> statistics.median([3, 5, 1, 4, 2]) 
 3 
 
 >>> statistics.median([3, 5, 1, 4, 2, 6]) 
 3.5 

Обратите внимание, что median() автоматически обрабатывает вычисление
медианы для выборок с нечетным или четным числом наблюдений.

Поиск режима образца

Режим — это наиболее частое наблюдение (или наблюдения) в выборке.
Если у нас есть образец [4, 1, 2, 2, 3, 5] , то его режим равен 2
потому что 2 появляется в образце два раза, тогда как другие элементы
появляются только один раз.

Режим не обязательно должен быть уникальным. Некоторые образцы имеют
более одного режима. Скажем, у нас есть образец [4, 1, 2, 2, 3, 5, 4]
. В этом примере есть два режима — 2 и 4 потому что эти значения
появляются чаще и оба появляются одинаковое количество раз.

Этот режим обычно используется для категориальных данных.
Распространенными категориальными типами данных являются:

  • логическое значение — может принимать только два значения,
    например true или false , male или female
  • номинальный — может принимать более двух значений, например,
    American - European - Asian - African
  • порядковый — может принимать более двух значений, но значения
    имеют логический порядок, например, few - some - many

Когда мы анализируем набор категориальных данных, мы можем использовать
этот режим, чтобы узнать, какая категория является наиболее
распространенной в наших данных.

Мы можем найти образцы, у которых нет режима. Если все наблюдения
уникальны (нет повторяющихся наблюдений), то в вашей выборке не будет
режима.

Теперь, когда мы знаем основы режима, давайте посмотрим, как его найти с
помощью Python.

Поиск режима с помощью Python

Чтобы найти режим с помощью Python, мы начнем с подсчета количества
вхождений каждого значения в рассматриваемом примере. Затем мы получим
значения с большим количеством вхождений.

Поскольку подсчет объектов — обычная операция, Python предоставляет
класс
collections.Counter
Этот класс специально разработан для подсчета предметов.

Класс Counter предоставляет метод, определенный как
.most_common([n]) . Этот метод возвращает list кортежей из двух
элементов с n более общими элементами и их соответствующими
счетчиками. Если n опущено или None , то .most_common() возвращает
все элементы.

Давайте воспользуемся Counter и .most_common() чтобы закодировать
функцию, которая берет образец данных и возвращает свой режим.

Вот возможная реализация:

 >>> from collections import Counter 
 
 >>> def my_mode(sample): 
 ... c = Counter(sample) 
 ... return [k for k, v in c.items() if v == c.most_common(1)[0][1]] 
 ... 
 
 >>> my_mode(["male", "male", "female", "male"]) 
 ['male'] 
 
 >>> my_mode(["few", "few", "many", "some", "many"]) 
 ['few', 'many'] 
 
 >>> my_mode([4, 1, 2, 2, 3, 5]) 
 [2] 
 
 >>> my_mode([4, 1, 2, 2, 3, 5, 4]) 
 [4, 2] 

Сначала мы подсчитываем наблюдения в sample с помощью объекта
Counter c ). Затем мы используем составление
списка, чтобы создать list
содержащий наблюдения, которые встречаются в выборке одинаковое
количество раз.

Поскольку .most_common(1) возвращает list с одним tuple формы
(observation, count) , нам нужно получить наблюдение с индексом 0 в
list а затем элемент с индексом 1 во вложенном tuple . Это можно
сделать с помощью выражения c.most_common(1)[0][1] . Это значение
является первым режимом нашего образца.

Обратите внимание, что условие понимания сравнивает счетчик каждого
наблюдения ( v ) со счетчиком наиболее распространенного наблюдения (
c.most_common(1)[0][1] ). Это позволит нам получить несколько
наблюдений ( k ) с одним и тем же подсчетом в случае многомодовой
выборки.

Использование режима Python ()

Python statistics.mode() принимает некоторые data и возвращает свой
(первый) режим. Посмотрим, как это можно использовать:

 >>> import statistics 
 
 >>> statistics.mode([4, 1, 2, 2, 3, 5]) 
 2 
 
 >>> statistics.mode([4, 1, 2, 2, 3, 5, 4]) 
 4 
 
 >>> st.mode(["few", "few", "many", "some", "many"]) 
 'few' 

В одномодовом примере функция Python mode() возвращает наиболее
распространенное значение 2 . Однако в следующих двух примерах он
вернул 4 и few . В этих образцах были другие элементы, встречающиеся
такое же количество раз, но они не были включены.

Начиная с Python
3.8 мы также
можем использовать statistics.multimode() который принимает итерацию и
возвращает list режимов.

Вот пример использования multimode() :

 >>> import statistics 
 
 >>> statistics.multimode([4, 1, 2, 2, 3, 5, 4]) 
 [4, 2] 
 
 >>> statistics.multimode(["few", "few", "many", "some", "many"]) 
 ['few', 'many'] 
 
 >>> st.multimode([4, 1, 2, 2, 3, 5]) 
 [2] 

Примечание . Функция всегда возвращает list , даже если вы
передаете одномодовый образец.

Заключение

Среднее (или среднее), медиана и мода обычно являются нашим первым
взглядом на выборку данных, когда мы пытаемся понять центральную
тенденцию данных.

В этом руководстве мы узнали, как найти или вычислить среднее значение,
медиану и режим с помощью Python. Сначала мы пошагово рассмотрели, как
создавать наши собственные функции для их вычисления, а затем как
использовать statistics Python как быстрый способ найти эти
показатели.

Calculate Mode in Python (4 Examples)

In this article, I’ll explain how to find the mode in the Python programming language.

The page is structured as follows:

You’re here for the answer, so let’s get straight to the Python code:

Example 1: Mode of List Object

In this example, I’ll demonstrate how to find the mode in a list object in the Python programming language.

Let’s first create an example list:

my_list = ['x', 'x', 'y', 'x', 'z', 'z']                              # Create example list
print(my_list)                                                        # Print example list
# ['x', 'x', 'y', 'x', 'z', 'z']

In the next step, we have to import the statistics module:

import statistics                                                     # Load statistics

Now, we can apply the mode function provided by the statistics module to calculate the mode of our example list:

print(statistics.mode(my_list))                                       # Get mode of list
# x

As you can see, the most common value in our list is the character x.

Example 2: Mode of One Particular Column in pandas DataFrame

The following Python programming syntax explains how to get the mode of a specific column in a pandas DataFrame.

First, we have to import the pandas library:

import pandas as pd                                                   # Load pandas library

Next, we have to create an exemplifying pandas DataFrame:

data = pd.DataFrame({'x1':[5, 2, 7, 3, 4, 4, 2, 3, 2, 1, 2, 5],       # Create pandas DataFrame
                     'x2':['y', 'x', 'x', 'z', 'x', 'y', 'y', 'x', 'z', 'x', 'z', 'x'],
                     'group':['A', 'C', 'B', 'B', 'A', 'C', 'A', 'A', 'C', 'B', 'B', 'A']})
print(data)                                                           # Print pandas DataFrame

table 1 DataFrame calculate mode python programming language

By running the previous syntax, we have created Table 1, i.e. a new pandas DataFrame that contains three columns.

Finally, we can apply the mode function to a certain column of our data set (i.e. x1) as shown below:

print(data['x1'].mode())                                              # Get mode of one column
# 0    2
# dtype: int64

The previous output shows that the mode of the variable x1 is 2.

Example 3: Mode of All Columns in pandas DataFrame

The following Python programming syntax shows how to return the most common value in each variable of our pandas DataFrame.

Consider the Python syntax below:

print(data.mode())                                                    # Get mode of all columns
#    x1 x2 group
# 0   2  x     A

As you can see, the mode of the column x1 is 2, the mode of the column x2 is x, and the mode of the column group is A.

In this example, I’ll explain how to GroupBy a pandas DataFrame and select the most common value.

For this task, we have to use the groupby, agg, and value_counts functions as shown in the following Python code:

print(data.groupby('group').agg(lambda x:x.value_counts().index[0]))  # Get mode by group
#        x1 x2
# group       
# A       5  x
# B       1  z
# C       2  y

The mode values for each column and group are shown in the output above.

Video, Further Resources & Summary

Would you like to learn more about the calculation of the mode in a list and a pandas DataFrame column? Then you may have a look at the following video that I have published on my YouTube channel. In the video, I explain the content of this article.

Furthermore, you could have a look at the related tutorials on this homepage:

  • Calculate Mode by Group in Python
  • mode() & multimode() Functions of statistics Module
  • Mode of NumPy Array in Python
  • Mean of Columns & Rows of pandas DataFrame
  • Calculate Median in Python
  • Summary Statistics of pandas DataFrame in Python
  • Introduction to the pandas Library in Python
  • Python Programming Examples

You have learned in this tutorial how to calculate the mode in a list or a pandas DataFrame column in Python programming. Tell me about it in the comments section below, if you have any further questions.

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. 

Python3

import statistics

set1 =[1, 2, 3, 3, 4, 4, 4, 5, 5, 6]

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. 

Python3

from statistics import mode

from fractions import Fraction as fr

data1 = (2, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 7)

data2 = (2.4, 1.3, 1.3, 1.3, 2.4, 4.6)

data3 = (fr(1, 2), fr(1, 2), fr(10, 3), fr(2, 3))

data4 = (-1, -2, -2, -2, -7, -7, -9)

data5 = ("red", "blue", "black", "blue", "black", "black", "brown")

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 

Python3

import statistics

data1 =[1, 1, 1, -1, -1, -1]

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 :
23 Aug, 2021

Like Article

Save Article

Понравилась статья? Поделить с друзьями:
  • Опущение щек в 30 лет как исправить
  • Wordpad как составить таблицу
  • Мясо сухое при жарке как исправить
  • Как можно составить предложение с словосочетанием
  • Как найти косинус зная две стороны треугольника