Statistics Module in Python
statistics Module in Python
The statistics module in Python provides functions to perform statistical calculations on data, such as measures of central tendency (mean, median, mode), measures of spread (variance, standard deviation), and other useful statistical operations.
Here’s an overview of the most commonly used functions in the statistics module:
1. Central Tendency Functions
mean(): Returns the arithmetic mean (average) of a dataset.median(): Returns the median (middle value) of a dataset.mode(): Returns the mode (most common value) of a dataset.
Examples:
import statisticsdata = [1, 2, 3, 4, 5, 6, 7, 8, 9]# Meanmean_val = statistics.mean(data)print("Mean:", mean_val) # Output: 5# Medianmedian_val = statistics.median(data)print("Median:", median_val) # Output: 5# Mode# Mode requires data to have a value that appears more than once.data_with_mode = [1, 2, 2, 3, 4]mode_val = statistics.mode(data_with_mode)print("Mode:", mode_val) # Output: 22. Variability Functions
variance(): Returns the variance of a dataset, which measures the spread of the data around the mean. It calculates the sample variance by default, usingn-1in the denominator.pstdev(): Returns the population standard deviation (requires dividing byn).stdev(): Returns the sample standard deviation (requires dividing byn-1).
Examples:
# Variance (sample variance by default)variance_val = statistics.variance(data)print("Variance:", variance_val) # Output: 6.666666666666667# Population Standard Deviationpstdev_val = statistics.pstdev(data)print("Population Standard Deviation:", pstdev_val) # Output: 2.8722813232690143# Sample Standard Deviationstdev_val = statistics.stdev(data)print("Sample Standard Deviation:", stdev_val) # Output: 2.96069412624930673. Data Distribution Functions
harmonic_mean(): Returns the harmonic mean, useful for rates or ratios.geometric_mean(): Returns the geometric mean, useful for growth rates or multiplications.
Examples:
# Harmonic Meanharmonic_mean_val = statistics.harmonic_mean([1, 2, 4])print("Harmonic Mean:", harmonic_mean_val) # Output: 1.6# Geometric Mean (requires numbers > 0)geometric_mean_val = statistics.geometric_mean([1, 2, 4])print("Geometric Mean:", geometric_mean_val) # Output: 2.04. Other Statistical Functions
quantiles(): Returns a list of quantiles that divide the dataset into equal parts.median_low(): Returns the low median (lower middle value) when the data has an even number of elements.median_high(): Returns the high median (upper middle value) when the data has an even number of elements.multimode(): Returns a list of modes (handles multiple modes).skewness()(requiresscipylibrary): Measures the asymmetry of the probability distribution of a real-valued random variable.
Examples:
# Quantiles (divides data into equal parts)quantiles_val = statistics.quantiles(data, n=4)print("Quantiles:", quantiles_val) # Output: [3.0, 5.0, 7.0]# Median Low and Median High (for even-length datasets)even_data = [1, 2, 3, 4]median_low_val = statistics.median_low(even_data)median_high_val = statistics.median_high(even_data)print("Median Low:", median_low_val) # Output: 1print("Median High:", median_high_val) # Output: 35. Exceptions and Edge Cases
Some functions will raise exceptions if the data doesn’t meet the required conditions:
StatisticsError: Raised when a function cannot calculate a result due to invalid data.For example,
mode()will raise aStatisticsErrorif there is no unique mode.
Example of Exception Handling:
data_no_mode = [1, 1, 2, 2, 3, 3]try: mode_val = statistics.mode(data_no_mode)except statistics.StatisticsError: print("No unique mode found")6. Summary of Common Functions in the statistics Module
| Function | Description |
|---|---|
mean(data) | Returns the mean of the data. |
median(data) | Returns the median of the data. |
mode(data) | Returns the most common value(s) in the data. |
variance(data) | Returns the sample variance of the data. |
pstdev(data) | Returns the population standard deviation of the data. |
stdev(data) | Returns the sample standard deviation of the data. |
harmonic_mean(data) | Returns the harmonic mean of the data. |
geometric_mean(data) | Returns the geometric mean of the data. |
quantiles(data, n) | Returns a list of quantiles dividing the data into n parts. |
median_low(data) | Returns the low median (lower middle value) in even-length data. |
median_high(data) | Returns the high median (upper middle value) in even-length data. |
multimode(data) | Returns a list of modes (can handle multiple modes). |
Conclusion
The statistics module in Python provides a variety of useful functions for basic statistical calculations. It’s perfect for performing operations such as calculating the mean, median, mode, standard deviation, variance, and more. If you need advanced statistical functions or large-scale data analysis, consider using libraries like NumPy, SciPy, or Pandas, which offer more powerful and optimized methods for handling data.