How to Calculate Geometric Mean in Python (Using SciPy)

Опубликовано: 18 Март 2026
на канале: Ryan & Matt Data Science
590
19

🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automations! 📈 https://www.skool.com/data-and-ai-aut...

Need to calculate geometric mean for financial returns, growth rates, or skewed data? In this step-by-step tutorial, you'll learn how to compute it easily using Python and SciPy—with clear explanations, examples, and best practices.

Code: https://ryanandmattdatascience.com/ge...

🚀 Hire me for Data Work: https://ryanandmattdatascience.com/da...
👨‍💻 Mentorships: https://ryanandmattdatascience.com/me...
📧 Email: [email protected]
🌐 Website & Blog: https://ryanandmattdatascience.com/
🖥️ Discord:   / discord  
📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan
📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg

🍿 WATCH NEXT
Statistics for Data Science Playlist:    • Statistics for Data Science  
Python Covariance:    • How to Compute Covariance in Python for Da...  
Python Harmonic Mean:    • How to Calculate Harmonic Mean in Python (...  
Python CDF:    • Mastering CDF (Cumulative Distribution Fun...  


In this video, I walk you through how to calculate the geometric mean in Python using three different methods. We start with a manual calculation to understand the math behind it, then move to NumPy for a simplified approach, and finally use SciPy's gmean function for a one-line solution. The geometric mean is particularly useful when working with percentages, growth rates, and proportional data, which is why it differs from the arithmetic mean you learned in school.

I cover two practical examples in this tutorial. The first example uses simple whole numbers to demonstrate the core concept, while the second example tackles a more complex scenario involving percentages and negative values. This is where geometric mean really shines—when calculating average growth rates or returns where you need to account for compounding effects. I show you exactly how to convert percentages to decimals, handle negative percentages properly, and convert your results back to meaningful percentage values.

Whether you're preparing for a data science interview, working on financial analysis, or just want to expand your Python statistics toolkit, this tutorial breaks down geometric mean calculation step by step. By the end, you'll know when to use geometric mean versus arithmetic mean and how to implement it efficiently in your code.

TIMESTAMPS
00:00 Introduction to Geometric Mean
00:28 Background & Theory of Geometric Mean
01:42 Manual Calculation Example
03:02 Coding Setup & Imports
03:42 Example 1: Manual Calculation in Python
05:55 Example 2: Using NumPy
07:32 Example 3: Using SciPy
08:15 Example 4: Percentages & Negative Numbers
10:40 Converting Back to Percentages

OTHER SOCIALS:
Ryan’s LinkedIn:   / ryan-p-nolan  
Matt’s LinkedIn:   / matt-payne-ceo  
Twitter/X: https://x.com/RyanMattDS

Who is Ryan
Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Who is Matt
Matt is the founder of Width.ai, an AI and Machine Learning agency. Before starting his own company, he was a Machine Learning Engineer at Capital One.

*This is an affiliate program. We receive a small portion of the final sale at no extra cost to you.