Geometric Mean in Python

Table of Contents

				
					import numpy as np
from scipy.stats import gmean
				
			

Example 1 Manual

				
					data = [4, 7, 3, 8, 2, 1, 5]
				
			
				
					n = len(data)
				
			
				
					product = 1
				
			
				
					for x in data:
        product *= x
				
			
				
					geometric_mean = product ** (1/n)
				
			
				
					print(geometric_mean)
				
			

Example 2 Numpy

				
					product_np = np.prod(data)
				
			
				
					result_numpy = np.power(product_np, 1/n)
				
			
				
					print(result_numpy)
				
			

Example 3 Scipy

				
					result_scipy = gmean(data)
				
			
				
					print(result_scipy)
				
			

Example 4 Scipy Percentages Growth Rate

Growth Rate
				
					percentages = [10, 15, 20, -5]
				
			
				
					decimal_values = [(1 + p / 100) for p in percentages]
				
			
				
					geometric_mean_decimal = gmean(decimal_values)
				
			
				
					geometric_mean_decimal
				
			
				
					geometric_mean_percentage = (geometric_mean_decimal - 1) * 100
				
			
				
					geometric_mean_percentage
				
			

Free Community

Join 1,000+ AI Automation Builders

Weekly tutorials, live calls & direct access to Ryan & Matt.

Join Free →

Keep Learning

python quantiles statistics

In Python, a quantile is a statistical term used to describe a point or value below which a certain proportion of the...

python variance and standard deviation

https://youtu.be/p4H2b2x_nWc#population and sample variance/std deviationVariance measures how far each data point in the set is from the mean andthus from every other...

Python Z-Score

We are going to be looking at Python Z-score. Z-score tells us how far a data poin is from the mean. https://youtu.be/QjG1ljFNF9U...

Spearman Rank Correlation

Spearman Rank Correlation [Simply explained] https://youtu.be/TNQTd9gR1c0 Example 2 Fast wth scipy