Python统计模块中的middian_grouped()函数

来到 统计函数 ,数据集的中值是稳健中心趋势的度量,受数据中异常值的影响较小。如前所述,使用 中位数() , 中位_高() , 中位数低() 功能。 Python还提供了计算分组和连续数据函数中值的选项,这是这种健壮且方便的语言的最好部分。 中位数 统计模块下的函数,帮助从一组连续数据中计算中值。 假设数据按宽度间隔分组。数组中的每个数据点都是包含真值的区间的中点。假设该区间内的真值均匀分布,则通过中值区间(包含中值的区间)内的插值计算中值:

null
median = L + interval * (N / 2 - CF) / FL = lower limit of the median intervalN = total number of data pointsCF = number of data points below the median intervalF = number of data points in the median interval

语法: 中位数分组([数据集],区间) 参数: [数据集]: 列表、元组或具有一组数值的iterable。 间隔 (默认情况下为1) : 确定分组数据的宽度并进行更改。它还将改变计算中值的插值。 返回类型: 返回分组连续数据的中位数,计算为50 th 百分位。 例外情况: 统计误差 当iterable passed为空或list为空时引发。

代码#1:

Python3

# Python3 code to demonstrate median_grouped()
# importing median_grouped from
# the statistics module
from statistics import median_grouped
# creating an simple data-set
data1 = [ 15 , 20 , 25 , 30 , 35 ]
# printing median_grouped for the set
print ( "Grouped Median of the median is %s"
% (median_grouped(data1)))


输出:

Grouped Median of the median is 25.0

代码#2: 根据一系列不同的数据对中位数进行分组

Python3

# Python code to demonstrate the
# working of median_grouped()
# importing statistics module
from statistics import median_grouped
# tuple of a set of positive integers
set1 = [ 2 , 5 , 3 , 4 , 8 , 9 ]
# tuple of a set of negative integers
set2 = [ - 6 , - 2 , - 9 , - 12 ]
# tuple of a set of positive
# and negative integers
set3 = [ 2 , 4 , 8 , 9 , - 2 , - 3 , - 5 , - 6 ]
# Printing grouped median for
# the given set of data
print ( "Grouped Median of set 1 is % s" % (median_grouped(set1)))
print ( "Grouped Median of set 2 is % s" % (median_grouped(set2)))
print ( "Grouped Median of set 3 is % s" % (median_grouped(set3)))


输出:

Grouped Median of set 1 is 4.5Grouped Median of set 2 is -6.5Grouped Median of set 3 is 1.5

代码#3: 工作的 间隔

Python3

# Python code to demonstrate the working of
# interval in median_grouped() function
# importing statistics module
from statistics import median_grouped
# creating a tuple of simple data
set1 = ( 10 , 12 , 13 , 12 , 13 , 15 )
# Printing median_grouped()
# keeping default interval at 1
print ( "Grouped Median for Interval set as "
"(default) 1 is % s" % (median_grouped(set1)))
# For interval value of 2
print ( "Grouped Median for Interval set as "
"2 is % s" % (median_grouped(set1, interval = 2 )))
# Now for interval value of 5
print ( "Grouped Median for Interval set as "
"5 is % s" % (median_grouped(set1, interval = 5 )))


输出:

Grouped Median for Interval set as (default) 1 is 12.5Grouped Median for Interval set as 2 is 12.0Grouped Median for Interval set as 5 is 10.5Grouped Median for Interval set as 10 is 8.0

注: 观察一种模式,即随着间隔值的增加,中值减小。 代码#4: 显示统计错误

Python3

# Python code to demonstrate StatisticsError
# importing the statistics module
import statistics
# creating an empty dataset
list1 = []
# Will raise StatisticsError
print (statistics.median_grouped(list1))


输出:

Traceback (most recent call last):  File "/home/0990a4a3f5206c7cd12a596cf82a1587.py", line 10, in     print(statistics.median_grouped(list1))  File "/usr/lib/python3.5/statistics.py", line 431, in median_grouped    raise StatisticsError("no median for empty data")statistics.StatisticsError: no median for empty data

应用: 分组中值与中值具有相同的应用。它通常用于涉及大量数据(如银行和金融)的计算中。它是统计的重要组成部分,是数据计算中最强大的工具。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享