Python statistics Module
In data analysis and scientific computing, statistics is a very important tool.
Python provides a built-in statistics module specifically designed for handling basic statistical calculations. This article will detail the functions and usage of the statistics module, helping beginners quickly master how to use this module for basic statistical analysis.
The statistics module provides many commonly used statistical functions, such as mean, median, variance, standard deviation, etc.
To use statistics functions, you must first import:
View the contents of the statistics module:
Common Statistical Functions
Section titled “Common Statistical Functions”The mean is the average value of all numbers in a dataset. The statistics module provides the mean() function to calculate the mean.
Example
Section titled “Example”Output:
Median
Section titled “Median”The median is the value located at the middle position when the dataset is arranged in order of size. The statistics module provides the median() function to calculate the median.
Example
Section titled “Example”Output:
If the dataset length is even, the median() function automatically calculates the average of the two middle numbers.
Example
Section titled “Example”Output:
The mode is the value that appears most frequently in the dataset. The statistics module provides the mode() function to calculate the mode.
Example
Section titled “Example”Output:
If there are no duplicate values in the dataset, the mode() function will raise a StatisticsError exception.
Variance
Section titled “Variance”Variance is a measure of the dispersion of values in a dataset. The statistics module provides the variance() function to calculate variance.
Example
Section titled “Example”Output:
Standard Deviation
Section titled “Standard Deviation”Standard deviation is the square root of the variance, used to measure the dispersion of a dataset. The statistics module provides the stdev() function to calculate standard deviation.
Example
Section titled “Example”Output:
Harmonic Mean
Section titled “Harmonic Mean”The harmonic mean is a special type of average, suitable for calculating rates and similar scenarios. The statistics module provides the harmonic_mean() function to calculate the harmonic mean.
Example
Section titled “Example”Output:
Geometric Mean
Section titled “Geometric Mean”The geometric mean is a type of average used for calculating growth rates or ratios. The statistics module provides the geometric_mean() function to calculate the geometric mean.
Example
Section titled “Example”Output:
Other Common Functions
Section titled “Other Common Functions”Median Low and Median High
Section titled “Median Low and Median High”The statistics module also provides the median_low() and median_high() functions, used to calculate the median low and median high of a dataset respectively.
Example
Section titled “Example”Output:
Quantiles
Section titled “Quantiles”Quantiles are values that divide a dataset into equal parts. The statistics module provides the quantiles() function to calculate quantiles.
Example
Section titled “Example”Output:
statistics Module Methods
Section titled “statistics Module Methods”| Method | Description |
| statistics.harmonic_mean() | Calculates the harmonic mean of the given dataset. |
| statistics.mean() | Calculates the arithmetic mean of the dataset. |
| statistics.median() | Calculates the median of the dataset. |
| statistics.median_grouped() | Calculates the grouped median of the grouped dataset. |
| statistics.median_high() | Calculates the high median of the given dataset. |
| statistics.median_low() | Calculates the low median of the given dataset. |
| statistics.mode() | Calculates the mode (most frequent value) of the dataset. |
| statistics.pstdev() | Calculates the population standard deviation. |
| statistics.stdev() | Calculates the standard deviation of the dataset. |
| statistics.pvariance() | Calculates the population variance of the given dataset. |
| statistics.variance() | Calculates the variance of the dataset. |
| statistics.quantiles() | Calculates quantiles of the dataset, with configurable number of quantiles (default is quartiles). |