1. RuntimeWarning: Degrees of freedom <= 0 for slice
切片(list,tuple或ndarray)的自由度 <= 0,如下:
>> np.std([1., ], ddof=1)
/Users/xx/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:135: RuntimeWarning: Degrees of freedom <= 0 for slice
keepdims=keepdims)
/Users/xx/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py:127: RuntimeWarning: invalid value encountered in double_scalars
ret = ret.dtype.type(ret / rcount)
nan
对于 np.std 的实现,
s = ∑ i N ( x i − x ˉ ) 2 N − 1 s=\sqrt\frac{\sum_i^N(x_i-\bar x)^2}{N-1} s=N−1∑iN(xi−xˉ)2
np.std 还有一个 ddof 的可选参数,表示 delta degree of freedom,对应着上述计算公式根号内分母减去的值,默认为 0,一般为与统计计算公式保持一致,常取 ddof=1。但当待计算的序列的长度为1时,自由度为 0,也即警告中的 Degrees of freedom <= 0 for slice
。
2. RuntimeWarning: invalid value encountered in double_scalars
- 出现该警告时,数值计算返回的结果将为 nan;