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 are desired. The harmonic mean is also the reciprocal of the arithmetic mean of the reciprocals of a 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.