What is the statistics.geometric_mean() function in Python?
In this shot, we will discuss how to use the statistics.geometric_mean() function in Python.
Introduction
The statistics module in Python is an extremely useful module that provides functions to calculate mathematical statistics of numeric data.
The statistics module supports both int and float type numeric data. Some of the functions in the statistics module in Python include:
mean()median()mode()geometric_mean()harmonic_mean()
Geometric mean
The geometric mean is referred to as the root of the product of numbers. Say we have a set of numbers, , … . The geometric mean is defined as:
=
For example, let the list of numbers be 54, 24, 36. Their geometric mean is = 36.
Code
Let’s take a look at the code snippet below.
import statisticsdata = [54, 24, 36]result = statistics.geometric_mean(data)print(result)
Explanation
-
In line 1, we import the
statisticsmodule to callgeometric_mean(). -
In line 2, we provide a list of data whose geometric mean needs to be found.
-
In line 3, we compute the geometric mean of the provided numbers.
-
In line 4, we print the output, which is stored in
result.
Note:
Module_name.function_nameis the syntax for calling any function inside a module.- The output is not exactly 36 and has some floating points. This is because of the computational style of a machine. We can overcome this by using the
round()function.
The statistics.geometric_mean() function comes in handy when we want to compute the central tendency of datasets.