2013-07-26 67 views
0

我隔离在我的剧本的问题,即由于在标准偏差计算使用SciPy的的.tstd机能发生这种尝试时零浮动师,ZeroDivisionError:计算标准偏差

sp.stats.tstd(IR) 

在我的IR值是0.0979。有没有办法让它停止(我认为)将它舍入到零?我已经尝试从以前的一个stackoverflow帖子,建议调用号码np.float64,但没有奏效的建议。希望有人有答案。

已满错误打印输出:

Traceback (most recent call last): 
    File "Utt_test.py", line 995, in <module> 
    X.write(Averaging()) 
    File "Utt_test.py", line 115, in Averaging 
    IR_sdev=str(round(sp.stats.tstd(IR),4)) 
    File "/usr/lib64/python2.7/site-packages/scipy/stats/stats.py", line 848, in tstd 
    return np.sqrt(tvar(a,limits,inclusive)) 
    File "/usr/lib64/python2.7/site-packages/scipy/stats/stats.py", line 755, in tvar 
    return a.var()*(n/(n-1.)) 
ZeroDivisionError: float division by zero 
+6

'tstd'需要numpy的阵列的输入。很难计算单个数字的标准偏差。 – Daniel

+1

根据错误信息,在计算“a.var()*(n /(n-1。))”时出现问题。比问题是''n = 1.'''。与numpy float或IR无关... – hivert

+0

我看到...嗯,那么必须重写一些东西。该程序通常运行多个数字。我正在尝试一个特例。 – Matt

回答

0

方法tstd计算样本方差的平方根。样本方差与总体方差相差n/(n-1),这是使样本方差成为总体方差无偏估计量所必需的因子。这对n = 1来说是分解的,这是可以理解的,因为有一个数字让我们不知道总体变化可能是什么。

如果这种调整是不可取的(也许您的数组是总人口,而不是来自它的样本),请改用numpy.std。对于大小为1的数组,它将按预期返回0。如果与参数ddof = 1一起使用,则numpy.std等效于stats.tstd


记录Asie:SciPy's documentation状态

tstd computes the unbiased sample standard deviation, i.e. it uses a correction factor n/(n - 1).

重复常见的误解,这种标准误差估计是无偏(事实上,校正因子消除偏压的方差,而不是为标准偏差)。 NumPy's std documentation证明是正确的在这一点上,其中它讨论ddof参数

If, however, ddof is specified, the divisor N - ddof is used instead. In standard statistical practice, ddof=1 provides an unbiased estimator of the variance of the infinite population. ddof=0 provides a maximum likelihood estimate of the variance for normally distributed variables. The standard deviation computed in this function is the square root of the estimated variance, so even with ddof=1, it will not be an unbiased estimate of the standard deviation per se.