是什么导致numpy.nanstd()返回nan? [英] What could cause numpy.nanstd() to return nan?

查看:145
本文介绍了是什么导致numpy.nanstd()返回nan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对较大的一维数组(20000个观察值)。当我计算诸如标准差,均值,总和之类的描述性统计量时,我得到了nan值,但是当我从同一数组中计算出一个区间时,我得到了我期望的值。我尝试了诸如numpy.nanstd,nanmean之类的函数。我可能做错了什么?我正在使用python 2.7.9

I have a relatively large 1-D array (20000 observations). When I calculate descrpitive statistics such as standard deviation, mean, sum, I get nan value, but when I calculate a percintle from the same array I get the value I expected. I tried functions such as numpy.nanstd, nanmean. What could I be doing wrong? I am using python 2.7.9

推荐答案

在三种情况下, np.nanstd 可能返回 NaN


  1. 如果输入为空

  1. If the input is empty

如果输入中的所有元素都是 NaN

If all of the elements in the input are NaN

如果其中一个元素是正无穷大或负无穷大。要了解为什么会发生这种情况,请记住标准差的公式为

If one of the elements is either positive or negative infinity. To understand why this happens, remember that the formula for standard deviation is

由于 x 包含 inf ,因此 x的平均值也将是 inf 。因此,在计算均值偏差时,至少有一个元素等于 inf-inf 。如果在IPython提示符下尝试此操作,则会看到 inf-inf 被定义为 NaN

Since x contains inf, the mean of x will also be inf. Therefore when computing the deviations from the mean, there is at least one element that is equal to inf - inf. If you try this at the IPython prompt, you will see that inf - inf is defined as NaN.

在前两种情况下,您应该得到一个有用的警告:

In the former two cases you should get a helpful warning:

RuntimeWarning: Degrees of freedom <= 0 for slice.

这篇关于是什么导致numpy.nanstd()返回nan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