统计学中众数的介绍

在统计学中,一组给定数据值中出现次数最多的值被称为众数。换句话说,具有较高频率或重复出现的数字或值被称为众数众数值众数中心趋势的三个测量之一,另外两个是均值和中位数。

例如 -

我们有一个集合A = {4, 5, 6, 6, 7, 8, 9}。由于数字6具有较高的频率,因此集合A的众数是6。因此,对于有限数量的观察来说,找到众数是很容易的。一组数据值可能具有一个模态值、多个模态值或根本没有众数。连续概率分布的众数通常被认为是任何值x。其概率密度函数具有最大局部值,因此任何峰值都是众数。

Python中的mode()函数

在处理统计数据和与大范围数据值集合交互时,Python成为了一种非常强大的编程语言。Python提供了statistics模块,其中包含许多用于处理大型数据集的函数,其中之一就是mode()函数。mode()函数用于返回所提供数据集中的中心数据点的强健度量。

mode()函数是Python编程语言标准statistics库中唯一适用于非数值(标称)数据的函数。

让我们看一下Python中mode函数的语法。

语法:

mode()函数的语法如下所示:

statistics.mode(data)

Python中mode()函数的参数

mode()函数的参数是data,它可以是可迭代或序列,例如列表、元组等等。

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

Python中mode()函数的返回值

mode()函数将根据参数中提供的数据计算后返回一个浮点数或非数值(标称)值,例如列表、元组等等。

让我们根据Python编程语言标准statistics库中mode()函数的示例考虑一些示例。

示例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

解释:

在上面的示例中,我们导入了statistics库并创建了一个名为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库和fractions模块。然后,我们创建了不同类型的元组,以检查mode()函数是否适用于各种数据类型。我们创建了一个正整数、浮点值、分数、负整数和字符串的元组。然后,我们使用statistics.mode()函数计算了每个数据集的众数。然后,我们将这些估算值打印给用户。

mode()函数的一些应用

mode()函数通常在金融领域中使用,以比较价格和与先前记录的值。它还有助于从价格分布集合中计算和预测未来可能的价格。mode()函数通常不单独使用,而是与统计的另外两个测量值——均值和中位数——一起使用。这三者一起作为强大的工具,可以揭示数据的许多方面。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程