Python statistics geometric_mean()

The statistics.geometric_mean function in Python's statistics module calculates the geometric mean of a given set of numbers. The geometric mean is the nth root of the product of the numbers, where n is the number of values. It is useful for finding the central tendency of multiplicative data.

Table of Contents

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

Introduction

The statistics.geometric_mean function is part of the statistics module, which provides functions for mathematical statistics of numeric data. The geometric mean is particularly useful when dealing with data that involves growth rates, such as population growth, financial investments, and other multiplicative processes.

statistics.geometric_mean Function Syntax

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

import statistics

geometric_mean_value = statistics.geometric_mean(data)

Parameters:

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

Returns:

  • The geometric mean of the given data.

Raises:

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

Examples

Basic Usage

Calculate the geometric mean of a list of numbers.

import statistics

data = [1, 2, 3, 4, 5]
geometric_mean_value = statistics.geometric_mean(data)
print(f"Geometric Mean: {geometric_mean_value}")

Output:

Geometric Mean: 2.6051710846973517

Geometric Mean of a List of Numbers

Calculate the geometric mean of a list of integers.

import statistics

numbers = [10, 20, 30, 40, 50]
geometric_mean_value = statistics.geometric_mean(numbers)
print(f"Geometric Mean of numbers: {geometric_mean_value}")

Output:

Geometric Mean of numbers: 26.051710846973528

Geometric Mean of a List of Positive Numbers

Calculate the geometric mean of a list of positive numbers.

import statistics

numbers = [1.5, 2.5, 3.5, 4.5, 5.5]
geometric_mean_value = statistics.geometric_mean(numbers)
print(f"Geometric Mean of numbers: {geometric_mean_value}")

Output:

Geometric Mean of numbers: 3.179324839089783

Handling Different Types of Numeric Data

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

import statistics

numbers = [1.1, 2.2, 3.3, 4.4, 5.5]
geometric_mean_value = statistics.geometric_mean(numbers)
print(f"Geometric Mean of numbers: {geometric_mean_value}")

Output:

Geometric Mean of numbers: 2.865688193167087

Real-World Use Case

Calculating Average Growth Rate

Calculate the average growth rate of an investment over several years using the geometric mean.

import statistics

growth_rates = [1.05, 1.10, 1.15, 1.20]  # growth rates for 4 years
average_growth_rate = statistics.geometric_mean(growth_rates)
print(f"Average growth rate: {average_growth_rate}")

Output:

Average growth rate: 1.1236091512399002

Conclusion

The statistics.geometric_mean function is a simple and effective way to calculate the geometric mean of a set of numbers in Python. It is particularly useful for finding the central tendency of multiplicative data, such as growth rates and investment returns. This function makes it easy to determine the geometric mean, which is a common requirement in various fields such as finance, biology, and environmental science.

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