先決條件:Python統計信息| variance()
pvariance()
函數有助於計算整個方差,而不是樣本方差。之間的唯一區別variance()
和pvariance()
是在使用variance()時,僅考慮樣本均值,而在pvariance()期間,則考慮整個總體的均值。
總體方差與樣本方差相似,它說明了特定總體中的數據點如何分布。它是從data-points到data-set均值的平均距離,為平方。總體方差是總體的一個參數,不依賴於研究方法或抽樣方法。
用法: pvariance( [data], mu)
Parameters:
[數據]:具有實值數字的可迭代項。
mu (optional):將data-set /人口的實際平均值作為值。
Returnype:
返回作為參數傳遞的值的實際總體方差。
Exceptions:
StatisticsError為data-set引發的值小於作為參數傳遞的2個值。
不可能的價值當以mu形式提供的值與data-set的實際平均值不匹配時。
代碼1:
# Pythom code to demonstrate the
# use of pvariance()
# importing statistics module
import statistics
# creating a random population list
population = (1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.9, 2.2,
2.3, 2.4, 2.6, 2.9, 3.0, 3.4, 3.3, 3.2)
# Prints the population variance
print("Population variance is %s"
%(statistics.pvariance(population)))
輸出:
Population variance is 0.6658984375
代碼2:在不同範圍的種群樹上演示pvariance()。
# Python code to demonstrate pvariance()
# on various range of population sets
# importing statistics module
from statistics import pvariance
# importing fractions module as F
from fractions import Fraction as F
# Population tree for a set of positive integers
pop1 = (1, 2, 3, 5, 4, 6, 1, 2, 2, 3, 1, 3,
7, 8, 9, 1, 1, 1, 2, 6, 7, 8, 9, )
# Creating a population tree for
# a set of negative integers
pop2 = (-36, -35, -34, -32, -30, -31, -33, -33, -33,
-38, -36, -35, -34, -38, -40, -31, -32)
# Creating a population tree for
# a set of fractional numbers
pop3 = (F(1, 3), F(2, 4), F(2, 3),
F(3, 2), F(2, 5), F(2, 2),
F(1, 1), F(1, 4), F(1, 2), F(2, 1))
# Creating a population tree for
# a set of decimal values
pop4 = (3.45, 3.2, 2.5, 4.6, 5.66, 6.43,
4.32, 4.23, 6.65, 7.87, 9.87, 1.23,
1.00, 1.45, 10.12, 12.22, 19.88)
# Print the population variance for
# the created population trees
print("Population variance of set 1 is % s"
%(pvariance(pop1)))
print("Population variance of set 2 is % s"
%(pvariance(pop2)))
print("Population variance of set 3 is % s"
%(pvariance(pop3)))
print("Population variance of set 4 is % s"
%(pvariance(pop4)))
輸出:
Population variance of set 1 is 7.913043478260869 Population variance of set 2 is 7.204152249134948 Population variance of set 3 is 103889/360000 Population variance of set 4 is 21.767923875432526
代碼3:演示使用mu參數。
# Python code to demonstrate the use
# of 'mu' parameter on pvariance()
# importing statistics module
import statistics
# Apparently, the Python interpreter doesn't
# even check whether the value entered for mu
# is the actual mean of data-set or not.
# Thus providing incorrect value would
# lead to impossible answers
# Creating a population tree of the
# age of kids in a locality
tree = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 12, 12, 12, 13, 1, 2, 12, 2, 2,
2, 3, 4, 5, 5, 5, 5, 6, 6, 6)
# Finding the mean of population tree
m = statistics.mean(tree)
# Using the mu parameter
# while using pvariance()
print("Population Variance is % s"
%(statistics.pvariance(tree, mu = m)))
輸出:
Population Variance is 14.30385015608741
代碼4:演示pvariance()和variance()之間的區別
# Pythom code to demonstrate the
# difference between pvariance()
# and variance()
# importing statistocs module
import statistics
# Population tree and extract
# a sample from it
tree = (1.1, 1.22, .23, .55, .67, 2.33, 2.81,
1.54, 1.2, 0.2, 0.1, 1.22, 1.61)
# Sample extract from population tree
sample = (1.22, .23, .55, .67, 2.33,
2.81, 1.54, 1.2, 0.2)
# Print sample variance and as
# well as population variance
print ("Variance of whole popuation is %s"
%(statistics.pvariance(tree)))
print ("Variance of sample from population is %s "
% (statistics.variance(sample)))
# Print the difference in both population
# variance and sample variance
print("\n")
print("Difference in Population variance"
"and Sample variance is % s"
%(abs(statistics.pvariance(tree)
- statistics.variance(sample))))
輸出:
Variance of the whole popuation is 0.6127751479289941 Variance of the sample from population is 0.8286277777777779 Difference in Population variance and Sample variance is 0.21585262984878373
注意:從上麵的示例示例中可以看出,總體方差和示例方差相差不大。
代碼5:展示StatisticsError
# Python code to demonstrate StatisticsError
# importing statistics module
import statistics
# creating an empty population set
pop = ()
# will raise StatisticsError
print(statistics.pvariance(pop))
輸出:
Traceback (most recent call last): File "/home/fa112e1405f09970eeddd48214318a3c.py", line 10, in print(statistics.pvariance(pop)) File "/usr/lib/python3.5/statistics.py", line 603, in pvariance raise StatisticsError('pvariance requires at least one data point') statistics.StatisticsError:pvariance requires at least one data point
應用範圍:
總體方差的應用與樣本方差非常相似,盡管總體方差的範圍比樣本方差大得多。僅在要計算整個總體的方差時才使用總體方差,否則在計算樣本方差時,最好使用variance()。人口差異是統計和處理大量數據中非常重要的工具。就像,當無所不知的均值是未知的(樣本均值)時,方差被用作偏差估計量。
相關用法
- Python statistics mean()用法及代碼示例
- Python statistics harmonic_mean()用法及代碼示例
- Python statistics median_low()用法及代碼示例
- Python statistics median_high()用法及代碼示例
- Python statistics variance()用法及代碼示例
- Python statistics median_grouped()用法及代碼示例
- Python - statistics stdev()用法及代碼示例
- Python statistics median()用法及代碼示例
注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python statistics | pvariance()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。