Как найти среднее геометрическое в python

While working with Python, we can have a problem in which we need to find geometric mean of a list cumulative. This problem is common in Data Science domain. Let’s discuss certain ways in which this problem can be solved. 
Method #1 : Using loop + formula The simpler manner to approach this problem is to employ the formula for finding geometric mean and perform using loop shorthands. This is the most basic approach to solve this problem. 

Python3

import math

test_list = [6, 7, 3, 9, 10, 15]

print("The original list is : " + str(test_list))

temp = 1

for i in range(0, len(test_list)) :

    temp = temp * test_list[i]

temp2 = (float)(math.pow(temp, (1 / len(test_list))))

res = (float)(temp2)

print("The geometric mean of list is : " + str(res))

Output : 

The original list is : [6, 7, 3, 9, 10, 15]
The geometric mean of list is : 7.443617568993922

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the loop + formula which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space

  Method #2 : Using statistics.geometric_mean() This task can also be performed using inbuilt function of geometric_mean(). This is new in Python versions >= 3.8. 

Python3

import statistics

test_list = [6, 7, 3, 9, 10, 15]

print("The original list is : " + str(test_list))

res = statistics.geometric_mean(test_list, 1)

print("The geometric mean of list is : " + str(res))

Output : 

The original list is : [6, 7, 3, 9, 10, 15]
The geometric mean of list is : 7.443617568993922

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required

Method #3: Using numpy library

Note: Install numpy module using command “pip install numpy”

We can use the numpy library which has a numpy.prod() function to find the product of all elements in the list and then use numpy.power() function with the reciprocal of the length of the list as the exponent to find the geometric mean.

Python

import numpy as np

test_list = [6, 7, 3, 9, 10, 15]

print("The original list is : " + str(test_list))

result = np.power(np.prod(test_list), 1/len(test_list))

print("The geometric mean of list is : " + str(result))

Output:

The original list is : [6, 7, 3, 9, 10, 15]
The geometric mean of list is : 7.443617568993922

Time complexity: O(n)
Auxiliary space: O(1)

Last Updated :
09 Apr, 2023

Like Article

Save Article

  • Редакция Кодкампа

17 авг. 2022 г.
читать 1 мин


В Python есть два способа вычисления среднего геометрического:

Метод 1: вычислить среднее геометрическое с помощью SciPy

from scipy. stats import gmean

#calculate geometric mean
gmean([value1, value2, value3, ...])

Метод 2: вычислить среднее геометрическое с помощью NumPy

import numpy as np

#define custom function
def g_mean(x):
 a = np.log (x)
 return np.exp (a.mean())

#calculate geometric mean 
g_mean([value1, value2, value3, ...])

Оба метода вернут одинаковые результаты.

В следующих примерах показано, как использовать каждый из этих методов на практике.

Пример 1: расчет среднего геометрического с помощью SciPy

В следующем коде показано, как использовать функцию gmean() из библиотеки SciPy для вычисления среднего геометрического массива значений:

from scipy. stats import gmean

#calculate geometric mean
gmean([1, 4, 7, 6, 6, 4, 8, 9])

4.81788719702029

Среднее геометрическое оказывается равным 4,8179 .

Пример 2: вычисление среднего геометрического с использованием NumPy

В следующем коде показано, как написать пользовательскую функцию для вычисления среднего геометрического с использованием встроенных функций из библиотеки NumPy :

import numpy as np

#define custom function
def g_mean(x):
 a = np.log (x)
 return np.exp (a.mean())

#calculate geometric mean
g_mean([1, 4, 7, 6, 6, 4, 8, 9])

4.81788719702029

Среднее геометрическое оказывается равным 4,8179 , что соответствует результату из предыдущего примера.

Как обращаться с нулями

Обратите внимание, что оба метода возвращают ноль, если в массиве, с которым вы работаете, есть нули.

Таким образом, вы можете использовать следующий фрагмент кода, чтобы удалить все нули из массива перед вычислением среднего геометрического:

#create array with some zeros
x = [1, 0, 0, 6, 6, 0, 8, 9]

#remove zeros from array 
x_new = [i for i in x if i != 0]

#view updated array
print(x_new)

[1, 6, 6, 8, 9]

Дополнительные ресурсы

Как рассчитать среднеквадратичную ошибку (MSE) в Python
Как рассчитать среднюю абсолютную ошибку в Python


There are two ways to calculate the geometric mean in Python:

Method 1: Calculate Geometric Mean Using SciPy

from scipy.stats import gmean

#calculate geometric mean
gmean([value1, value2, value3, ...])

Method 2: Calculate Geometric Mean Using NumPy

import numpy as np

#define custom function
def g_mean(x):
    a = np.log(x)
    return np.exp(a.mean())

#calculate geometric mean 
g_mean([value1, value2, value3, ...])

Both methods will return the exact same results.

The following examples show how to use each of these methods in practice.

Example 1: Calculate Geometric Mean Using SciPy

The following code shows how to use the gmean() function from the SciPy library to calculate the geometric mean of an array of values:

from scipy.stats import gmean

#calculate geometric mean
gmean([1, 4, 7, 6, 6, 4, 8, 9])

4.81788719702029

The geometric mean turns out to be 4.8179.

Example 2: Calculate Geometric Mean Using NumPy

The following code shows how to write a custom function to calculate a geometric mean using built-in functions from the NumPy library:

import numpy as np

#define custom function
def g_mean(x):
    a = np.log(x)
    return np.exp(a.mean())

#calculate geometric mean
g_mean([1, 4, 7, 6, 6, 4, 8, 9])

4.81788719702029

The geometric mean turns out to be 4.8179, which matches the result from the previous example.

