statistics
 数学统计函数¶
3.4 新版功能.
源代码: Lib/statistics.py
该模块提供了用于计算数字 (Real
valued) 数据的数理统计量的函数。
此模块并不是诸如 NumPy ， SciPy 等第三方库或者诸如 Minitab ， SAS ， Matlab 等针对专业统计学家的专有全功能统计软件包的竟品。此模块针对图形和科学计算器的水平。
除非明确注释，这些函数支持 int
， float
， Decimal
和 Fraction
。当前不支持同其他类型（是否在数字塔中）的行为。混合类型的集合也是未定义的，并且依赖于实现。如果你输入的数据由混合类型组成，你应该能够使用 map()
来确保一个一致的结果，比如： map(float, input_data)
。
中心位置的平均值和度量¶
这些函数计算一个整体或样本的平均值或者特定值
数据的算术平均数（“平均数”）。 

快速的，浮点算数平均数。 

数据的几何平均数 

数据的调和均值 

数据的中位数（中间值） 

数据的低中位数 

数据的高中位数 

分组数据的中位数，即第50个百分点。 

离散的或标称的数据的单模（最常见的值）。 

离散的或标称的数据的模式列表（最常见的值）。 

将数据以相等的概率分为多个间隔。 
函数细节¶
注释：这些函数不需要对提供给它们的数据进行排序。但是，为了方便阅读，大多数例子展示的是已排序的序列。

statistics.
mean
(data)¶ 返回 数据 的样本算术平均数，数据可以是一个序列或迭代器。
算术平均数是数据之和与数据点个数的商。通常称作“平均数”，尽管它指示诸多数学平均数之一。它是数据的中心位置的度量。
若 data 为空，将会引发
StatisticsError
。一些用法示例：
>>> mean([1, 2, 3, 4, 4]) 2.8 >>> mean([1.0, 2.5, 3.25, 5.75]) 2.625 >>> from fractions import Fraction as F >>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)]) Fraction(13, 21) >>> from decimal import Decimal as D >>> mean([D("0.5"), D("0.75"), D("0.625"), D("0.375")]) Decimal('0.5625')

statistics.
fmean
(data)¶ 将浮点数转换成 data 并且计算算术平均数。
它的运行速度比
mean()
函数快并且它始终返回float
。 data 应该是个序列或迭代器。如果输入的数据集合失控的，此函数将会引发StatisticsError
。>>> fmean([3.5, 4.0, 5.25]) 4.25
3.8 新版功能.

statistics.
geometric_mean
(data)¶ 将浮点数转换成 data 并且计算几何平均数。
几何平均值使用值的乘积表示 数据 的中心趋势或典型值（与使用它们的总和的算术平均值相反）。
如果输入数据集是空的则引发
StatisticsError
，如果它包含一个零 或者包含一个负值。 数据 可以是一个序列或迭代器。无需做出特殊努力即可获得准确的结果。（但是，将来或许会修改。）
>>> round(geometric_mean([54, 24, 36]), 1) 36.0
3.8 新版功能.

statistics.
harmonic_mean
(data)¶ 返回 data 的调和均值，数据可以是序列或实数值的迭代器。
调和均值,也叫次相反均值，所有数据的倒数的算术平均数
mean()
的倒数。比如说，数据 a ， b ， c 的调和均值等于3/(1/a + 1/b + 1/c)
。调和均值是一种均值类型，是数据中心位置的度量。它通常适合于求比率和比例的平均值，比如速率。
假设一辆车在 40 km/hr 的速度下行驶了 10 km ，然后又以 60 km/hr 的速度行驶了 10 km 。车辆的平均速率是多少？
>>> harmonic_mean([40, 60]) 48.0
假设一名投资者在三家公司各购买了等价值的股票，以 2.5， 3 ， 10 的 P/E (价格/收益) 率。投资者投资组合的平均市盈率是多少？
>>> harmonic_mean([2.5, 3, 10]) # For an equal investment portfolio. 3.6
如果 data 为空或者 任何一个元素的值小于零，会引发
StatisticsError
。3.6 新版功能.

statistics.
median
(data)¶ Return the median (middle value) of numeric data, using the common "mean of middle two" method. If data is empty,
StatisticsError
is raised. data can be a sequence or iterator.The median is a robust measure of central location and is less affected by the presence of outliers. When the number of data points is odd, the middle data point is returned:
>>> median([1, 3, 5]) 3
When the number of data points is even, the median is interpolated by taking the average of the two middle values:
>>> median([1, 3, 5, 7]) 4.0
This is suited for when your data is discrete, and you don't mind that the median may not be an actual data point.
If the data is ordinal (supports order operations) but not numeric (doesn't support addition), consider using
median_low()
ormedian_high()
instead.

statistics.
median_low
(data)¶ Return the low median of numeric data. If data is empty,
StatisticsError
is raised. data can be a sequence or iterator.The low median is always a member of the data set. When the number of data points is odd, the middle value is returned. When it is even, the smaller of the two middle values is returned.
>>> median_low([1, 3, 5]) 3 >>> median_low([1, 3, 5, 7]) 3
Use the low median when your data are discrete and you prefer the median to be an actual data point rather than interpolated.

statistics.
median_high
(data)¶ Return the high median of data. If data is empty,
StatisticsError
is raised. data can be a sequence or iterator.The high median is always a member of the data set. When the number of data points is odd, the middle value is returned. When it is even, the larger of the two middle values is returned.
>>> median_high([1, 3, 5]) 3 >>> median_high([1, 3, 5, 7]) 5
Use the high median when your data are discrete and you prefer the median to be an actual data point rather than interpolated.

statistics.
median_grouped
(data, interval=1)¶ Return the median of grouped continuous data, calculated as the 50th percentile, using interpolation. If data is empty,
StatisticsError
is raised. data can be a sequence or iterator.>>> median_grouped([52, 52, 53, 54]) 52.5
In the following example, the data are rounded, so that each value represents the midpoint of data classes, e.g. 1 is the midpoint of the class 0.51.5, 2 is the midpoint of 1.52.5, 3 is the midpoint of 2.53.5, etc. With the data given, the middle value falls somewhere in the class 3.54.5, and interpolation is used to estimate it:
>>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5]) 3.7
Optional argument interval represents the class interval, and defaults to 1. Changing the class interval naturally will change the interpolation:
>>> median_grouped([1, 3, 3, 5, 7], interval=1) 3.25 >>> median_grouped([1, 3, 3, 5, 7], interval=2) 3.5
This function does not check whether the data points are at least interval apart.
CPython implementation detail: Under some circumstances,
median_grouped()
may coerce data points to floats. This behaviour is likely to change in the future.参见
"Statistics for the Behavioral Sciences", Frederick J Gravetter and Larry B Wallnau (8th Edition).
The SSMEDIAN function in the Gnome Gnumeric spreadsheet, including this discussion.

statistics.
mode
(data)¶ Return the single most common data point from discrete or nominal data. The mode (when it exists) is the most typical value and serves as a measure of central location.
If there are multiple modes with the same frequency, returns the first one encountered in the data. If the smallest or largest of those is desired instead, use
min(multimode(data))
ormax(multimode(data))
. If the input data is empty,StatisticsError
is raised.mode
assumes discrete data and returns a single value. This is the standard treatment of the mode as commonly taught in schools:>>> mode([1, 1, 2, 3, 3, 3, 3, 4]) 3
The mode is unique in that it is the only statistic in this package that also applies to nominal (nonnumeric) data:
>>> mode(["red", "blue", "blue", "red", "green", "red", "red"]) 'red'
在 3.8 版更改: Now handles multimodal datasets by returning the first mode encountered. Formerly, it raised
StatisticsError
when more than one mode was found.

statistics.
multimode
(data)¶ Return a list of the most frequently occurring values in the order they were first encountered in the data. Will return more than one result if there are multiple modes or an empty list if the data is empty:
>>> multimode('aabbbbccddddeeffffgg') ['b', 'd', 'f'] >>> multimode('') []
3.8 新版功能.

statistics.
pstdev
(data, mu=None)¶ Return the population standard deviation (the square root of the population variance). See
pvariance()
for arguments and other details.>>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]) 0.986893273527251

statistics.
pvariance
(data, mu=None)¶ Return the population variance of data, a nonempty sequence or iterator of realvalued numbers. Variance, or second moment about the mean, is a measure of the variability (spread or dispersion) of data. A large variance indicates that the data is spread out; a small variance indicates it is clustered closely around the mean.
If the optional second argument mu is given, it is typically the mean of the data. It can also be used to compute the second moment around a point that is not the mean. If it is missing or
None
(the default), the arithmetic mean is automatically calculated.Use this function to calculate the variance from the entire population. To estimate the variance from a sample, the
variance()
function is usually a better choice.Raises
StatisticsError
if data is empty.示例：
>>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25] >>> pvariance(data) 1.25
If you have already calculated the mean of your data, you can pass it as the optional second argument mu to avoid recalculation:
>>> mu = mean(data) >>> pvariance(data, mu) 1.25
Decimals and Fractions are supported:
>>> from decimal import Decimal as D >>> pvariance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")]) Decimal('24.815') >>> from fractions import Fraction as F >>> pvariance([F(1, 4), F(5, 4), F(1, 2)]) Fraction(13, 72)
注解
When called with the entire population, this gives the population variance σ². When called on a sample instead, this is the biased sample variance s², also known as variance with N degrees of freedom.
If you somehow know the true population mean μ, you may use this function to calculate the variance of a sample, giving the known population mean as the second argument. Provided the data points are a random sample of the population, the result will be an unbiased estimate of the population variance.

statistics.
stdev
(data, xbar=None)¶ Return the sample standard deviation (the square root of the sample variance). See
variance()
for arguments and other details.>>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]) 1.0810874155219827

statistics.
variance
(data, xbar=None)¶ Return the sample variance of data, an iterable of at least two realvalued numbers. Variance, or second moment about the mean, is a measure of the variability (spread or dispersion) of data. A large variance indicates that the data is spread out; a small variance indicates it is clustered closely around the mean.
If the optional second argument xbar is given, it should be the mean of data. If it is missing or
None
(the default), the mean is automatically calculated.Use this function when your data is a sample from a population. To calculate the variance from the entire population, see
pvariance()
.Raises
StatisticsError
if data has fewer than two values.示例：
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5] >>> variance(data) 1.3720238095238095
If you have already calculated the mean of your data, you can pass it as the optional second argument xbar to avoid recalculation:
>>> m = mean(data) >>> variance(data, m) 1.3720238095238095
This function does not attempt to verify that you have passed the actual mean as xbar. Using arbitrary values for xbar can lead to invalid or impossible results.
Decimal and Fraction values are supported:
>>> from decimal import Decimal as D >>> variance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")]) Decimal('31.01875') >>> from fractions import Fraction as F >>> variance([F(1, 6), F(1, 2), F(5, 3)]) Fraction(67, 108)
注解
This is the sample variance s² with Bessel's correction, also known as variance with N1 degrees of freedom. Provided that the data points are representative (e.g. independent and identically distributed), the result should be an unbiased estimate of the true population variance.
If you somehow know the actual population mean μ you should pass it to the
pvariance()
function as the mu parameter to get the variance of a sample.

statistics.
quantiles
(data, *, n=4, method='exclusive')¶ Divide data into n continuous intervals with equal probability. Returns a list of
n  1
cut points separating the intervals.Set n to 4 for quartiles (the default). Set n to 10 for deciles. Set n to 100 for percentiles which gives the 99 cuts points that separate data into 100 equal sized groups. Raises
StatisticsError
if n is not least 1.The data can be any iterable containing sample data. For meaningful results, the number of data points in data should be larger than n. Raises
StatisticsError
if there are not at least two data points.The cut points are linearly interpolated from the two nearest data points. For example, if a cut point falls onethird of the distance between two sample values,
100
and112
, the cutpoint will evaluate to104
.The method for computing quantiles can be varied depending on whether the data includes or excludes the lowest and highest possible values from the population.
The default method is "exclusive" and is used for data sampled from a population that can have more extreme values than found in the samples. The portion of the population falling below the ith of m sorted data points is computed as
i / (m + 1)
. Given nine sample values, the method sorts them and assigns the following percentiles: 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%.Setting the method to "inclusive" is used for describing population data or for samples that are known to include the most extreme values from the population. The minimum value in data is treated as the 0th percentile and the maximum value is treated as the 100th percentile. The portion of the population falling below the ith of m sorted data points is computed as
(i  1) / (m  1)
. Given 11 sample values, the method sorts them and assigns the following percentiles: 0%, 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%, 100%.# Decile cut points for empirically sampled data >>> data = [105, 129, 87, 86, 111, 111, 89, 81, 108, 92, 110, ... 100, 75, 105, 103, 109, 76, 119, 99, 91, 103, 129, ... 106, 101, 84, 111, 74, 87, 86, 103, 103, 106, 86, ... 111, 75, 87, 102, 121, 111, 88, 89, 101, 106, 95, ... 103, 107, 101, 81, 109, 104] >>> [round(q, 1) for q in quantiles(data, n=10)] [81.0, 86.2, 89.0, 99.4, 102.5, 103.6, 106.0, 109.8, 111.0]
3.8 新版功能.
异常¶
A single exception is defined:

exception
statistics.
StatisticsError
¶ Subclass of
ValueError
for statisticsrelated exceptions.
NormalDist
objects¶
NormalDist
is a tool for creating and manipulating normal
distributions of a random variable. It is a
class that treats the mean and standard deviation of data
measurements as a single entity.
Normal distributions arise from the Central Limit Theorem and have a wide range of applications in statistics.

class
statistics.
NormalDist
(mu=0.0, sigma=1.0)¶ Returns a new NormalDist object where mu represents the arithmetic mean and sigma represents the standard deviation.
If sigma is negative, raises
StatisticsError
.
mean
¶ A readonly property for the arithmetic mean of a normal distribution.

stdev
¶ A readonly property for the standard deviation of a normal distribution.

variance
¶ A readonly property for the variance of a normal distribution. Equal to the square of the standard deviation.

classmethod
from_samples
(data)¶ Makes a normal distribution instance with mu and sigma parameters estimated from the data using
fmean()
andstdev()
.The data can be any iterable and should consist of values that can be converted to type
float
. If data does not contain at least two elements, raisesStatisticsError
because it takes at least one point to estimate a central value and at least two points to estimate dispersion.

samples
(n, *, seed=None)¶ Generates n random samples for a given mean and standard deviation. Returns a
list
offloat
values.If seed is given, creates a new instance of the underlying random number generator. This is useful for creating reproducible results, even in a multithreading context.

pdf
(x)¶ Using a probability density function (pdf), compute the relative likelihood that a random variable X will be near the given value x. Mathematically, it is the limit of the ratio
P(x <= X < x+dx) / dx
as dx approaches zero.The relative likelihood is computed as the probability of a sample occurring in a narrow range divided by the width of the range (hence the word "density"). Since the likelihood is relative to other points, its value can be greater than 1.0.

cdf
(x)¶ Using a cumulative distribution function (cdf), compute the probability that a random variable X will be less than or equal to x. Mathematically, it is written
P(X <= x)
.

inv_cdf
(p)¶ Compute the inverse cumulative distribution function, also known as the quantile function or the percentpoint function. Mathematically, it is written
x : P(X <= x) = p
.Finds the value x of the random variable X such that the probability of the variable being less than or equal to that value equals the given probability p.

overlap
(other)¶ Measures the agreement between two normal probability distributions. Returns a value between 0.0 and 1.0 giving the overlapping area for the two probability density functions.

quantiles
(n=4)¶ Divide the normal distribution into n continuous intervals with equal probability. Returns a list of (n  1) cut points separating the intervals.
Set n to 4 for quartiles (the default). Set n to 10 for deciles. Set n to 100 for percentiles which gives the 99 cuts points that separate the normal distribution into 100 equal sized groups.
Instances of
NormalDist
support addition, subtraction, multiplication and division by a constant. These operations are used for translation and scaling. For example:>>> temperature_february = NormalDist(5, 2.5) # Celsius >>> temperature_february * (9/5) + 32 # Fahrenheit NormalDist(mu=41.0, sigma=4.5)
Dividing a constant by an instance of
NormalDist
is not supported because the result wouldn't be normally distributed.Since normal distributions arise from additive effects of independent variables, it is possible to add and subtract two independent normally distributed random variables represented as instances of
NormalDist
. For example:>>> birth_weights = NormalDist.from_samples([2.5, 3.1, 2.1, 2.4, 2.7, 3.5]) >>> drug_effects = NormalDist(0.4, 0.15) >>> combined = birth_weights + drug_effects >>> round(combined.mean, 1) 3.1 >>> round(combined.stdev, 1) 0.5
3.8 新版功能.

NormalDist
Examples and Recipes¶
NormalDist
readily solves classic probability problems.
For example, given historical data for SAT exams showing that scores are normally distributed with a mean of 1060 and a standard deviation of 192, determine the percentage of students with test scores between 1100 and 1200, after rounding to the nearest whole number:
>>> sat = NormalDist(1060, 195)
>>> fraction = sat.cdf(1200 + 0.5)  sat.cdf(1100  0.5)
>>> round(fraction * 100.0, 1)
18.4
Find the quartiles and deciles for the SAT scores:
>>> list(map(round, sat.quantiles()))
[928, 1060, 1192]
>>> list(map(round, sat.quantiles(n=10)))
[810, 896, 958, 1011, 1060, 1109, 1162, 1224, 1310]
To estimate the distribution for a model than isn't easy to solve
analytically, NormalDist
can generate input samples for a Monte
Carlo simulation:
>>> def model(x, y, z):
... return (3*x + 7*x*y  5*y) / (11 * z)
...
>>> n = 100_000
>>> X = NormalDist(10, 2.5).samples(n, seed=3652260728)
>>> Y = NormalDist(15, 1.75).samples(n, seed=4582495471)
>>> Z = NormalDist(50, 1.25).samples(n, seed=6582483453)
>>> quantiles(map(model, X, Y, Z))
[1.4591308524824727, 1.8035946855390597, 2.175091447274739]
Normal distributions commonly arise in machine learning problems.
Wikipedia has a nice example of a Naive Bayesian Classifier. The challenge is to predict a person's gender from measurements of normally distributed features including height, weight, and foot size.
We're given a training dataset with measurements for eight people. The
measurements are assumed to be normally distributed, so we summarize the data
with NormalDist
:
>>> height_male = NormalDist.from_samples([6, 5.92, 5.58, 5.92])
>>> height_female = NormalDist.from_samples([5, 5.5, 5.42, 5.75])
>>> weight_male = NormalDist.from_samples([180, 190, 170, 165])
>>> weight_female = NormalDist.from_samples([100, 150, 130, 150])
>>> foot_size_male = NormalDist.from_samples([12, 11, 12, 10])
>>> foot_size_female = NormalDist.from_samples([6, 8, 7, 9])
Next, we encounter a new person whose feature measurements are known but whose gender is unknown:
>>> ht = 6.0 # height
>>> wt = 130 # weight
>>> fs = 8 # foot size
Starting with a 50% prior probability of being male or female, we compute the posterior as the prior times the product of likelihoods for the feature measurements given the gender:
>>> prior_male = 0.5
>>> prior_female = 0.5
>>> posterior_male = (prior_male * height_male.pdf(ht) *
... weight_male.pdf(wt) * foot_size_male.pdf(fs))
>>> posterior_female = (prior_female * height_female.pdf(ht) *
... weight_female.pdf(wt) * foot_size_female.pdf(fs))
The final prediction goes to the largest posterior. This is known as the maximum a posteriori or MAP:
>>> 'male' if posterior_male > posterior_female else 'female'
'female'