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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。