How to Handle Zeros

Note that both methods will return a zero if there are any zeros in the array that you’re working with.

Thus, you can use the following bit of code to remove any zeros from an array before calculating the geometric mean:

#create array with some zeros
x = [1, 0, 0, 6, 6, 0, 8, 9]

#remove zeros from array 
x_new = [i for i in x if i != 0]

#view updated array
print(x_new)

[1, 6, 6, 8, 9]

Additional Resources

How to Calculate Mean Squared Error (MSE) in Python
How to Calculate Mean Absolute Error in Python

I wonder is there any easy way to do geometric mean using python but without using python package. If there is not, is there any simple package to do geometric mean?

asked Mar 29, 2017 at 16:48

3

The formula of the gemetric mean is:

geometrical mean

So you can easily write an algorithm like:

import numpy as np

def geo_mean(iterable):
    a = np.array(iterable)
    return a.prod()**(1.0/len(a))

You do not have to use numpy for that, but it tends to perform operations on arrays faster than Python. See this answer for why.

In case the chances of overflow are high, you can map the numbers to a log domain first, calculate the sum of these logs, then multiply by 1/n and finally calculate the exponent, like:

import numpy as np

def geo_mean_overflow(iterable):
    return np.exp(np.log(iterable).mean())

answered Mar 29, 2017 at 17:00

Willem Van Onsem's user avatar

Willem Van OnsemWillem Van Onsem

434k30 gold badges420 silver badges541 bronze badges

7

In case someone is looking here for a library implementation, there is gmean() in scipy, possibly faster and numerically more stable than a custom implementation:

>>> from scipy.stats import gmean
>>> gmean([1.0, 0.00001, 10000000000.])
46.415888336127786

Compatible with both Python 2 and 3.*

answered Nov 28, 2018 at 19:00

Marcin Wojnarski's user avatar

0

Starting Python 3.8, the standard library comes with the geometric_mean function as part of the statistics module:

from statistics import geometric_mean

geometric_mean([1.0, 0.00001, 10000000000.]) # 46.415888336127786

answered Apr 7, 2019 at 17:55

Xavier Guihot's user avatar

Xavier GuihotXavier Guihot

53k21 gold badges286 silver badges186 bronze badges

1

just do this:

numbers = [1, 3, 5, 7, 10]


print reduce(lambda x, y: x*y, numbers)**(1.0/len(numbers))

answered Mar 29, 2017 at 16:51

Liam's user avatar

LiamLiam

5,9194 gold badges39 silver badges53 bronze badges

1

Here’s an overflow-resistant version in pure Python, basically the same as the accepted answer.

import math

def geomean(xs):
    return math.exp(math.fsum(math.log(x) for x in xs) / len(xs))

answered May 20, 2019 at 22:41

rmmh's user avatar

rmmhrmmh

6,98125 silver badges37 bronze badges

You can also calculate the geometrical mean with numpy:

import numpy as np
np.exp(np.mean(np.log([1, 2, 3])))

result:

1.8171205928321397

answered May 12, 2020 at 21:19

gil.fernandes's user avatar

gil.fernandesgil.fernandes

12.6k5 gold badges60 silver badges75 bronze badges

you can use pow function, as follows :

def p(*args):
    k=1
    for i in args:
        k*=i
    return pow(k, 1/len(args))]

>>> p(2,3)
2.449489742783178

Asclepius's user avatar

Asclepius

56.4k17 gold badges164 silver badges142 bronze badges

answered Dec 25, 2020 at 11:36

Bairam Komaki's user avatar

Geometric mean
import pandas as pd
geomean=Variable.product()**(1/len(Variable))
print(geomean)
Geometric mean with Scipy
from scipy import stats
print(stats.gmean(Variable))

Asclepius's user avatar

Asclepius

56.4k17 gold badges164 silver badges142 bronze badges

answered Oct 30, 2019 at 7:02

user12295593's user avatar

In this short guide, you’ll see how to calculate the geometric mean in Python.

In the sections below, you’ll observe 3 ways to calculate the geometric mean in Python. For each of the methods to be reviewed, the goal is to derive the geometric mean, given the following values:

8, 16, 22, 12, 41

Method 1: Simple Calculations to get the Geometric Mean

To start, you can use the following calculations to get the geometric mean:

multiply_values = 8*16*22*12*41
n = 5
geometric_mean = (multiply_values)**(1/n)
print ('The Geometric Mean is: ' + str(geometric_mean)) 

Where:

  • multiply_values represents the multiplication of all the values in the dataset
  • reflects the number of items in the dataset. In our example, there are 5 items
  • geometric_mean = (multiply_values)**(1/n) is the actual calculation to derive the geometric mean

Run the code in Python, and you’ll get the following result: 16.9168

Method 2: Using a List to Derive the Geometric Mean in Python

Alternatively, you can place all the values in a list, where each value should be separated by a comma:

multiply = 1
values = [8,16,22,12,41]
n = len(values)

for i in values:
    multiply = (multiply)*(i)

geometric_mean = (multiply)**(1/n)
print ('The Geometric Mean is: ' + str(geometric_mean)) 

Once you run the code in Python, you’ll get the same result: 16.9168

Method 3: Using Pandas and Scipy

You could also use Pandas and Scipy to obtain the geometric mean:

from pandas import DataFrame
from scipy.stats.mstats import gmean

data = {'values': [8,16,22,12,41]}
df = DataFrame(data)

geometric_mean = gmean(df.loc[:,'values'])
print ('The Geometric Mean is: ' + str(geometric_mean)) 

As before, you’ll get the same geometric mean: 16.9168

Добавить комментарий