当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python statistics median_grouped()用法及代码示例


谈到统计函数,data-set的中位数是鲁棒集中趋势的度量,该趋势受数据中异常值的影响较小。如前所示,使用median(),median_high(),median_low()的未分组data-set的中位数起作用。

Python还提供了计算分组数据和连续数据函数的中位数的选项,这是此健壮且方便的语言的最佳组成部分。统计模块下的median_grouped()函数有助于从一组连续数据中计算中值。

假定数据被分组为宽度间隔的间隔。数组中的每个数据点都是包含真实值的间隔的中点。假设在该间隔内的真实值均匀分布,则通过在中间间隔(包含中间值的间隔)内进行插值来计算中间值:


median = L + interval * (N / 2 - CF) / FL = lower limit of the median interval
N = total number of data points
CF = number of data points below the median interval
F = number of data points in the median interval
用法: median_grouped( [data-set], interval)

Parameters:
[data-set]:列表或元组,或带有一组数字的迭代。
interval (默认为1):确定分组数据的宽度和更改的宽度。它还将更改计算出的中位数的插值。

Returntype:返回分组的连续数据的中位数,计算为50th百分百。

Exceptions: StatisticsError当iterable通过为空或list为null时引发。


代码1:

# 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:median_grouped处理各种变化的数据

# Python code to demonsrate 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.5
Grouped Median of set 2 is -6.5
Grouped Median of set 3 is 1.5


代码3:间隔工作

# Python code to demonsrate 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.5
Grouped Median for Interval set as 2 is 12.0
Grouped Median for Interval set as 5 is 10.5
Grouped Median for Interval set as 10 is 8.0


注意:观察到随着间隔值增加,中间值减小的模式。

代码4:展示StatisticsError

# 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


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



相关用法


注:本文由纯净天空筛选整理自retr0大神的英文原创作品 Python statistics | median_grouped()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。