Python statistics harmonic_mean()

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

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

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare 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.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

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