🧠 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...
Want to calculate the harmonic mean of your data in Python? In this quick and practical tutorial, you'll learn what the harmonic mean is, when to use it, and how to compute it efficiently using SciPy and Python.
Code: https://ryanandmattdatascience.com/ha...
🚀 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 Quantiles: • Understanding Quantiles in Python: A Step-...
Python Geometric Mean: • How to Calculate Geometric Mean in Python ...
Python Cumulative Distribution Function: • Mastering CDF (Cumulative Distribution Fun...
In this video, I walk you through calculating the harmonic mean in Python, starting with a manual calculation and then showing you how to simplify it down to just one line of code using two powerful libraries. The harmonic mean is a type of average that's particularly useful when working with rates, ratios, speeds, or percentages where the average of values isn't linear. Unlike the arithmetic mean, it gives more weight to smaller values and is always less than or equal to it.
I begin by explaining the background of the harmonic mean and when you should use it, including why it cannot be calculated with datasets containing zero values. Then I demonstrate a practical running example where we calculate the average speed of marathon runners with different paces, manually stepping through finding the length, calculating reciprocals, and summing them up.
After that, I show you how to achieve the same result using the statistics library and scipy's hmean function, reducing everything to a single line. I also include a real-world investment returns example, showing how to convert percentages to decimals, calculate the harmonic mean, and convert back to percentages. By the end of this tutorial, you'll understand exactly when to use the harmonic mean and how to implement it efficiently in Python whether you need the manual approach or prefer quick library solutions.
TIMESTAMPS
00:00 Introduction to Harmonic Mean
00:22 Background & Theory of Harmonic Mean
01:05 Running Example Walkthrough
02:18 Setting Up Python Code
03:00 Manual Calculation - Finding Length
03:40 Manual Calculation - Reciprocals & Sum
04:33 Manual Calculation - Final Result
05:09 Example 2: Using Statistics Library
06:00 Example 3: Using SciPy Library
06:29 Example 4: Investment Returns
07:31 Converting Percentages to Decimals
08:15 Calculating Investment Return Mean
09:06 Key Takeaways & Summary
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.