諧波均值(也稱為“相反均值”)是幾種平均值之一,尤其是勾股均值之一。通常用於需要平均費率的情況。調和平均值也是給定一組觀測值的倒數的算術平均值的倒數。
例如,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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。