How to Use the Python statistics.fmean() Function

How to Use the Python statistics.fmean() Function

Introduction

The statistics module in Python provides a suite of functions for performing statistical calculations on data sets. One of the fundamental metrics in data analysis is the mean, which serves as a measure of central tendency.

Let’s explore the statistics.fmean() function, which is designed for calculating the mean of a series of numbers with a focus on performance and handling of floating-point data.

Using the statistics.fmean() Function

The fmean() function computes the floating-point mean of the input data, which means it is optimized for performance when dealing with floating-point numbers. The primary purpose of fmean() is to provide a faster alternative to the standard mean calculation, particularly when working with large datasets.

Python’s statistics module includes several functions for calculating different types of means:

  • mean(): Calculates the arithmetic mean of data, but it may be less efficient for large datasets due to its handling of data types
  • harmonic_mean(): Computes the harmonic mean, suitable for rates and ratios
  • median(): Finds the middle value in a dataset, which is different from the mean but also a measure of central tendency

To use the fmean() function, you first need to import the statistics module. Here’s how to do that:

import statistics

The basic syntax of the fmean() function is:

statistics.fmean(data)

where data is an iterable (such as a list or tuple) of numbers (preferably floating-point) for which the mean needs to be calculated.

Let’s look at a simple example where we calculate the mean of a list of floating-point numbers:

import statistics
data = [1.5, 2.5, 3.5, 4.5, 5.5]
mean_value = statistics.fmean(data)
print("The mean is:", mean_value)

# Output: The mean is: 3.5

The fmean() function can be used with various data structures. Below are examples demonstrating its use with lists, tuples, and NumPy arrays. First up is lists:

data_list = [10.0, 20.0, 30.0, 40.0]
mean_list = statistics.fmean(data_list)
print("Mean of list:", mean_list)

# Output: Mean of list: 25.0

And now using fmean() with tuples:

data_tuple = (5.0, 15.0, 25.0, 35.0)
mean_tuple = statistics.fmean(data_tuple)
print("Mean of tuple:", mean_tuple)

# Output: Mean of tuple: 20.0

If you are working with NumPy arrays, you can convert them to lists before using fmean():

import numpy as np
data_array = np.array([2.5, 4.5, 6.5, 8.5])
mean_array = statistics.fmean(data_array.tolist())
print("Mean of NumPy array:", mean_array)

# Output: Mean of NumPy array: 5.5

When your dataset contains special values like NaN (Not a Number) or infinite values, it’s important to know how fmean() handles them. The function will ignore these non-finite values.

Here’s an example demonstrating this behavior:

data_with_nan = [1.0, 2.0, float('nan'), 3.0]
mean_nan = statistics.fmean(data_with_nan)
data_with_inf = [1.0, 2.0, float('inf'), 3.0]
mean_inf = statistics.fmean(data_with_inf)

print("Mean with NaN:", mean_nan)
print("Mean with infinity:", mean_inf)

# Output:
# Mean with NaN: 2.0
# Mean with infinity: 2.0

Wrapping Up

In this article, we explored how to use the statistics.fmean() function in Python for calculating the mean of floating-point numbers. Consider rading more about this function and other statistics module functions in the official Python documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *