Geometric Mean in Python (Example)
This tutorial illustrates how to compute the geometric mean in Python programming.
The post consists of this information:
Let’s do this!
Example Data & Add-On Libraries
I’ll use the data below as a basis for this Python tutorial:
my_list = [4, 3, 8, 7, 8, 4, 4, 1, 5] # Create example list print(my_list) # Print example list # [4, 3, 8, 7, 8, 4, 4, 1, 5] |
my_list = [4, 3, 8, 7, 8, 4, 4, 1, 5] # Create example list print(my_list) # Print example list # [4, 3, 8, 7, 8, 4, 4, 1, 5]
The previous output of the Python console shows that our example data is a list object containing nine integers.
Example: Get Geometric Mean Using gmean() Function of SciPy Library
In this example, I’ll demonstrate how to calculate the geometric mean of a list in Python.
For this task, we first have to import the gmean function of the SciPy library:
from scipy.stats import gmean # Import gmean function of SciPy library |
from scipy.stats import gmean # Import gmean function of SciPy library
In the next step, we can apply the gmean function to our example list to get the geometric mean:
print(gmean(my_list)) # Apply gmean function # 4.226198741306655 |
print(gmean(my_list)) # Apply gmean function # 4.226198741306655
As you can see, the geometric mean of the numbers in our list is 4.22.
Video & Further Resources
Have a look at the following video which I have published on my YouTube channel. In the video, I’m explaining the Python programming code of this tutorial in the Python programming language:
The YouTube video will be added soon.
Furthermore, you might want to read the related articles on this website.
- Calculate Mean in Python
- Harmonic Mean in Python
- Mean of Columns & Rows of pandas DataFrame
- Calculate Mean by Group in Python
- mean() Function of NumPy Library
- mean() Function of statistics Module in Python
- Introduction to Python
You have learned in this tutorial how to find the geometric mean in Python. Don’t hesitate to let me know in the comments below, in case you have any additional questions.