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


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


中位数通常被称为中心位置的可靠度量,并且较少受数据中异常值的影响。

统计Python中的模块允许使用三个选项来处理数据集中的中间/中间元素,分别是median(), median_low() and median_high()

高中位数始终是数据集的成员。当数据点的数量为奇数时,将返回中间值。当它是偶数时,将返回两个中间值中较大的一个。让我们看看如何median_high()函数起作用。


用法: median_high( [data - set] )

Parameters:
[data-set]:采用列表或一组可交互的数值数据。

Returntype:返回数值数据的高中位数(始终在实际的data-set中)。

Exceptions: StatisticsError当data-set为空时引发。

代码1:工作中

# Python code to demonstrate working of 
# median_high() on a data-set 
  
# importing the statistics module 
import statistics 
  
# simple list of a set of integers 
set1 = [1, 3, 2, 8, 5, 4] 
  
# Print high median of the data-set 
print("High median of the data-set is %s " 
        % (statistics.median_high(set1)))

输出:

High median of the data-set is 4 


代码2:median_high()和median()的工作来证明它们之间的区别。

# Working of median_high() and median() to 
# demonstrate the difference between them. 
  
# importing the statistics module 
import statistics 
  
# simple list of a set of integers 
set1 = [1, 3, 3, 4, 5, 7] 
  
# Print median of the data-set 
  
# Median value may or may not 
# lie within the data-set 
print("Median of the set is %s" 
      % (statistics.median(set1))) 
  
# Print high median of the data-set 
print("High Median of the set is %s " 
      % (statistics.median_high(set1)))

输出:

Median of the set is 3.5
High Median of the set is 4 


代码3:median_high()在不同范围的data-values上的工作。

# Python code to demonstrate the 
# working of median_high() 
  
# importing statistics module 
from statistics import median_high 
  
# 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 a set of floating point values 
data2 = (2.4, 5.1, 6.7, 8.9) 
  
# tuple of a set 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) 
  
# Print the high_median() of the given data-sets 
print("High Median of data-set 1 is %s" % (median_high(data1))) 
print("High Median of data-set 2 is %s" % (median_high(data2))) 
print("High Median of data-set 3 is %s" % (median_high(data3))) 
print("High Median of data-set 4 is %s" % (median_high(data4))) 
print("High Median of data-set 5 is %s" % (median_high(data5)))

输出:

High Median of data-set 1 is 5
High Median of data-set 2 is 6.7
High Median of data-set 3 is 10/3
High Median of data-set 4 is -5
High Median of data-set 5 is 1


代码4:演示StatisticsError

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

输出:

Traceback (most recent call last):
  File "/home/fc2eae1616bfaa0987b261d9d40f4602.py", line 10, in 
    print(median_high(empty))
  File "/usr/lib/python3.5/statistics.py", line 398, in median_high
    raise StatisticsError("no median for empty data")
statistics.StatisticsError:no median for empty data


应用范围:
仅当数据是离散数据时才使用高中位数,并且首选中位数为实际中位数而不是插值数据集。



相关用法


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