fmean() function in Python statistics

Statistics module of Python 3.8 provides a function fmean() that converts all the data into float data-type and then computes the arithmetic mean or average of data that is provided in the form of a sequence or an iterable. The output of this function is always a float.
The only difference in computing mean using mean() and fmean() is that while using fmean() data gets converted to floats whereas in case of mean(), data does not get converted to floats. Moreover fmean() function runs faster than the mean() function.

Syntax: fmean([data-set}])

Parameters:[data-set]: List or tuple of a set of numbers.

Returns: floating-point arithmetic mean of the provided data.

Exceptions: StatisticsError. Is raised when the data set is empty



Code #1: Demonstration of fmean()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate fmean() 
    
  
import statistics 
    
# list of numbers 
data = [1.8, 3.8, 4, 5.8, 7, 9.6, 2.4
    
fm = statistics.fmean(data) 
    
# Printing the floating-point mean 
print("Floating Point Mean is :", fm) 

chevron_right


Output:

Floating Point Mean is: 4.914285714285714

Code #2: Demonstration of fmean()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate fmean() 
  
  
from statistics import fmean 
    
# tuple of positive numbers 
A1 = (11.4, 3.7, 4, 5, 7.9, 9.4, 2
    
# tuple of negative numbers
A2 = (-1.9, -2.8, -4, -7.5, -12.2, -19
    
# tuple of a mixed range of numbers 
A3 = (-1.9, -13.8, -6, 4.2, 5.9, 9.1
      
# dictionary of a set of values
# keys are taken in consideration by fmean() 
A4 = {1.1:"one.one", 2.8:"two.eight", 3:"three"
    
# Printing the mean of A1, A2, A3, A4
print("Floating Point Mean of A1 is", fmean(A1))
print("Floating Point Mean of A2 is", fmean(A2))
print("Floating Point Mean of A3 is", fmean(A3))
print("Floating Point Mean of A4 is", fmean(A4))

chevron_right


Output :

Floating Point Mean of A1 is 6.2
Floating Point Mean of A2 is -7.8999999999999995
Floating Point Mean of A3 is -0.41666666666666674
Floating Point Mean of A4 is 2.3000000000000003

Code #3: StatisticsError

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate StatisticsError
    
from statistics import fmean 
    
data =[]
print(fmean(data))  

chevron_right


Output:

Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\statistics.
py”, line 345, in fmean
raise StatisticsError(‘fmean requires at least one data point’) from None
statistics.StatisticsError: fmean requires at least one data point

Applications:
The applications of fmean() is similar to mean(), although fmean() is relatively faster than mean(). fmean() is only to be used when the floating-point mean of data needs to be calculated, otherwise for calculating the mean of a sample, mean() is preferred. Since fmean() converts its data into floats, it produces more accurate results.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.