Skip to content
Related Articles

Related Articles

fmean() function in Python statistics

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Jul, 2020

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()

# 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) 


Floating Point Mean is: 4.914285714285714

Code #2: Demonstration of fmean()

# 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:"", 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))

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

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


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

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
Recommended Articles
Page :

Start Your Coding Journey Now!