Python statistics Module
In data analysis and scientific computing, statistics is a very important tool.
Python provides a built-in statistics module, dedicated to basic statistical calculations. This article explains the statistics module's features and usage in detail, helping beginners quickly learn how to use it for basic statistical analysis.
statistics The module provides many commonly used statistical functions, such as mean, median, variance and standard deviation.
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 data set.statistics The module provides the mean() function to calculate the mean.
Example
data
=
[
1
,
2
,
3
,
4
,
5
]
mean_value
=
statistics.
mean
(
data
)
(
"Mean:"
,
mean_value
)
Output:
均值: 3
Median
The median is the value in the middle of a data set when it is sorted by size.statistics The module provides the median() function to calculate the median.
Example
data
=
[
1
,
2
,
3
,
4
,
5
]
median_value
=
statistics.
median
(
data
)
(
"Median:"
,
median_value
)
Output:
中位数: 3
If the length of the data set is even, themedian() function automatically computes the average of the two middle numbers.
Example
data
=
[
1
,
2
,
3
,
4
]
median_value
=
statistics.
median
(
data
)
(
"Median:"
,
median_value
)
Output:
中位数: 2.5
Mode
The mode is the value that appears most frequently in a data set.statistics The module provides the mode() function to calculate the mode.
Example
data
=
[
1
,
2
,
2
,
3
,
4
]
mode_value
=
statistics.
mode
(
data
)
(
"Mode:"
,
mode_value
)
Output:
众数: 2
If there are no repeated values in the data set, themode() function raises StatisticsError exception.
Variance
Variance is a measure of how spread out the values in a data set are.statistics The module provides the variance() function to calculate the variance.
Example
data
=
[
1
,
2
,
3
,
4
,
5
]
variance_value
=
statistics.
variance
(
data
)
(
"Variance:"
,
variance_value
)
Output:
方差: 2.5
Standard Deviation
The standard deviation is the square root of the variance and measures the dispersion of a data set.statistics The module provides the stdev() function to calculate the standard deviation.
Example
data
=
[
1
,
2
,
3
,
4
,
5
]
stdev_value
=
statistics.
stdev
(
data
)
(
"Standard deviation:"
,
stdev_value
)
Output:
标准差: 1.5811388300841898
Harmonic Mean
The harmonic mean is a special kind of average, suitable for scenarios such as computing rates.statistics The module provides the harmonic_mean() function to calculate the harmonic mean.
Example
data
=
[
1
,
2
,
4
]
harmonic_mean_value
=
statistics.
harmonic_mean
(
data
)
(
"Harmonic mean:"
,
harmonic_mean_value
)
Output:
调和平均数: 1.7142857142857142
Geometric Mean
The geometric mean is an average used to compute growth rates or ratios.statistics The module provides the geometric_mean() function to calculate the geometric mean.
Example
data
=
[
1
,
2
,
4
]
geometric_mean_value
=
statistics.
geometric_mean
(
data
)
(
"Geometric mean:"
,
geometric_mean_value
)
Output:
几何平均数: 2.0
Other common functions
Median Low and Median High
statistics The module also provides the median_low() and median_high() functions, used to compute the low median and the high median of a data set respectively.
Example
data
=
[
1
,
2
,
3
,
4
]
median_low_value
=
statistics.
median_low
(
data
)
median_high_value
=
statistics.
median_high
(
data
)
(
"Median low:"
,
median_low_value
)
(
"Median high:"
,
median_high_value
)
Output:
中位数低: 2
中位数高: 3
Quantiles
Quantiles are values that divide a data set into several equal parts.statistics The module provides the quantiles() function to calculate quantiles.
Example
data
=
[
1
,
2
,
3
,
4
,
5
]
quantiles_value
=
statistics.
quantiles
(
data
,
n
=
4
)
(
"Quartiles:"
,
quantiles_value
)
Output:
四分位数: [1.5, 3.0, 4.5]
math module methods
| Method | Description |
|---|---|
| statistics.harmonic_mean() | Compute the harmonic mean of a given data set. |
| statistics.mean() | Compute the mean of a data set |
| statistics.median() | Compute the median of a data set |
| statistics.median_grouped() | Compute the grouped median of a given grouped data set |
| statistics.median_high() | Compute the high median of a given data set |
| statistics.median_low() | Compute the low median of a given data set. |
| statistics.mode() | Compute the mode of a data set (the most frequent value) |
| statistics.pstdev() | Compute the sample standard deviation of a given data set |
| statistics.stdev() | Compute the standard deviation of a data set |
| statistics.pvariance() | Compute the sample variance of a given data set |
| statistics.variance() | Compute the variance of a data set |
| statistics.quantiles() | Compute the quantiles of a data set; the number of quantiles can be specified (quartiles by default) |