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]

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

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

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.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top