Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python statistics.mode() Function



The Pythonstatistics.mode() function returns the single most common data point from the discrete or nominal data. This function calculates the central tendency from the numerical dataset.

In this function if there are multiple modes with the same frequency, then this function returns the first one in the dataset.

Mode assumes a discrete dataset and returns a single value.

Syntax

Following is the basic syntax for thestatistics.mode() function −

statistics.mode(data)

Parameters

Here, the data values can be used as any sequence, list or iterator.

Return Value

This function returns the most common first element from the dataset.

Example 1

In the below example we are calculating the mode (central tendency) of the given data usingstatistics.mode() function.

import statisticsx = statistics.mode([2, 4, 4, 4, 6, 7, 7, 8])print(x)

Output

This produces the following result −

4

Example 2

We are calculating mode for the non-numeric dataset usingstatistics.mode() function.

import statisticsx = statistics.mode(['blue', 'red', 'blue', 'yellow', 'blue', 'red'])print(x)

Output

The result produced is as follows −

blue

Example 3

Here, we are calculating negative numbers in the dataset usingstatistics.mode() function.

import statisticsx = statistics.mode([-1, 2, -1, 3, -1, -2, -1])print(x)

Output

The output obtained is as follows −

-1
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp