Statistics
Python中的模块提供了称为stdev()的函数,可用于计算标准差。stdev()
函数仅从数据样本计算标准差,而不从整个总体计算。
要计算整个总体的标准差,另一个函数称为pstdev()用来。
标准差是统计量分布的度量。它用于量化一组数据值的散布,变化的度量。它与方差非常相似,给出偏差的度量,而方差提供平方值。
较低的标准偏差表示数据散布较少,而较高的标准偏差表示一组数据分散在其平均值之外。标准偏差的一个有用属性是,与方差不同,它以与数据相同的单位表示。
Standard Deviation is calculated by: where x1, x2, x3.....xn are observed values in sample data, is the mean value of observations and N is the number of sample observations.
用法: stdev( [data-set], xbar )
Parameters:
[数据]:具有实值数字的可迭代项。
xbar (Optional):将data-set的实际平均值作为值。
Returnype:
返回作为参数传递的值的实际标准偏差。
Exceptions:
StatisticsError为data-set引发小于2个作为参数传递的值。
不可能/precision-less值当xbar提供的值与data-set的实际平均值不匹配时。
代码1:
# 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)))
输出:
Standard Deviation of the sample is 1.5811388300841898
代码2:在一组不同的数据类型上演示stdev()
# Python code to demonstrate stdev()
# function on varioius range of datasets
# importing the statistics module
from statistics import stdev
# importing frations 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()结果之间的差异
# 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参数的使用
# 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
# Python code to demonstarte 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
应用范围:
- 在统计数学和统计研究领域,标准差非常重要。它通常用于衡量统计计算中的置信度。例如,如果要进行多次相同的检查,则通过计算结果中的预期标准偏差来确定计算检查分数时的误差范围。
- 它在金融研究领域非常有用,并且有助于确定损益的幅度。标准偏差也很重要,其中投资回报率的标准偏差是投资波动性的度量。
相关用法
- Python statistics median_high()用法及代码示例
- Python statistics variance()用法及代码示例
- Python statistics harmonic_mean()用法及代码示例
- Python statistics median_grouped()用法及代码示例
- Python statistics mean()用法及代码示例
- Python statistics median_low()用法及代码示例
- Python statistics median()用法及代码示例
- Python statistics pvariance()用法及代码示例
注:本文由纯净天空筛选整理自retr0大神的英文原创作品 Python statistics | stdev()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。