Python numpy 計算標準差 standard deviation

本篇紀錄如何使用 python numpy 的 np.std 來計算陣列標準差 standard deviation 的方法。

以下例子為簡單的無偏標準差計算, 1/n,
[1, 2, 3] mean=2,std=1
[5,6,8,9] mean=7,std=1.58114
[0.8, 0.4, 1.2, 3.7, 2.6, 5.8] mean=2.4166666666666665,std=2.0634114147853952

範例. 用 numpy 計算標準差

以下範例使用 numpy 來計算標準差,使用 np.array 帶入 python list [5,6,8,9],
接著再使用 np.std 計算標準差。

使用 np.std() 可以指定 ddof 參數,全名為 Delta Degree of Freedom,np.std() 預設的 ddof 是 0

ddof=0,回傳 population standard deviation 母體標準差,分母(n),有偏估計
ddof=1,回傳 sample standard deviation 樣本標準差,分母(n-1),無偏估計

python-numpy-std.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np

arr = np.array([5, 6, 8, 9])
print(np.std(arr, ddof=1))

ddof=1 輸出如下:

1
1.8257418583505538

ddof=0 輸出如下:

1
1.5811388300841898

關於這兩種差異可以看參考[5],
簡單說,如果要由樣本去估計整體實際的標準差,就應該使用 ddof=1。

範例. Python 內建的 statistics.stdev 計算標準差

這個範例是使用 Python 內建的 statistics 來計算標準差 stdev。

python-std.py
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import statistics

arr = [5, 6, 8, 9]
print(statistics.stdev(arr))

輸出如下:

1
1.8257418583505538

參考
[1] numpy.std() in Python - GeeksforGeeks
https://www.geeksforgeeks.org/numpy-std-in-python/
[2] python 标准差计算(std)_Python_Gooooa的博客-CSDN博客
https://blog.csdn.net/Gooooa/article/details/78923469
[3] 標準差 - 維基百科,自由的百科全書
https://zh.m.wikipedia.org/zh-tw/%E6%A8%99%E6%BA%96%E5%B7%AE
[4] python - Standard deviation in numpy - Stack Overflow
https://stackoverflow.com/questions/34050491/standard-deviation-in-numpy
[5] 為什麼統計的樣本標準差計算要除(n-1)而母體標準差則除n? | 電子製造,工作狂人(ResearchMFG)
https://www.researchmfg.com/2016/07/sigma-n-1/

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python numpy 計算平均值 mean/average
Python 新手入門教學懶人包
Python str 字串用法與範例
Python list 串列用法與範例
Python set 集合用法與範例
Python dict 字典用法與範例
Python tuple 元組用法與範例