Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python statistics.harmonic_mean() Method

Python statistics.harmonic_mean() Method

Python statistics Module Python statistics Module

Python statistics.harmonic_mean() Used to compute the harmonic mean of a given data set.

The harmonic mean is a method of computing an average, used to calculate the average ratio of a set of values. It is computed by taking the reciprocal of each value, computing their arithmetic mean, and then taking the reciprocal of the result.

The method's data parameter is an iterable containing numeric values, such as a list or tuple. It computes and returns the harmonic mean of the data set as the result.

The harmonic mean is computed as follows:

If you have four data values (a, b, c and d), the calculation is:4 / (1/a + 1/b + 1/c + 1/d)。

Syntax

The syntax of the statistics.harmonic_mean() method is as follows:

statistics.harmonic_mean(data)

Parameter Description:

Return Value

The function computes and returns the harmonic mean of the data set as the result.

Example

The following example returns the arccosine of specified numbers:

Example

import

statistics

data

=

[

2

,

4

,

8

,

16

]

harmonic_mean

=

statistics.

harmonic_mean

(

data

)

print

(

harmonic_mean

)

Output result:



4.0

In the above example, the given data set is [2, 4, 8, 16], and the harmonic_mean() function computes a harmonic mean of 4.0.

The harmonic mean is often used for data related to ratios and rates, such as speed, working time and unit cost. It is very useful for handling reciprocals and proportional relationships, and can effectively handle special cases such as computing average speed or average efficiency.

Python statistics Module Python statistics Module