當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python statistics variance()用法及代碼示例


Statistics該模塊提供了非常強大的工具,可用於計算與統計信息相關的任何信息。 variance()是這樣一種函數。此函數有助於根據數據樣本(樣本是填充數據的子集)計算方差。

variance()僅當需要計算樣本方差時才應使用該函數。還有另一個函數叫做pvariance(),用於計算整個總體的方差。

在純統計數據中,variance是變量與其平均值的平方偏差。本質上,它根據平均值或中位數來衡量一組數據的隨機分布。較低的方差值表示數據聚集在一起並且沒有廣泛散布,而較高的值表示給定集中的數據與平均值相比散布得多。


方差是科學中的重要工具,在科學中,對數據進行統計分析很普遍。它是給定data-set的標準偏差的平方,也被稱為分布的第二中心矩。通常用 s^{2}, \sigma ^{2}, \operatorname {Var} (X)純統計。

方差通過以下公式計算:

It’s calculated by mean of square minus square of mean

 \operatorname {Var} (X)=\operatorname {E} \left[(X-\mu )^{2}\right]

用法: variance( [data], xbar )

Parameters:
[數據]:具有實值數字的可迭代項。
xbar (Optional):將data-set的實際平均值作為值。

Returnype:返回作為參數傳遞的值的實際方差。

Exceptions:
StatisticsError為data-set引發的值小於作為參數傳遞的2個值。
當xbar提供的值與data-set的實際均值不匹配時,將拋出不可能的值。

代碼1:

# Python code to demonstrate the working of 
# variance() function of Statistics Module 
  
# Importing Statistics module 
import statistics 
  
# Creating a sample of data 
sample = [2.74, 1.23, 2.63, 2.22, 3, 1.98] 
  
# Prints variance of the sample set 
  
# Function will automatically calculate 
# it's mean and set it as xbar 
print("Variance of sample set is % s" 
      %(statistics.variance(sample)))

輸出:


Variance of sample set is 0.40924


代碼2:演示一係列數據類型上的variance()

# Python code to demonstrate variance()  
# function on varying range of data-types 
  
# importing statistics module 
from statistics import variance 
  
# importing fractions as parameter values 
from fractions import Fraction as fr 
  
# tuple of a set of positive integers 
# numbers are spread apart but not very much 
sample1 = (1, 2, 5, 4, 8, 9, 12) 
  
# tuple of a set of negative integers 
sample2 = (-2, -4, -3, -1, -5, -6) 
  
# tuple of a set of positive and negative numbers 
# data-points are spread apart considerably  
sample3 = (-9, -1, -0, 2, 1, 3, 4, 19) 
  
# tuple of a set of fractional numbers 
sample4 = (fr(1, 2), fr(2, 3), fr(3, 4), 
                     fr(5, 6), fr(7, 8)) 
  
# tuple of a set of floating point values 
sample5 = (1.23, 1.45, 2.1, 2.2, 1.9) 
  
# Print the variance of each samples 
print("Variance of Sample1 is % s " %(variance(sample1))) 
print("Variance of Sample2 is % s " %(variance(sample2))) 
print("Variance of Sample3 is % s " %(variance(sample3))) 
print("Variance of Sample4 is % s " %(variance(sample4))) 
print("Variance of Sample5 is % s " %(variance(sample5)))

輸出:

Variance of Sample 1 is 15.80952380952381 
Variance of Sample 2 is 3.5 
Variance of Sample 3 is 61.125 
Variance of Sample 4 is 1/45 
Variance of Sample 5 is 0.17613000000000006 


代碼3:演示xbar參數的使用

# Python code to demonstrate  
# the use of xbar parameter 
  
# Importing statistics module 
import statistics 
  
# creating a sample list 
sample = (1, 1.3, 1.2, 1.9, 2.5, 2.2) 
  
# calculating the mean of sample set 
m = statistics.mean(sample) 
  
  
# calculating the variance of sample set 
print("Variance of Sample set is % s" 
    %(statistics.variance(sample, xbar = m)))

輸出:

Variance of Sample set is 0.3656666666666667


代碼4:當xbar的值與平均值/平均值不同時,顯示錯誤

# Python code to demonstrate the error caused 
# when garbage value of xbar is entered 
  
# Importing statistics module 
import statistics 
  
# creating a sample list 
sample = (1, 1.3, 1.2, 1.9, 2.5, 2.2) 
  
# calculating the mean of sample set 
m = statistics.mean(sample) 
  
# Actual value of mean after calculation 
# comes out to 1.6833333333333333 
# But to demonstrate xbar error let's enter  
# -100 as the value for xbar parameter 
print(statistics.variance(sample, xbar = -100))

輸出:

0.3656666666663053

注意:它的精度與代碼3中的輸出不同

代碼4:展示StatisticsError

# Python code to demonstrate StatisticsError 
  
# importing Statistics module 
import statistics 
  
# creating an emoty data-srt 
sample = [] 
  
# will raise Statistics Error 
print(statistics.variance(sample))

輸出:

Traceback (most recent call last):
  File "/home/64bf6d80f158b65d2b75c894d03a7779.py", line 10, in 
    print(statistics.variance(sample))
  File "/usr/lib/python3.5/statistics.py", line 555, in variance
    raise StatisticsError('variance requires at least two data points')
statistics.StatisticsError:variance requires at least two data points


應用範圍:
方差是統計和處理大量數據中非常重要的工具。就像,當無所不知的均值是未知的(樣本均值)時,方差被用作偏差估計量。現實世界中的觀察,例如一天中公司所有股份的增減價值,不可能是所有可能的觀察。因此,方差是從有限的一組數據中計算出來的,盡管考慮到總體時,方差不會匹配,但仍會為用戶提供足以進行其他計算的估計值。



相關用法


注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python statistics | variance()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。