Harmonic Mean (also known as Contrary mean) is one of several kinds of average and in particular one of the Pythagorean means. Usually used in situations when average rates is desired. The harmonic mean is also the reciprocal of arithmetic mean of the reciprocals of given set of observations.
harmonic mean of 1, 4 and 4 can be calculated as :
Harmonic mean can be incorporated in Python3 by using
harmonic_mean() function from the
Syntax : harmonic_mean([data-set])
[data-set]: which is a list or tuple or iterator of real valued numbers.
Returntype : Returns the harmonic_mean of the given set of data.
Errors and Exceptions :
StatisticsError when a empty data-set is passed or if data-set consist of negative values.
TypeError for datset of non-numeric type values.
Note : Harmonic mean is calculated only using positive values in list, set or any sequence.
Code #1 :
Harmonic Mean is 2.797513321492007
Code #2 :
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
Code #3 : Demonstrating
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
Note : Following codes may not run on online IDEs, since the
harmonic_mean() function is newly introduced in Python3.6
Harmonic Mean is one of the many important tools in finance (under statistics). The weighted harmonic mean is the preferable method for averaging multiples, such as thee price–earnings ratio (P/E), in which price is in the numerator. It is also used in calculations in places where the arithmetic mean over-estimates the required result.
- Python statistics | median_low()
- Python statistics | median_grouped()
- Python statistics | variance()
- Python statistics | mean() function
- Python statistics | median()
- Python statistics | median_high()
- Python statistics | stdev()
- Python statistics | pvariance()
- Python statistics | mode function
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.