新聞中心
statistics —- 數(shù)學統(tǒng)計函數(shù)
3.4 新版功能.

專注于為中小企業(yè)提供成都網(wǎng)站設計、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)溫宿免費做網(wǎng)站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。
源代碼:Lib/statistics.py
該模塊提供了用于計算數(shù)字 (Real-valued) 數(shù)據(jù)的數(shù)理統(tǒng)計量的函數(shù)。
此模塊并不是諸如 NumPy , SciPy 等第三方庫或者諸如 Minitab , SAS , Matlab 等針對專業(yè)統(tǒng)計學家的專有全功能統(tǒng)計軟件包的競品。此模塊針對圖形和科學計算器的水平。
除非明確注釋,這些函數(shù)支持 int , float , Decimal 和 Fraction 。當前不支持同其他類型(是否在數(shù)字塔中)的行為。混合類型的集合也是未定義的,并且依賴于實現(xiàn)。如果你輸入的數(shù)據(jù)由混合類型組成,你應該能夠使用 map() 來確保一個一致的結果,比如: map(float, input_data) 。
Some datasets use NaN (not a number) values to represent missing data. Since NaNs have unusual comparison semantics, they cause surprising or undefined behaviors in the statistics functions that sort data or that count occurrences. The functions affected are median(), median_low(), median_high(), median_grouped(), mode(), multimode(), and quantiles(). The NaN values should be stripped before calling these functions:
>>> from statistics import median>>> from math import isnan>>> from itertools import filterfalse>>> data = [20.7, float('NaN'),19.2, 18.3, float('NaN'), 14.4]>>> sorted(data) # This has surprising behavior[20.7, nan, 14.4, 18.3, 19.2, nan]>>> median(data) # This result is unexpected16.35>>> sum(map(isnan, data)) # Number of missing values2>>> clean = list(filterfalse(isnan, data)) # Strip NaN values>>> clean[20.7, 19.2, 18.3, 14.4]>>> sorted(clean) # Sorting now works as expected[14.4, 18.3, 19.2, 20.7]>>> median(clean) # This result is now well defined18.75
平均值以及對中心位置的評估
這些函數(shù)用于計算一個總體或樣本的平均值或者典型值。
|
mean() |
數(shù)據(jù)的算術平均數(shù)(“平均數(shù)”)。 |
|
fmean() |
Fast, floating point arithmetic mean, with optional weighting. |
|
geometric_mean() |
數(shù)據(jù)的幾何平均數(shù) |
|
harmonic_mean() |
數(shù)據(jù)的調和均值 |
|
median() |
數(shù)據(jù)的中位數(shù)(中間值) |
|
median_low() |
數(shù)據(jù)的低中位數(shù) |
|
median_high() |
數(shù)據(jù)的高中位數(shù) |
|
median_grouped() |
分組數(shù)據(jù)的中位數(shù),即第50個百分點。 |
|
mode() |
離散的或標稱的數(shù)據(jù)的單個眾數(shù)(出現(xiàn)最多的值)。 |
|
multimode() |
離散的或標稱的數(shù)據(jù)的眾數(shù)(出現(xiàn)最多的值)列表。 |
|
quantiles() |
將數(shù)據(jù)以相等的概率分為多個間隔。 |
對分散程度的評估
這些函數(shù)用于計算總體或樣本與典型值或平均值的偏離程度。
|
pstdev() |
數(shù)據(jù)的總體標準差 |
|
pvariance() |
數(shù)據(jù)的總體方差 |
|
stdev() |
數(shù)據(jù)的樣本標準差 |
|
variance() |
數(shù)據(jù)的樣本方差 |
對兩個輸入之間關系的統(tǒng)計
這些函數(shù)計算兩個輸入之間關系的統(tǒng)計值。
|
covariance() |
兩個變量的樣本協(xié)方差。 |
|
correlation() |
兩個變量的皮爾遜相關系數(shù)。 |
|
linear_regression() |
簡單線性回歸的斜率和截距。 |
函數(shù)細節(jié)
注釋:這些函數(shù)不需要對提供給它們的數(shù)據(jù)進行排序。但是,為了方便閱讀,大多數(shù)例子展示的是已排序的序列。
statistics.mean(data)
返回 data 的樣本算術平均數(shù),形式為序列或迭代器。
算術平均數(shù)是數(shù)據(jù)之和與數(shù)據(jù)點個數(shù)的商。通常稱作“平均數(shù)”,盡管它指示諸多數(shù)學平均數(shù)之一。它是數(shù)據(jù)的中心位置的度量。
若 data 為空,將會引發(fā) 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')
備注
平均數(shù)會受到 異常值 的強烈影響因而不一定能作為數(shù)據(jù)點的典型樣本。 想獲得對于 集中趨勢 的更可靠的度量,可以參看 median(),但其效率要低一些。
樣本均值給出了一個無偏向的真實總體均值的估計,因此當平均抽取所有可能的樣本, mean(sample) 收斂于整個總體的真實均值。如果 data 代表整個總體而不是樣本,那么 mean(data) 等同于計算真實整體均值 μ 。
statistics.fmean(data, weights=None)
將 data 轉換成浮點數(shù)并且計算算術平均數(shù)。
此函數(shù)的運行速度比 mean() 函數(shù)快并且它總是返回一個 float。 data 可以為序列或可迭代對象。 如果輸入數(shù)據(jù)集為空,則會引發(fā) StatisticsError。
>>> fmean([3.5, 4.0, 5.25])4.25
Optional weighting is supported. For example, a professor assigns a grade for a course by weighting quizzes at 20%, homework at 20%, a midterm exam at 30%, and a final exam at 30%:
>>> grades = [85, 92, 83, 91]>>> weights = [0.20, 0.20, 0.30, 0.30]>>> fmean(grades, weights)87.6
If weights is supplied, it must be the same length as the data or a ValueError will be raised.
3.8 新版功能.
在 3.11 版更改: 添加了對 weights 的支持。
statistics.geometric_mean(data)
將 data 轉換成浮點數(shù)并且計算幾何平均數(shù)。
幾何平均值使用值的乘積表示 數(shù)據(jù) 的中心趨勢或典型值(與使用它們的總和的算術平均值相反)。
如果輸入數(shù)據(jù)集為空、包含零或包含負值則將引發(fā) StatisticsError。 data 可以是序列或可迭代對象。
無需做出特殊努力即可獲得準確的結果。(但是,將來或許會修改。)
>>> round(geometric_mean([54, 24, 36]), 1)36.0
3.8 新版功能.
statistics.harmonic_mean(data, weights=None)
返回包含實數(shù)值的序列或可迭代對象 data 的調和平均數(shù)。 如果 weights 被省略或為 None,則會假定權重相等。
調和平均數(shù)是數(shù)據(jù)的倒數(shù)的算術平均值 mean() 的倒數(shù)。 例如,三個數(shù)值 a, b 和 c 的調和平均數(shù)將等于 3/(1/a + 1/b + 1/c)。 如果其中一個值為零,則結果也將為零。
調和平均數(shù)是均值的一種,是對數(shù)據(jù)的中心位置的度量。 它通常適用于求比率和比例(如速度)的均值。
假設一輛車在 40 km/hr 的速度下行駛了 10 km ,然后又以 60 km/hr 的速度行駛了 10 km 。車輛的平均速率是多少?
>>> harmonic_mean([40, 60])48.0
假設一輛汽車以速度 40 公里/小時行駛了 5 公里,當?shù)缆纷兊脮惩ê螅崴俚?60 公里/小時行駛了行程中剩余的 30 km。 請問其平均速度是多少?
>>> harmonic_mean([40, 60], weights=[5, 30])56.0
如果 data 為空、任意元素小于零,或者加權匯總值不為正數(shù)則會引發(fā) StatisticsError。
當前算法在輸入中遇到零時會提前退出。這意味著不會測試后續(xù)輸入的有效性。(此行為將來可能會更改。)
3.6 新版功能.
在 3.10 版更改: 添加了對 weights 的支持。
statistics.median(data)
使用普通的“取中間兩數(shù)平均值”方法返回數(shù)值數(shù)據(jù)的中位數(shù)(中間值)。 如果 data 為空,則將引發(fā) StatisticsError。 data 可以是序列或可迭代對象。
中位數(shù)是衡量中間位置的可靠方式,并且較少受到極端值的影響。 當數(shù)據(jù)點的總數(shù)為奇數(shù)時,將返回中間數(shù)據(jù)點:
>>> median([1, 3, 5])3
當數(shù)據(jù)點的總數(shù)為偶數(shù)時,中位數(shù)將通過對兩個中間值求平均進行插值得出:
>>> median([1, 3, 5, 7])4.0
這適用于當你的數(shù)據(jù)是離散的,并且你不介意中位數(shù)不是實際數(shù)據(jù)點的情況。
如果數(shù)據(jù)是有序的(支持排序操作)但不是數(shù)字(不支持加法),請考慮改用 median_low() 或 median_high()。
statistics.median_low(data)
返回數(shù)值數(shù)據(jù)的低中位數(shù)。 如果 data 為空則將引發(fā) StatisticsError。 data 可以是序列或可迭代對象。
低中位數(shù)一定是數(shù)據(jù)集的成員。 當數(shù)據(jù)點總數(shù)為奇數(shù)時,將返回中間值。 當其為偶數(shù)時,將返回兩個中間值中較小的那個。
>>> median_low([1, 3, 5])3>>> median_low([1, 3, 5, 7])3
當你的數(shù)據(jù)是離散的,并且你希望中位數(shù)是一個實際數(shù)據(jù)點而非插值結果時可以使用低中位數(shù)。
statistics.median_high(data)
返回數(shù)據(jù)的高中位數(shù)。 如果 data 為空則將引發(fā) StatisticsError。 data 可以是序列或可迭代對象。
高中位數(shù)一定是數(shù)據(jù)集的成員。 當數(shù)據(jù)點總數(shù)為奇數(shù)時,將返回中間值。 當其為偶數(shù)時,將返回兩個中間值中較大的那個。
>>> median_high([1, 3, 5])3>>> median_high([1, 3, 5, 7])5
當你的數(shù)據(jù)是離散的,并且你希望中位數(shù)是一個實際數(shù)據(jù)點而非插值結果時可以使用高中位數(shù)。
statistics.median_grouped(data, interval=1)
返回分組的連續(xù)數(shù)據(jù)的中位數(shù),根據(jù)第 50 個百分點的位置使用插值來計算。 如果 data 為空則將引發(fā) StatisticsError。 data 可以是序列或可迭代對象。
>>> median_grouped([52, 52, 53, 54])52.5
在下面的示例中,數(shù)據(jù)已經(jīng)過舍入,這樣每個值都代表數(shù)據(jù)分類的中間點,例如 1 是 0.5—1.5 分類的中間點,2 是 1.5—2.5 分類的中間點,3 是 2.5—3.5 的中間點等待。 根據(jù)給定的數(shù)據(jù),中間值應落在 3.5—4.5 分類之內,并可使用插值法來進行估算:
>>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])3.7
可選參數(shù) interval 表示分類間隔,默認值為 1。 改變分類間隔自然會改變插件結果:
>>> median_grouped([1, 3, 3, 5, 7], interval=1)3.25>>> median_grouped([1, 3, 3, 5, 7], interval=2)3.5
此函數(shù)不會檢查數(shù)據(jù)點之間是否至少相隔 interval 的距離。
Cpython 實現(xiàn)細節(jié):在某些情況下,median_grouped() 可以會將數(shù)據(jù)點強制轉換為浮點數(shù)。 此行為在未來有可能會發(fā)生改變。
參見
-
“Statistics for the Behavioral Sciences”, Frederick J Gravetter and Larry B Wallnau (8th Edition).
-
Gnome Gnumeric 電子表格中的 SSMEDIAN 函數(shù),包括 這篇討論。
statistics.mode(data)
從離散或標稱的 data 返回單個出現(xiàn)最多的數(shù)據(jù)點。 此眾數(shù)(如果存在)是最典型的值,并可用來度量中心的位置。
如果存在具有相同頻率的多個眾數(shù),則返回在 data 中遇到的第一個。 如果想要其中最小或最大的一個,請使用 min(multimode(data)) 或 max(multimode(data))。 如果輸入的 data 為空,則會引發(fā) StatisticsError。
mode 將假定是離散數(shù)據(jù)并返回一個單一的值。 這是通常的學校教學中標準的處理方式:
>>> mode([1, 1, 2, 3, 3, 3, 3, 4])3
此眾數(shù)的獨特之處在于它是這個包中唯一還可應用于標稱(非數(shù)字)數(shù)據(jù)的統(tǒng)計信息:
>>> mode(["red", "blue", "blue", "red", "green", "red", "red"])'red'
在 3.8 版更改: 現(xiàn)在會通過返回所遇到的第一個眾數(shù)來處理多模數(shù)據(jù)集。 之前它會在遇到超過一個的眾數(shù)時引發(fā) StatisticsError。
statistics.multimode(data)
返回最頻繁出現(xiàn)的值的列表,并按它們在 data 中首次出現(xiàn)的位置排序。 如果存在多個眾數(shù)則將返回一個以上的眾數(shù),或者如果 data 為空則將返回空列表:
>>> multimode('aabbbbccddddeeffffgg')['b', 'd', 'f']>>> multimode('')[]
3.8 新版功能.
statistics.pstdev(data, mu=None)
返回總體標準差(總體方差的平方根)。 請參閱 pvariance() 了解參數(shù)和其他細節(jié)。
>>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])0.986893273527251
statistics.pvariance(data, mu=None)
返回非空序列或包含實數(shù)值的可迭代對象 data 的總體方差。 方差或稱相對于均值的二階距,是對數(shù)據(jù)變化幅度(延展度或分散度)的度量。 方差值較大表明數(shù)據(jù)的散布范圍較大;方差值較小表明它緊密聚集于均值附近。
如果給出了可選的第二個參數(shù) mu,它通常是 data 的均值。 它也可以被用來計算相對于一個非均值點的二階距。 如果該參數(shù)省略或為 None (默認值),則會自動進行算術均值的計算。
使用此函數(shù)可根據(jù)所有數(shù)值來計算方差。 要根據(jù)一個樣本來估算方差,通常 variance() 函數(shù)是更好的選擇。
如果 data 為空則會引發(fā) StatisticsError。
示例:
>>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]>>> pvariance(data)1.25
如果你已經(jīng)計算過數(shù)據(jù)的平均值,你可以將其作為可選的第二個參數(shù) mu 傳入以避免重復計算:
>>> mu = mean(data)>>> pvariance(data, mu)1.25
同樣也支持使用 Decimal 和 Fraction 值:
>>> 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)
備注
當調用時附帶完整的總體數(shù)據(jù)時,這將給出總體方差 σ2。 而當調用時只附帶一個樣本時,這將給出偏置樣本方差 s2,也被稱為帶有 N 個自由度的方差。
如果你通過某種方式知道了真實的總體平均值 μ,則可以使用此函數(shù)來計算一個樣本的方差,并將已知的總體平均值作為第二個參數(shù)。 假設數(shù)據(jù)點是總體的一個隨機樣本,則結果將為總體方差的無偏估計值。
statistics.stdev(data, xbar=None)
返回樣本標準差(樣本方差的平方根)。 請參閱 variance() 了解參數(shù)和其他細節(jié)。
>>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])1.0810874155219827
statistics.variance(data, xbar=None)
返回包含至少兩個實數(shù)值的可迭代對象 data 的樣本方差。 方差或稱相對于均值的二階矩,是對數(shù)據(jù)變化幅度(延展度或分散度)的度量。 方差值較大表明數(shù)據(jù)的散布范圍較大;方差值較小表明它緊密聚集于均值附近。
如果給出了可選的第二個參數(shù) xbar,它應當是 data 的均值。 如果該參數(shù)省略或為 None (默認值),則會自動進行均值的計算。
當你的數(shù)據(jù)是總體數(shù)據(jù)的樣本時請使用此函數(shù)。 要根據(jù)整個總體數(shù)據(jù)來計算方差,請參見 pvariance()。
如果 data 包含的值少于兩個則會引發(fā) StatisticsError。
示例:
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]>>> variance(data)1.3720238095238095
如果你已經(jīng)計算過數(shù)據(jù)的平均值,你可以將其作為可選的第二個參數(shù) xbar 傳入以避免重復計算:
>>> m = mean(data)>>> variance(data, m)1.3720238095238095
此函數(shù)不會試圖檢查你所傳入的 xbar 是否為真實的平均值。 使用任意值作為 xbar 可能導致無效或不可能的結果。
同樣也支持使用 Decimal 和 Fraction 值:
>>> 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)
備注
這是附帶貝塞爾校正的樣本方差 s2,也稱為具有 N-1 自由度的方差。 假設數(shù)據(jù)點具有代表性(即為獨立且均勻的分布),則結果應當是對總體方差的無偏估計。
如果你通過某種方式知道了真實的總體平均值 μ 則應當調用 pvariance() 函數(shù)并將該值作為 mu 形參傳入以得到一個樣本的方差。
statistics.quantiles(data, **, n=4, method=’exclusive’*)
將 data 分隔為具有相等概率的 n 個連續(xù)區(qū)間。 返回分隔這些區(qū)間的 n - 1 個分隔點的列表。
將 n 設為 4 以使用四分位(默認值)。 將 n 設為 10 以使用十分位。 將 n 設為 100 以使用百分位,即給出 99 個分隔點來將 data 分隔為 100 個大小相等的組。 如果 n 小于 1 則將引發(fā) StatisticsError。
data 可以是包含樣本數(shù)據(jù)的任意可迭代對象。 為了獲得有意義的結果,data 中數(shù)據(jù)點的數(shù)量應當大于 n。 如果數(shù)據(jù)點的數(shù)量小于兩個則將引發(fā) StatisticsError。
分隔點是通過對兩個最接近的數(shù)據(jù)點進行線性插值得到的。 例如,如果一個分隔點落在兩個樣本值 100 和 112 之間距離三分之一的位置,則分隔點的取值將為 104。
method 用于計算分位值,它會由于 data 是包含還是排除總體的最低和最高可能值而有所不同。
默認 method 是 “唯一的” 并且被用于在總體中數(shù)據(jù)采樣這樣可以有比樣本中找到的更多的極端值。落在 m 個排序數(shù)據(jù)點的第 i-th 個以下的總體部分被計算為 i / (m + 1) 。給定九個樣本值,方法排序它們并且分配一下的百分位: 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90% 。
將 method 設為 “inclusive” 可用于描述總體數(shù)據(jù)或已明確知道包含有總體數(shù)據(jù)中最極端值的樣本。 data 中的最小值會被作為第 0 個百分位而最大值會被作為第 100 個百分位。 總體數(shù)據(jù)里處于 m 個已排序數(shù)據(jù)點中 第 i 個 以下的部分會以 (i - 1) / (m - 1) 來計算。 給定 11 個樣本值,該方法會對它們進行排序并賦予以下百分位: 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 新版功能.
statistics.covariance(x, y, /)
返回兩個輸入 x 和 y 的樣本協(xié)方差。 樣本協(xié)方差是對兩個輸入的同步變化性的度量。
兩個輸入必須具有相同的長度(不少于兩個元素),否則會引發(fā) StatisticsError。
示例:
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]>>> covariance(x, y)0.75>>> z = [9, 8, 7, 6, 5, 4, 3, 2, 1]>>> covariance(x, z)-7.5>>> covariance(z, x)-7.5
3.10 新版功能.
statistics.correlation(x, y, /)
返回兩個輸入的 皮爾遜相關系數(shù)。 皮爾遜相關系數(shù) r 的取值在 -1 到 +1 之間。 它衡量線性相關的強度和方向,其中 +1 表示非常強的正線性相關,-1 表示非常強的負線性相關,0 表示無線性相關。
兩個輸入必須具有相同的長度(不少于兩個元素),并且不必為常量,否則會引發(fā) StatisticsError。
示例:
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> y = [9, 8, 7, 6, 5, 4, 3, 2, 1]>>> correlation(x, x)1.0>>> correlation(x, y)-1.0
3.10 新版功能.
statistics.linear_regression(x, y, /, **, proportional=False*)
返回使用普通最小二乘法估計得到的 簡單線性回歸 參數(shù)的斜率和截距。 簡單純屬回歸通過此線性函數(shù)來描述自變量 x 和因變量 y 之間的關系。
y = slope \ x + intercept + noise*
其中 slope 和 intercept 是估計得到的回歸參數(shù),而 noise 代表不可由線性回歸解釋的數(shù)據(jù)變異性(它等于因變量的預測值和實際值之間的差異)。
兩個輸入必須具有相同的長度(不少于兩個元素),并且自變量 x 不可為常量;否則會引發(fā) StatisticsError。
例如,我們可以使用 Monty Python 系列電影的發(fā)布日期 在假定出品方保持現(xiàn)有步調的情況下預測到 2019 年時產(chǎn)出的 Monty Python 電影的累計數(shù)量。
>>> year = [1971, 1975, 1979, 1982, 1983]>>> films_total = [1, 2, 3, 4, 5]>>> slope, intercept = linear_regression(year, films_total)>>> round(slope * 2019 + intercept)16
If proportional is true, the independent variable x and the dependent variable y are assumed to be directly proportional. The data is fit to a line passing through the origin. Since the intercept will always be 0.0, the underlying linear function simplifies to:
y = slope \ x + noise*
3.10 新版功能.
在 3.11 版更改: Added support for proportional.
異常
只定義了一個異常:
exception statistics.StatisticsError
ValueError 的子類,表示統(tǒng)計相關的異常。
NormalDist 對象
NormalDist 工具可用于創(chuàng)建和操縱 隨機變量 的正態(tài)分布。 這個類將數(shù)據(jù)度量值的平均值和標準差作為單一實體來處理。
正態(tài)分布的概念來自于 中央極限定理 并且在統(tǒng)計學中有廣泛的應用。
class statistics.NormalDist(mu=0.0, sigma=1.0)
返回一個新的 NormalDist 對象,其中 mu 代表 算術平均值 而 sigma 代表 標準差。
若 sigma 為負數(shù),將會引發(fā) StatisticsError。
-
mean
一個只讀特征屬性,表示特定正態(tài)分布的 算術平均值。
-
median
一個只讀特征屬性,表示特定正態(tài)分布的 中位數(shù)。
-
mode
一個只讀特征屬性,表示特定正態(tài)分布的 眾數(shù))。
-
stdev
一個只讀特征屬性,表示特定正態(tài)分布的 標準差。
-
variance
一個只讀特征屬性,表示特定正態(tài)分布的 方差。 等于標準差的平方。
-
classmethod from_samples(data)
傳入使用 fmean() 和 stdev() 基于 data 估算出的 mu 和 sigma 形參創(chuàng)建一個正態(tài)分布實例。
data 可以是任何 iterable 并且應當包含能被轉換為 float 類型的值。 如果 data 不包含至少兩個元素,則會引發(fā) StatisticsError,因為估算中心值至少需要一個點而估算分散度至少需要兩個點。
-
samples(n, **, seed=None*)
對于給定的平均值和標準差生成 n 個隨機樣本。 返回一個由 float 值組成的 list。
當給定 seed 時,創(chuàng)建一個新的底層隨機數(shù)生成器實例。 這適用于創(chuàng)建可重現(xiàn)的結果,即使對于多線程上下文也有效。
-
pdf(x)
使用 概率密度函數(shù) (pdf),計算一個隨機變量 X 趨向于給定值 x 的相對可能性。 在數(shù)學意義上,它是當 dx 趨向于零時比率
P(x <= X < x+dx) / dx的極限。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)
使用 累積分布函數(shù) (cdf),計算一個隨機變量 X 小于等于 x 的概率。 在數(shù)學上,它表示為
P(X <= x)。 -
inv_cdf(p)
Compute the inverse cumulative distribution function, also known as the quantile function or the percent-point function. Mathematically, it is written
x : P(X <= x) = p.找出隨機變量 X 的值 x 使得該變量小于等于該值的概率等于給定的概率 p。
-
overlap(other)
測量兩個正態(tài)概率分布之間的一致性。 返回介于 0.0 和 1.0 之間的值,給出 兩個概率密度函數(shù)的重疊區(qū)域。
-
quantiles(n=4)
將指定正態(tài)分布劃分為 n 個相等概率的連續(xù)分隔區(qū)。 返回這些分隔區(qū)對應的 (n - 1) 個分隔點的列表。
將 n 設為 4 以使用四分位(默認值)。 將 n 設為 10 以使用十分位。將 n 設為 100 以使用百分位,即給出 99 個分隔點來將正態(tài)分布分隔為 100 個大小相等的組。
-
zscore(x)
計算 標準分 即以高于或低于正態(tài)分布的平均值的標準差數(shù)值的形式來描述 x:
(x - mean) / stdev.3.9 新版功能.
NormalDist 的實例支持加上、減去、乘以或除以一個常量。 這些運算被用于轉換和縮放。 例如:
>>> temperature_february = NormalDist(5, 2.5) # Celsius>>> temperature_february * (9/5) + 32 # FahrenheitNormalDist(mu=41.0, sigma=4.5)
不允許一個常量除以 NormalDist 的實例,因為結果將不是正態(tài)分布。
由于正態(tài)分布是由獨立變量的累加效應產(chǎn)生的,因此允許表示為 NormalDist 實例的 兩組獨立正態(tài)分布的隨機變量相加和相減。 例如:
>>> 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 示例和用法
NormalDist 適合用來解決經(jīng)典概率問題。
舉例來說,如果 SAT 考試的歷史數(shù)據(jù) 顯示分數(shù)呈平均值為 1060 且標準差為 195 的正態(tài)分布,則可以確定考試分數(shù)處于 1100 和 1200 之間的學生的百分比舍入到最接近的整數(shù)應為:
>>> sat = NormalDist(1060, 195)>>> fraction = sat.cdf(1200 + 0.5) - sat.cdf(1100 - 0.5)>>> round(fraction * 100.0, 1)18.4
求 SAT 分數(shù)的 四分位 和 十分位:
>>> list(map(round, sat.quantiles()))[928, 1060, 1192]>>> list(map(round, sat.quantiles(n=10)))[810, 896, 958, 1011, 1060, 1109, 1162, 1224, 1310]
為了估算一個不易解析的模型分布,NormalDist 可以生成用于 蒙特卡洛模擬 的輸入樣本:
>>> 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 can be used to approximate Binomial distributions when the sample size is large and when the probability of a successful trial is near 50%.
例如,一次開源會議有 750 名與會者和兩個可分別容納 500 人的會議廳。 會上有一場關于 Python 的演講和一場關于 Ruby 的演講。 在往屆會議中,65% 的與會者更愿意去聽關于 Python 的演講。 假定人群的偏好沒有發(fā)生改變,那么 Python 演講的會議廳不超出其容量上限的可能性是多少?
>>> n = 750 # Sample size>>> p = 0.65 # Preference for Python>>> q = 1.0 - p # Preference for Ruby>>> k = 500 # Room capacity>>> # Approximation using the cumulative normal distribution>>> from math import sqrt>>> round(NormalDist(mu=n*p, sigma=sqrt(n*p*q)).cdf(k + 0.5), 4)0.8402>>> # Solution using the cumulative binomial distribution>>> from math import comb, fsum>>> round(fsum(comb(n, r) * p**r * q**(n-r) for r in range(k+1)), 4)0.8402>>> # Approximation using a simulation>>> from random import seed, choices>>> seed(8675309)>>> def trial():... return choices(('Python', 'Ruby'), (p, q), k=n).count('Python')>>> mean(trial() <= k for i in range(10_000))0.8398
在機器學習問題中也經(jīng)常會出現(xiàn)正態(tài)分布。
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.
我們得到了由八個人的測量值組成的訓練數(shù)據(jù)集。 假定這些測量值是正態(tài)分布的,因此我們用 NormalDist 來總結數(shù)據(jù):
>>> 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])新聞名稱:創(chuàng)新互聯(lián)Python教程:statistics—-數(shù)學統(tǒng)計函數(shù)
分享路徑:http://www.fisionsoft.com.cn/article/dhspeoj.html


咨詢
建站咨詢
