📜  Python统计模块中的median()函数

📅  最后修改于: 2022-05-13 01:55:03.570000             🧑  作者: Mango

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

在数据分析和统计方面, Python是一种非常流行的语言。幸运的是,Python3 提供了统计模块,它带有非常有用的函数,如 mean()、median()、mode() 等。
统计模块中的median()函数可用于从未排序的数据列表中计算中值。使用 median()函数的最大优点是数据列表在作为参数发送到 median()函数之前不需要进行排序。
中位数是将数据样本或概率分布的上半部分与下半部分分开的值。对于数据集,它可能被认为是中间值。中位数是统计和概率论中数据集属性集中趋势的度量。中位数比均值有很大的优势,即中位数不会被极大或极小的值扭曲太多。中值要么包含在所提供的值的数据集中,要么与所提供的数据相差不大。
对于奇数组元素,中值是中间值。
对于数组元素,中值是两个中间元素的平均值。

中位数可以用以下公式表示: {\displaystyle \mathrm {median} (a)={\frac {a_{\lfloor \#x\div 2\rfloor }+a_{\lfloor \#x\div 2+0.5\rfloor }}{2}}}


代码#1:工作

Python3
# 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)))


Python3
# 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 datasets
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)))


Python3
# 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))


输出 :

Median of data-set is : 3.5 


代码#2:

Python3

# 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 datasets
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

Python3

# 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%。