Python statistics harmonic_mean()

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

The statistics.harmonic_mean function in Python's statistics module calculates the harmonic mean of a given set of numbers. The harmonic mean is the reciprocal of the arithmetic mean of the reciprocals of the data. It is useful for calculating the average of rates or ratios.

Table of Contents

  1. Introduction
  2. statistics.harmonic_mean Function Syntax
  3. Examples
    • Basic Usage
    • Harmonic Mean of a List of Numbers
    • Harmonic Mean of a List of Fractions
    • Handling Different Types of Numeric Data
  4. Real-World Use Case
  5. Conclusion

Introduction

The statistics.harmonic_mean function is part of the statistics module, which provides functions for mathematical statistics of numeric data. The harmonic mean is particularly useful when dealing with averages of rates or ratios, such as speeds or densities.

statistics.harmonic_mean Function Syntax

Here's how you use the statistics.harmonic_mean function:

import statistics

harmonic_mean_value = statistics.harmonic_mean(data)

Parameters:

  • data: A sequence or iterable of numeric data (list, tuple, etc.).

Returns:

  • The harmonic mean of the given data.

Raises:

  • StatisticsError: If data is empty or contains non-positive values.

Examples

Basic Usage

Calculate the harmonic mean of a list of numbers.

import statistics

data = [1, 2, 3, 4, 5]
harmonic_mean_value = statistics.harmonic_mean(data)
print(f"Harmonic Mean: {harmonic_mean_value}")

Output:

Harmonic Mean: 2.18978102189781

Harmonic Mean of a List of Numbers

Calculate the harmonic mean of a list of integers.

import statistics

numbers = [10, 20, 30, 40, 50]
harmonic_mean_value = statistics.harmonic_mean(numbers)
print(f"Harmonic Mean of numbers: {harmonic_mean_value}")

Output:

Harmonic Mean of numbers: 21.8978102189781

Harmonic Mean of a List of Fractions

Calculate the harmonic mean of a list of fractions.

import statistics

fractions = [1/2, 1/3, 1/4, 1/5, 1/6]
harmonic_mean_value = statistics.harmonic_mean(fractions)
print(f"Harmonic Mean of fractions: {harmonic_mean_value}")

Output:

Harmonic Mean of fractions: 0.25

Handling Different Types of Numeric Data

Calculate the harmonic mean of a mixed list of integers and floats.

import statistics

numbers = [1.5, 2.5, 3.5, 4.5, 5.5]
harmonic_mean_value = statistics.harmonic_mean(numbers)
print(f"Harmonic Mean of numbers: {harmonic_mean_value}")

Output:

Harmonic Mean of numbers: 2.846697338153138

Real-World Use Case

Calculating Average Speed

Calculate the average speed of a trip with varying speeds using the harmonic mean.

import statistics

speeds = [60, 70, 80]  # speeds in km/h
average_speed = statistics.harmonic_mean(speeds)
print(f"Average speed: {average_speed} km/h")

Output:

Average speed: 69.04109589041096 km/h

Conclusion

The statistics.harmonic_mean function is a simple and effective way to calculate the harmonic mean of a set of numbers in Python. It is particularly useful for finding the average of rates or ratios, such as speeds or densities. This function makes it easy to determine the harmonic mean, which is a common requirement in various fields such as physics, engineering, and finance.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare