翻阅古今
有时候人容易犯知其一不知其二的错误,np.std也是支持计算无偏样本标准差的(话说无偏样本标准差这么常用,NumPy怎么会不支持呢),见如下代码:>>> a = np.arange(10)>>> aarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> np.std(a, ddof = 1)3.0276503540974917>>> np.sqrt(((a - np.mean(a)) ** 2).sum() / (a.size - 1))3.0276503540974917>>> np.sqrt(( a.var() * a.size) / (a.size - 1))3.0276503540974917
慕雪6442864
12345678910111213141516import numpy as np #可以直接用std函数a = np.arange(10)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])np.std(a)#2.8722813232690143 #或者按标准差公式写a = np.arange(10)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])mid = 0for i in mid: mid = mid + (i - np.mean(a)) ** 2np.sqrt(mid/a.size)#2.8722813232690143