- Geometric Mean Function in Python – pandas (Dataframe, Row and column wise Geometric mean)
- Geometric Mean Function in Python:
- Simple geometric mean function is shown below
- Geometric Mean of a dataframe:
- Geometric Mean of the column in dataframe:
- Row wise geometric Mean of the dataframe:
- Calculate the geometric mean of the specific Column:
- Author
- Related Posts:
- Как вычислить среднее геометрическое в Python (с примерами)
- Пример 1: расчет среднего геометрического с помощью SciPy
- Пример 2: вычисление среднего геометрического с использованием NumPy
- Как обращаться с нулями
- Geometric Mean in Python (Example)
- Example Data & Add-On Libraries
- Example: Get Geometric Mean Using gmean() Function of SciPy Library
- Video & Further Resources
- Geometric Mean Pandas
- What is Geometric Mean?
- How to Calculate Geometric Mean Using Python?
- How to Calculate Geometric Mean Using Pandas?
- Alternative Approach: Calculate Geometric Mean Using the “statistics” Module
- Conclusion
- About the author
- Talha Saif Malik
Geometric Mean Function in Python – pandas (Dataframe, Row and column wise Geometric mean)
Geometric Mean Function in python pandas is used to calculate the geometric mean of a given set of numbers, Geometric mean of a data frame, Geometric mean of column and Geometric mean of rows. let’s see an example of each we need to use the package name “stats” from scipy in calculation of geometric mean. In this section we will learn,
- How to find the geometric mean of a given set of numbers
- How to find geometric mean of a dataframe
- How to find the geometric mean of a column in dataframe
- How to find row wise geometric mean of a dataframe
Geometric Mean Function in Python:
Simple geometric mean function is shown below
# calculate geometric mean from scipy import stats print(stats.gmean([1,9,5,6,6,7])) print(stats.gmean([4,11,15,16,5,7]))
Geometric Mean of a dataframe:
import pandas as pd import numpy as np from scipy import stats #Create a DataFrame d = < 'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine', 'Rahul','David','Andrew','Ajay','Teresa'], 'Score1':[62,47,55,74,31,77,85,63,42,32,71,57], 'Score2':[89,87,67,55,47,72,76,79,44,92,99,69]>df = pd.DataFrame(d) print df
So the resultant dataframe will be
Geometric Mean of the column in dataframe:
# Geometric Mean of the column in dataframe from scipy import stats scipy.stats.gmean(df.iloc[:,1:3],axis=0)
axis=0 argument calculates the column wise geometric mean of the dataframe so the result will be
Row wise geometric Mean of the dataframe:
# Row wise geometric mean of the dataframe from scipy import stats scipy.stats.gmean(df.iloc[:,1:3],axis=1)
axis=1 argument calculates the row wise geometric mean of the dataframe so the result will be
Calculate the geometric mean of the specific Column:
# geometric mean of the specific column scipy.stats.gmean(df.loc[:,"Score1"])
the above code calculates the geometric mean of the “Score1” column so the result will be
Author
With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark. View all posts
Related Posts:
Как вычислить среднее геометрическое в Python (с примерами)
В 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]
Geometric Mean in Python (Example)
This tutorial illustrates how to compute the geometric mean in Python programming.
The post consists of this information:
Example Data & Add-On Libraries
I’ll use the data below as a basis for this Python tutorial:
my_list = [4, 3, 8, 7, 8, 4, 4, 1, 5] # Create example list print(my_list) # Print example list # [4, 3, 8, 7, 8, 4, 4, 1, 5]
The previous output of the Python console shows that our example data is a list object containing nine integers.
Example: Get Geometric Mean Using gmean() Function of SciPy Library
In this example, I’ll demonstrate how to calculate the geometric mean of a list in Python.
For this task, we first have to import the gmean function of the SciPy library:
from scipy.stats import gmean # Import gmean function of SciPy library
In the next step, we can apply the gmean function to our example list to get the geometric mean:
print(gmean(my_list)) # Apply gmean function # 4.226198741306655
As you can see, the geometric mean of the numbers in our list is 4.22.
Video & Further Resources
Have a look at the following video which I have published on my YouTube channel. In the video, I’m explaining the Python programming code of this tutorial in the Python programming language:
The YouTube video will be added soon.
Furthermore, you might want to read the related articles on this website.
You have learned in this tutorial how to find the geometric mean in Python. Don’t hesitate to let me know in the comments below, in case you have any additional questions.
Geometric Mean Pandas
In statistics, the “geometric mean” is a measure of central tendency that represents the average value of a collection of numbers by utilizing the product of those numbers rather than their sum. A “geometric mean” is a useful tool for analyzing data that follows an exponential growth pattern, such as rates of change or investment returns.
This Python post provides a complete guide on calculating geometric means in Python using pandas. Following are the contents that will be covered:
What is Geometric Mean?
The “geometric mean” is utilized to calculate the central tendency of a set/collection of numbers. Unlike “arithmetic mean”, which adds up all the numbers and then divides them by the total number of items, the geometric mean multiplies all the numbers instead and then accept the “nth” root of the resulting product, where “n” represents the number of set items.
The formula for calculating the “geometric mean” of a set/collection of numbers is shown below:
Here, “x1”, “x2”, “x3”, …, and “xn” are the numbers in the set, and “n” refers to the number of items in the set.
How to Calculate Geometric Mean Using Python?
Python provides several built-in functions to perform mathematical calculations, including the calculation of the geometric mean. To compute/calculate the geometric mean of a collection of numbers in Python, use the combined “pow()” and “len()” functions in the below example.
Example
Overview of the below-given code:
import math
numbers = [ 2 , 4 , 6 , 8 , 10 ]
product = math . prod ( numbers )
geometric_mean = pow ( product , 1 / len ( numbers ) )
print ( «Geometric Mean:» , geometric_mean )
In the above code snippet:
- The “math” module is imported and the list containing numbers is initialized, respectively.
- The “prod()” function is used to compute the product of all the list numbers.
- The “pow()” function is used to calculate the geometric mean of these numbers by raising the product of all numbers to the power of “1/5” (the length of the list), which is equivalent to taking the fifth root of that product.
Output
The above output implies that the geometric mean has been calculated accordingly.
How to Calculate Geometric Mean Using Pandas?
Pandas provides several functions to perform mathematical calculations, including the calculation of the geometric mean. To compute/calculate the geometric mean of a column in pandas DataFrame, use the “geometric_mean()” function from the “scipy.stats” module.
Example
Go through the below-provided lines of code:
import pandas as pd
from scipy. stats import gmean
df = pd. DataFrame ( { ‘numbers’ : [ 2 , 4 , 6 , 8 , 10 ] } )
geometric_mean = gmean ( df [ ‘numbers’ ] )
print ( «Geometric Mean:» , geometric_mean )
- The “pandas” and “scipy.stats” modules are imported, respectively.
- After that, the “pd.DataFrame()” function is used to create a DataFrame.
- Lastly, the “gmean()” function from the “scipy.stats” module is utilized to find/calculate the geometric mean of the numbers in the “numbers” column of the given dataframe.
Output
The above output shows that the geometric mean has been calculated successfully.
Alternative Approach: Calculate Geometric Mean Using the “statistics” Module
Python also has a built-in “statistics” module that includes a “geometric_mean()” function that can be applied to calculate the geometric mean of a list of numbers. To use this function with a “Pandas Series” or “DataFrame object”, we first need to convert it to a list.
Example
Consider the below-stated code:
import pandas as pd
import statistics as stats
data = pd. Series ( [ 2 , 4 , 6 , 8 , 10 ] )
data_list = data. tolist ( )
gm = stats. geometric_mean ( data_list )
print ( «The geometric mean is:» , gm )
- Firstly, the “pandas” and “statistics” modules are imported.
- In the next step, the “pd.Series()” function is utilized to create/make the series object.
- Now, the “data.tolist()” function converts the series object into a list.
- Finally, the “stats.geometric_mean()” function takes the converted list as an argument and calculates the geometric mean of the passed list.
Output
The above output signifies that the geometric mean of the passed list has been computed successfully.
Conclusion
To calculate the “geometric mean” in Python, use the combined “pow()” and “len()” functions, the “gmean()” function, or the “statistics” module. The former approaches can be applied in simple Python, the “gmean()” function, however, can be used to compute the geometric mean of a given collection of integers, lists, or DataFrame in pandas. This post presented a complete guide on calculating the geometric mean of the given input using “pandas” and other modules.
About the author
Talha Saif Malik
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.