🎓 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.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
- Introduction
statistics.geometric_meanFunction Syntax- Examples
- Basic Usage
- Geometric Mean of a List of Numbers
- Geometric Mean of a List of Positive Numbers
- Handling Different Types of Numeric Data
- Real-World Use Case
- 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: Ifdatais 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.
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:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment