调和平均数(也称为逆平均数)是几种平均数之一,尤其是毕达哥拉斯平均数之一。通常用于需要平均速率的情况。调和平均值也是一组给定观测值的倒数算术平均值的倒数。
null
例如,1、4和4的调和平均值可以计算为:
调和平均值可以通过使用 调和平均 来自统计模块的函数。
语法: 调和平均值([数据集]) 参数: [数据集] :它是实数的列表、元组或迭代器。 返回类型: 返回给定数据集的调和平均值。 错误和例外: 统计误差 当传递空数据集时,或者如果数据集包含负值。 打字错误 用于非数值类型值的数据集。
注: 调和平均值仅使用列表、集合或任何序列中的正值计算。 代码#1:
Python3
# Python3 code to demonstrate the # working of harmonic_mean() function # Import statistics module import statistics # list of positive real valued numbers data = [ 1 , 3 , 5 , 7 , 9 ] # using harmonic mean function to calculate # the harmonic mean of the given data-set print ( "Harmonic Mean is % s " % (statistics.harmonic_mean(data))) |
输出:
Harmonic Mean is 2.797513321492007
代码#2:
Python3
# Python3 program to demonstrate harmonic_mean() # function from the statistics module # Importing the statistics module from statistics import harmonic_mean # 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 )) # dictionary of a set of values # Only the keys are taken in # consideration by harmonic_mean() data4 = { 1 : "one" , 2 : "two" , 3 : "three" } # Printing the harmonic mean of above datasets print ( "Harmonic Mean of data set 1 is % s" % (harmonic_mean(data1))) print ( "Harmonic Mean of data set 2 is % s" % (harmonic_mean(data2))) print ( "Harmonic Mean of data set 3 is % s" % (harmonic_mean(data3))) print ( "Harmonic Mean of data set 4 is % s" % (harmonic_mean(data4))) |
输出:
Harmonic Mean of data set 1 is 4.299197943900386Harmonic Mean of data set 2 is 4.574783168721765Harmonic Mean of data set 4 is 55/56Harmonic Mean of data set 5 is 1.6363636363636365
代码#3: 统计误差的证明
Python3
# Python code to demonstrate StatisticsError # while using harmonic_mean() # importing statistics module import statistics # data-set of numbers containing # a negative number dat1 = [ 1 , - 1 ] # Statistics Error is raised when the # data-set passed as parameter is # empty or contain a negative value print (statistics.harmonic_mean(dat1)) |
输出:
Traceback (most recent call last): File "C:/Users/Souveek/PycharmProjects/Test.py", line 12, in print(statistics.harmonic_mean((1, -1))) File "C:UsersSouveekAppDataLocalProgramsPythonPython36-32Libstatistics.py", line 356, in harmonic_mean T, total, count = _sum(1/x for x in _fail_neg(data, errmsg)) File "C:UsersSouveekAppDataLocalProgramsPythonPython36-32Libstatistics.py", line 148, in _sum for n, d in map(_exact_ratio, values): File "C:UsersSouveekAppDataLocalProgramsPythonPython36-32Libstatistics.py", line 356, in T, total, count = _sum(1/x for x in _fail_neg(data, errmsg)) File "C:UsersSouveekAppDataLocalProgramsPythonPython36-32Libstatistics.py", line 285, in _fail_neg raise StatisticsError(errmsg)statistics.StatisticsError: harmonic mean does not support negative values
注: 以下代码可能不会在在线IDE上运行,因为在Python3中新引入了harmonic_mean()函数。6. 应用: 调和平均是金融学中的许多重要工具之一。加权调和平均是对倍数(如市盈率(P/E))进行平均的较好方法,其中价格是分子。在算术平均值高估所需结果的地方,它也用于计算。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END