Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

fmean
python programming
statistics
communitycreator

What is statistics.fmean(data) in Python?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

This fmean() method is used to compute the float mean of input values. It converts input data into float values and calculates the arithmetic mean. This method is faster than the mean() method in older versions of Python.

Note: The new version of Python is version 3.8.

Syntax


statistics.fmean(data)

Parameters

It takes a single argument value:

  • data: It can be an iterable or sequence.

Return value

It returns an arithmetic mean as a floating-point value:

  • StatisticsError: If the dataset or data sequence is empty, then it raises a statistics error for a single data point.

Code example 1

# load statistice module
import statistics
# demo data as list
data = [3.52, 4.0, 5.25]
result = statistics.fmean(data)
print(result)

Explanation

In the code snippet above:

  • Line 2: We load the statistics module in our program.
  • Line 4: We create a list with demo data points.
  • Line 5: We use the statistics.fmean() method to compute floating mean.
  • Line 6: We print result (mean value) on the console as a float value.

Code example 2

# load statistice module
import statistics
# empty data sequence or list
data = []
result = statistics.fmean(data) # raises exception
# statistics.StatisticsError:
# fmean requires at least one data point
print(result)

Explanation

In the code snippet above:

  • Line 2: We load the statistics module in our program.
  • Line 4: We create an empty list.
  • Line 5: We use the statistics.fmean() method to compute the floating mean.

Due to an empty list, it raises the statistics.StatisticsError error. The fmean() requires at least one data point.

RELATED TAGS

fmean
python programming
statistics
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring