先决条件: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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。