谐波均值(也称为“相反均值”)是几种平均值之一,尤其是勾股均值之一。通常用于需要平均费率的情况。调和平均值也是给定一组观测值的倒数的算术平均值的倒数。
例如,harmonic mean
1、4和4的计算公式如下:
可以使用来自的harmonic_mean()函数将谐波均值合并到Python3中statistics
模块。
用法: harmonic_mean([data-set])
参数:
[data-set]:是实数值的列表,元组或迭代器。
Returntype:返回给定数据集的harmonic_mean。
错误和异常:
StatisticsError当传递空的data-set时,或者data-set由负值组成时。
TypeError用于非数字类型值的数据集。
注意:仅使用列表,集合或任何序列中的正值来计算谐波均值。
代码1:
# 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 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 datsets
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.299197943900386 Harmonic Mean of data set 2 is 4.574783168721765 Harmonic Mean of data set 4 is 55/56 Harmonic Mean of data set 5 is 1.6363636363636365
代码3:示范StatisticsError
# 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:\Users\Souveek\AppData\Local\Programs\Python\Python36-32\Lib\statistics.py", line 356, in harmonic_mean T, total, count = _sum(1/x for x in _fail_neg(data, errmsg)) File "C:\Users\Souveek\AppData\Local\Programs\Python\Python36-32\Lib\statistics.py", line 148, in _sum for n, d in map(_exact_ratio, values): File "C:\Users\Souveek\AppData\Local\Programs\Python\Python36-32\Lib\statistics.py", line 356, in T, total, count = _sum(1/x for x in _fail_neg(data, errmsg)) File "C:\Users\Souveek\AppData\Local\Programs\Python\Python36-32\Lib\statistics.py", line 285, in _fail_neg raise StatisticsError(errmsg) statistics.StatisticsError:harmonic mean does not support negative values
注意:以下代码可能无法在在线IDE上运行,因为harmonic_mean()
函数是Python3.6中新引入的
应用范围:
谐波均值是金融中许多重要工具(根据统计)。加权谐波平均值是平均乘数的优选方法,例如price-earnings比(P /E),其中价格在分子内。它也用于算术平均值over-estimates为所需结果的地方的计算中。
相关用法
- Python statistics median_high()用法及代码示例
- Python statistics mean()用法及代码示例
- Python statistics median_low()用法及代码示例
- Python statistics variance()用法及代码示例
- Python statistics pvariance()用法及代码示例
- Python - statistics stdev()用法及代码示例
- Python statistics median()用法及代码示例
- Python statistics median_grouped()用法及代码示例
注:本文由纯净天空筛选整理自retr0大神的英文原创作品 Python statistics | harmonic_mean()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。