📜  Python统计模块中的 stdev() 方法

📅  最后修改于: 2022-05-13 01:54:54.797000             🧑  作者: Mango

Python统计模块中的 stdev() 方法

Python中的统计模块提供了一个称为stdev()的函数,可用于计算标准差。 stdev()函数仅计算数据样本的标准偏差,而不是整个总体。

为了计算整个总体的标准偏差,使用了另一个称为pstdev()的函数。

标准偏差是统计中传播的量度。它用于量化一组数据值的分布、变化的度量。它与方差非常相似,给出了偏差的度量,而方差提供了平方值。
标准偏差的低测量值表明数据分散较少,而标准偏差的高值表明一组数据与其平均值分散开。标准差的一个有用特性是,与方差不同,它以与数据相同的单位表示。

标准偏差由以下公式计算: {\displaystyle s = {\sqrt {\frac {\sum _{i=1}^{N}(x_{i}-{\overline {x}})^{2}}{N-1}}} }其中 x1, x2, x3.....xn 是样本数据中的观测值, \scriptstyle {\overline {x}}是观测值的平均值,N 是样本观测值的数量。

代码#1:

Python3
# Python code to demonstrate stdev() function
 
# importing Statistics module
import statistics
 
# creating a simple data - set
sample = [1, 2, 3, 4, 5]
 
# Prints standard deviation
# xbar is set to default value of 1
print("Standard Deviation of sample is % s "
                % (statistics.stdev(sample)))


Python3
# Python code to demonstrate stdev() 
# function on various range of datasets
 
# importing the statistics module
from statistics import stdev
 
# importing fractions as parameter values
from fractions import Fraction as fr
 
# creating a varying range of sample sets
# 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 floating point values
sample4 = (1.23, 1.45, 2.1, 2.2, 1.9)
 
# Print the standard deviation of 
# following sample sets of observations
print("The Standard Deviation of Sample1 is % s"
                              %(stdev(sample1)))
                               
print("The Standard Deviation of Sample2 is % s"
                              %(stdev(sample2)))
                               
print("The Standard Deviation of Sample3 is % s"
                              %(stdev(sample3)))
                               
                               
print("The Standard Deviation of Sample4 is % s"
                              %(stdev(sample4)))


Python3
# Python code to demonstrate difference
# in results of stdev() and variance()
 
# importing Statistics module
import statistics
 
# creating a simple data-set
sample = [1, 2, 3, 4, 5]
 
# Printing standard deviation
# xbar is set to default value of 1
print("Standard Deviation of the sample is % s "
                    %(statistics.stdev(sample)))
 
# variance is approximately the
# squared result of what stdev is
print("Variance of the sample is % s"
     %(statistics.variance(sample)))


Python3
# Python code to demonstrate use of xbar
# parameter while using stdev() function
 
# 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)
 
# xbar is nothing but stores
# the mean of the sample set
 
# calculating the variance of sample set
print("Standard Deviation of Sample set is % s"
         %(statistics.stdev(sample, xbar = m)))


Python3
# Python code to demonstrate StatisticsError
 
# importing the statistics module
import statistics
 
# creating a data-set with one element
sample = [1]
 
# will raise StatisticsError
print(statistics.stdev(sample))


输出 :

Standard Deviation of the sample is 1.5811388300841898 

代码 #2:在一组不同的数据类型上演示 stdev()

Python3

# Python code to demonstrate stdev() 
# function on various range of datasets
 
# importing the statistics module
from statistics import stdev
 
# importing fractions as parameter values
from fractions import Fraction as fr
 
# creating a varying range of sample sets
# 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 floating point values
sample4 = (1.23, 1.45, 2.1, 2.2, 1.9)
 
# Print the standard deviation of 
# following sample sets of observations
print("The Standard Deviation of Sample1 is % s"
                              %(stdev(sample1)))
                               
print("The Standard Deviation of Sample2 is % s"
                              %(stdev(sample2)))
                               
print("The Standard Deviation of Sample3 is % s"
                              %(stdev(sample3)))
                               
                               
print("The Standard Deviation of Sample4 is % s"
                              %(stdev(sample4)))

输出 :

The Standard Deviation of Sample1 is 3.9761191895520196
The Standard Deviation of Sample2 is 1.8708286933869707
The Standard Deviation of Sample3 is 7.8182478855559445
The Standard Deviation of Sample4 is 0.41967844833872525

代码#3:展示variance()和stdev()结果的区别

Python3

# Python code to demonstrate difference
# in results of stdev() and variance()
 
# importing Statistics module
import statistics
 
# creating a simple data-set
sample = [1, 2, 3, 4, 5]
 
# Printing standard deviation
# xbar is set to default value of 1
print("Standard Deviation of the sample is % s "
                    %(statistics.stdev(sample)))
 
# variance is approximately the
# squared result of what stdev is
print("Variance of the sample is % s"
     %(statistics.variance(sample)))

输出 :

Standard Deviation of the sample is 1.5811388300841898 
Variance of the sample is 2.5

代码#4:演示xbar参数的使用

Python3

# Python code to demonstrate use of xbar
# parameter while using stdev() function
 
# 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)
 
# xbar is nothing but stores
# the mean of the sample set
 
# calculating the variance of sample set
print("Standard Deviation of Sample set is % s"
         %(statistics.stdev(sample, xbar = m)))

输出 :

Standard Deviation of Sample set is 0.6047037842337906

代码 #5:展示 StatisticsError

Python3

# Python code to demonstrate StatisticsError
 
# importing the statistics module
import statistics
 
# creating a data-set with one element
sample = [1]
 
# will raise StatisticsError
print(statistics.stdev(sample))

输出 :

Traceback (most recent call last):
  File "/home/f921f9269b061f1cc4e5fc74abf6ce10.py", line 12, in 
    print(statistics.stdev(sample))
  File "/usr/lib/python3.5/statistics.py", line 617, in stdev
    var = variance(data, xbar)
  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

应用:

  • 标准偏差在统计数学和统计研究领域非常重要。它通常用于衡量统计计算的置信度。例如,计算考试分数的误差幅度是通过计算结果中的预期标准偏差来确定的,如果同一考试要进行多次。
  • 它在金融研究领域非常有用,并且有助于确定盈亏幅度。标准差也很重要,其中投资回报率的标准差是衡量投资波动性的指标。