首页 > 编程知识 正文

Python 中的众数

时间:2023-11-25 15:30:11 阅读:309232 作者:FUJE

统计众数介绍

在统计学中,在所提供的一组数据值中更经常出现的值被称为众数。换句话说,具有高频率或重复出现的数字或值被称为众数或众数值。众数是中心趋势的三个措施之一。另外两个指标分别是平均值和中位数。

例如-

我们有一套A = { 4,5,6,6,7,8,9} 。由于数字 6 的频率较高,因此A 组的众数为 6 。因此,对于有限数量的观察,很容易找到众数。一组数据值可能有一个模态值或多个模态值,或者根本没有模态。连续概率分布的一种众数常被称为任意值 x,它的概率密度函数有一个最大局部值,所以任意一个峰值都是一种众数。

Python 中的 mode()函数

Python 在处理统计数据和处理一组大范围的数据值时,成为了一种非常强大的编程语言。Python 为 统计模块提供了许多进程相当大数据集的功能,而众数()功能就是其中之一。众数()函数用于返回给定数据集范围内中心数据点的稳健度量。

众数()函数是 Python 编程语言的标准统计库中唯一可以应用于非数值(标称)数据的函数。

让我们看看 Python 中众数函数的语法。

语法:

众数()功能的语法如下所示:


statistics.mode(data)

Python 中 mode()函数的参数

众数()功能的参数是数据。它可以是一个迭代或序列——例如,列表、元组等等。

注意:如果数据参数为空,mode()函数将引发 StatisticsError。

Python 中 mode()函数的返回值

一旦在迭代器(例如,列表、元组等等)中计算了所提供数据的众数,众数()函数将根据参数中所提供的数据返回一个浮点数或非数值(标称)值。

让我们考虑一些基于 Python 编程语言的标准统计库的众数()函数的例子。

例 1:找到下面给出的数据集的众数:


# importing the statistics library
import statistics

# creating the data set
my_set = [10, 20, 30, 30, 40, 40, 40, 50, 50, 60]

# estimating the mode of the given set
my_mode = statistics.mode( my_set)

# printing the estimated mode to the users  
print("Mode of given set of data values is", my_mode)

输出:

Mode of given set of data values is 40

说明:

在上例中,我们导入了统计库,并创建了一个集合作为 my_set 。然后,我们使用 statistics.mode() 函数估计给定集合的众数,并将其值打印给用户。结果,该组中具有最高频率的值被成功打印。

示例 2:演示 mode()函数在不同种类的数据类型上的工作。


# importing the statistics library
import statistics
# importing the fractions module
from fractions import Fraction as fr

# creating the tuple of positive integer numbers
data_1 = (20, 30, 30, 40, 50, 50, 50, 60, 70, 70)

# creating the tuple of floating point values
data_2 = (1.2, 2.3, 2.3, 3.4, 4.5, 4.5, 4.5, 5.6, 5.6, 7.8)

# creating the tuple of fractional numbers
data_3 = (fr(1,3), fr(1,5), fr(1,5), fr(2,3), fr(3,4), fr(8,9))

# creating the tuple of negative integer numbers
data_4 = (-9, -8, -7, -7, -7, -6, -5, -5, -4, -2)

# creating the tuple of strings
data_5 = ("apple", "mango", "mango", "mango", "banana", "guava", "guava")

# estimating the mode of the given datasets
mode_1 = statistics.mode( data_1)
mode_2 = statistics.mode( data_2)
mode_3 = statistics.mode( data_3)
mode_4 = statistics.mode( data_4)
mode_5 = statistics.mode( data_5)

# printing the estimated modes to the users  
print("1. Mode of First Data set is", mode_1)
print("2. Mode of Second Data set is", mode_2)
print("3. Mode of Third Data set is", mode_3)
print("4. Mode of Forth Data set is", mode_4)
print("5. Mode of Fifth Data set is", mode_5)

输出:

1. Mode of First Data set is 50
2. Mode of Second Data set is 4.5
3. Mode of Third Data set is 1/5
4. Mode of Forth Data set is -7
5. Mode of Fifth Data set is mango

说明:

在上例中,我们导入了统计库和分数模块。然后,我们创建了一个不同范围的元组来检查众数()函数是否适用于各种数据类型。我们已经创建了一个由正整数、浮点值、小数、负整数和字符串组成的元组。然后我们使用 statistics.mode() 函数来计算每个数据集的众数。然后,我们将这些估计值打印给用户。

mode()函数的一些应用

众数()功能是一个统计功能,通常用于金融行业,以便将价格和价值与以前的记录进行比较。它还有助于从价格分布集合中计算和预测未来可能的价格。众数()功能不单独使用;然而,除此之外还有另外两种统计方法,即平均值和中位数。这三者共同作为一个强大的工具来揭示数据的许多方面。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。