Python statistics Module

In data analysis and scientific computing, statistics is a very important tool.

Python provides a built-instatisticsmodule, specifically for handling basic statistical calculations. This article will detailstatisticsthe module's functions and usage, helping beginners quickly master how to use this module for basic statistical analysis.

statisticsThe module provides many common statistical functions, such as mean, median, variance, standard deviation, etc.

To use the statistics functions, you must first import:

import statistics

View the contents of the statistics module:

>>> import statistics
>>> dir(statistics)
['Counter', 'Decimal', 'Fraction', 'NormalDist', 'StatisticsError', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_coerce', '_convert', '_exact_ratio', '_fail_neg', '_find_lteq', '_find_rteq', '_isfinite', '_normal_dist_inv_cdf', '_ss', '_sum', 'bisect_left', 'bisect_right', 'erf', 'exp', 'fabs', 'fmean', 'fsum', 'geometric_mean', 'groupby', 'harmonic_mean', 'hypot', 'itemgetter', 'log', 'math', 'mean', 'median', 'median_grouped', 'median_high', 'median_low', 'mode', 'multimode', 'numbers', 'pstdev', 'pvariance', 'quantiles', 'random', 'sqrt', 'stdev', 'tau', 'variance']

Common Statistical Functions

Mean

The mean is the average of all values in a dataset.statisticsThe module provides themean()function to calculate the mean.

Example

data = [1, 2, 3, 4, 5]
mean_value = statistics.mean(data)
print("Mean:", mean_value)

Output:

均值: 3

Median

The median is the value located in the middle of a dataset when it is arranged in order of size.statisticsThe module provides themedian()function to calculate the median.

Example

data = [1, 2, 3, 4, 5]
median_value = statistics.median(data)
print("Median:", median_value)

Output:

中位数: 3

If the length of the dataset is even,median()the function automatically calculates the average of the two middle numbers.

Example

data = [1, 2, 3, 4]
median_value = statistics.median(data)
print("Median:", median_value)

Output:

中位数: 2.5

Mode

The mode is the value that appears most frequently in a dataset.statisticsThe module provides themode()function to calculate the mode.

Example

data = [1, 2, 2, 3, 4]
mode_value = statistics.mode(data)
print("Mode:", mode_value)

Output:

众数: 2

If there are no repeated values in the dataset,mode()the function will raise aStatisticsErrorexception.

Variance

Variance is a measure of the degree of dispersion of values in a dataset.statisticsThe module provides thevariance()function to calculate the variance.

Example

data = [1, 2, 3, 4, 5]
variance_value = statistics.variance(data)
print("Variance:", variance_value)

Output:

方差: 2.5

Standard Deviation

The standard deviation is the square root of the variance, used to measure the dispersion of a dataset.statisticsThe module provides thestdev()function to calculate the standard deviation.

Example

data = [1, 2, 3, 4, 5]
stdev_value = statistics.stdev(data)
print("Standard Deviation:", stdev_value)

Output:

标准差: 1.5811388300841898

Harmonic Mean

The harmonic mean is a special type of average, suitable for scenarios such as calculating rates.statisticsThe module provides theharmonic_mean()function to calculate the harmonic mean.

Example

data = [1, 2, 4]
harmonic_mean_value = statistics.harmonic_mean(data)
print("Harmonic Mean:", harmonic_mean_value)

Output:

调和平均数: 1.7142857142857142

Geometric Mean

The geometric mean is an average used to calculate growth rates or ratios.statisticsThe module provides thegeometric_mean()function to calculate the geometric mean.

Example

data = [1, 2, 4]
geometric_mean_value = statistics.geometric_mean(data)
print("Geometric Mean:", geometric_mean_value)

Output:

几何平均数: 2.0

Other Common Functions

Median Low and Median High

statisticsThe module also provides themedian_low()andmedian_high()functions, used to calculate the median low and median high of a dataset, respectively.

Example

data = [1, 2, 3, 4]
median_low_value = statistics.median_low(data)
median_high_value = statistics.median_high(data)
print("Median Low:", median_low_value)
print("Median High:", median_high_value)

Output:

中位数低: 2
中位数高: 3

Quantiles

Quantiles are values that divide a dataset into several equal parts.statisticsThe module provides thequantiles()function to calculate quantiles.

Example

data = [1, 2, 3, 4, 5]
quantiles_value = statistics.quantiles(data, n=4)
print("Quartiles:", quantiles_value)

Output:

四分位数: [1.5, 3.0, 4.5]

math Module Methods

Method Description
statistics.harmonic_mean() Calculate the harmonic mean of a given dataset.
statistics.mean() Calculate the mean of a dataset
statistics.median() Calculate the median of a dataset
statistics.median_grouped() Calculate the median of grouped data for a given grouped dataset
statistics.median_high() Calculate the high median of a given dataset
statistics.median_low() Calculate the low median of a given dataset.
statistics.mode() Calculate the mode of a dataset (the most frequently occurring value)
statistics.pstdev() Calculate the sample standard deviation of a given dataset
statistics.stdev() Calculate the standard deviation of a dataset
statistics.pvariance() Calculate the sample variance of a given dataset
statistics.variance() Calculate the variance of a dataset
statistics.quantiles() Calculate the quantiles of a dataset, with the number of quantiles specifiable (defaults to quartiles)
Other Extensions