# Various Uses of Python Statistics Module & Its Functions

Sakshi Khanna 14 Jan, 2024 • 3 min read

## Introduction

Python is a powerful programming language that offers a wide range of modules for various applications. One such module is the statistics module, which provides a comprehensive set of functions for statistical operations. In this blog, we will explore the Python statistics module in detail, covering all the methods, how to use them, and where to use them.

Python has rapidly become the go-to language in data science and is among the first things recruiters search for in a data scientist’s skill set. Are you looking to learn Python to switch to a data science career?

## Mathematical Statistics Functions

The Python statistics module is a powerful tool for performing mathematical statistics functions. It provides a wide range of functions for calculating measures of central tendency, dispersion, and more. For example, the mean, median, mode, variance, and standard deviation can all be easily calculated using the statistics module.

### Functions: Calculate Measures of Central Tendency

• mean(data): Calculates the arithmetic mean (average).
• median(data): Calculates the median (middle value).
• median_low(data): Calculates the low median of a multiset.
• median_high(data): Calculates the high median of a multiset.
• median_grouped(data, interval=1): Calculates the median of grouped continuous data.
• mode(data): Calculates the most frequent value(s) (mode).

### Functions: Measures of Dispersion

• pstdev(data, mu=None): Calculates the population standard deviation.
• pvariance(data, mu=None): Calculates the population variance.
• stdev(data, xbar=None): Calculates the sample standard deviation.
• variance(data, xbar=None): Calculates the sample variance.

Example:

``````import statistics

data = [1, 4, 6, 2, 3, 5]

mean = statistics.mean(data)

median = statistics.median(data)

stdev = statistics.stdev(data)

print("Mean:", mean)

print("Median:", median)

print("Standard deviation:", stdev)``````

Output:

Mean: 3.5

Median: 3.5

Standard deviation: 1.8708286933869707

In addition to basic statistical functions, the Python statistics module also allows you to describe your data in detail. This includes calculating the range, quartiles, and other descriptive statistics. These functions are extremely useful for gaining insights into the distribution and characteristics of your data.

• quantiles(data, n=4): Divides data into equal-sized groups (quartiles by default).
• fmean(data): Handles finite iterables gracefully.
• harmonic_mean(data): Useful for rates and ratios.
• geometric_mean(data): For values representing growth rates.
• multimode(data): Returns all modes (not just one).

Example:

``````import statistics

data = [1, 4, 6, 2, 3, 4, 4]  # Example dataset

quartiles = statistics.quantiles(data)

fmean = statistics.fmean(data)

print("Quartiles:", quartiles)

print("FMean:", fmean)``````

Output:

Quartiles: [2.0, 4.0, 4.0]

FMean: 3.4285714285714284

## Dealing with Missing Data

One common challenge in data analysis is dealing with missing values. The Python statistics module provides functions for handling missing data, such as removing or imputing missing values. This is essential for ensuring the accuracy and reliability of your statistical analysis.

Example: Imputing Missing Value with mean

``````import statistics

data = [1, 4, None, 6, 2, 3]

mean = statistics.mean(x for x in data if x is not None)

filled_data = [mean if x is None else x for x in data]

print(filled_data)``````

Output:

[1, 4, 3.2, 6, 2, 3]

## Data Analysis Techniques

The Python statistics module is an integral part of various data analysis techniques. Whether you’re performing hypothesis testing, regression analysis, or any other statistical analysis, the statistics module provides the necessary functions for carrying out these techniques. Understanding how to leverage the statistics module for different data analysis techniques is crucial for mastering Python statistics. Here’s an example of using the statistics module for hypothesis testing:

Example:

``````import statistics

import random

# Sample data

data = [1, 4, 6, 2, 3, 5]

# Calculate sample mean and standard deviation

sample_mean = statistics.mean(data)

sample_stdev = statistics.stdev(data)

# Generate many random samples with the same size as the original data

num_samples = 10000

random_means = []

for _ in range(num_samples):

random_sample = random.choices(data, k=len(data))

random_means.append(statistics.mean(random_sample))

# Calculate t-statistic

t_statistic = (sample_mean - 0) / (sample_stdev / (len(data) ** 0.5))  # Assuming a null hypothesis of 0

# Estimate p-value (proportion of random means more extreme than the sample mean)

p_value = (sum(1 for mean in random_means if abs(mean) >= abs(sample_mean))) / num_samples

print("t-statistic:", t_statistic)

print("p-value:", p_value)``````

Output:

t-statistic: 4.58257569495584

p-value: 0.5368

## Conclusion

In conclusion, the Python statistics module is a versatile and powerful tool for performing statistical operations. Whether you’re a data scientist, analyst, or researcher, mastering the statistics module is essential for gaining insights from your data. By understanding the various methods, how to use them, and where to use them, you can elevate your statistical analysis capabilities to new heights. So, start exploring the Python statistics module today and unlock its full potential for your data analysis needs.

Python has rapidly become the go-to language in data science and is among the first things recruiters search for in a data scientist’s skill set. Are you looking to learn Python to switch to a data science career?

Sakshi Khanna 14 Jan 2024