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


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


在数据分析和统计方面,Python是一种非常流行的语言。幸运的是,Python3提供了统计模块,它带有非常有用的函数,例如mean(), median(), mode()等等

统计模块中的median()函数可用于从未排序的data-list计算中值。使用median()函数的最大优点是,在将data-list作为参数发送给median()函数之前,无需对其进行排序。

中位数是将数据样本或概率分布的上半部分与下半部分分开的值。对于数据集,可以将其视为中间值。中位数是统计和概率论中data-set属性的集中趋势的度量。中值相对于均值具有很大的优势,因为中值不会因太大或很小的值而有太大的偏差。中间值包含在提供的值的data-set中,或者与提供的数据相差不大。


对于奇数个元素集,中间值是中间值。对于偶数元素集,中值是两个中间元素的平均值。

Median can be represented by the following formula:

   {\displaystyle \mathrm {median} (a)={\frac {a_{\lfloor \#x\div 2\rfloor }+a_{\lfloor \#x\div 2+0.5\rfloor }}{2}}}  

用法:
median([data-set])

参数:
[data-set]:列表或元组或带有一组数字的可迭代

返回:返回包含数据的可迭代对象的中位数(中间值)

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

代码1:工作中

# Python code to demonstrate the   
# working of median() function. 
  
# importing statistics module 
import statistics 
  
# unsorted list of random integers 
data1 = [2, -2, 3, 6, 9, 4, 5, -1] 
  
  
# Printing median of the 
# random data-set 
print("Median of data-set is:% s "
        % (statistics.median(data1)))

输出:

Median of data-set is:3.5 


代码2:

# Python code to demonstrate the 
# working of median() on various 
# range of data-sets 
  
# importing the statistics module 
from statistics import median 
  
# Importing fractions module as fr 
from fractions import Fraction as fr 
  
# tuple of positive integer numbers 
data1 = (2, 3, 4, 5, 7, 9, 11) 
  
# tuple of floating point values 
data2 = (2.4, 5.1, 6.7, 8.9) 
  
# tuple of fractional numbers 
data3 = (fr(1, 2), fr(44, 12), 
         fr(10, 3), fr(2, 3)) 
  
# tuple of a set of  negative integers 
data4 = (-5, -1, -12, -19, -3) 
  
# tuple of set of positive  
# and negative integers 
data5 = (-1, -2, -3, -4, 4, 3, 2, 1) 
  
# Printing the median of above datsets 
print("Median of data-set 1 is % s" % (median(data1))) 
print("Median of data-set 2 is % s" % (median(data2))) 
print("Median of data-set 3 is % s" % (median(data3))) 
print("Median of data-set 4 is % s" % (median(data4))) 
print("Median of data-set 5 is % s" % (median(data5)))

输出:

Median of data-set 1 is 5
Median of data-set 2 is 5.9
Median of data-set 3 is 2
Median of data-set 4 is -5
Median of data-set 5 is 0.0


代码3:展示StatisticsError

# Python code to demonstrate 
# StatisticsError of median() 
  
# importing the statistics module 
from statistics import median 
  
# creating an empty data-set 
empty = [] 
  
# will raise StatisticsError 
print(median(empty))

输出:

Traceback (most recent call last):
  File "/home/3c98774036f97845ee9f65f6d3571e49.py", line 12, in 
    print(median(empty))
  File "/usr/lib/python3.5/statistics.py", line 353, in median
    raise StatisticsError("no median for empty data")
statistics.StatisticsError:no median for empty data


应用范围:
对于实际应用,根据可以估计相应人口值的程度比较比较分散和人口趋势的不同度量。例如,比较显示,当数据不受重尾数据分布或数据分布混合的数据污染时,样本均值比样本中位数在统计上更有效,但在其他方面效率较低,并且样本中位数为高于各种发行版。更具体地说,与minimum-variance-mean(对于大型正常样本)相比,中位数效率为64%。



相关用法


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