Python 均值、南均值和警告:空切片的均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29688168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:52:50  来源:igfitidea点击:

mean, nanmean and warning: Mean of empty slice

pythonnumpywarnings

提问by Michael Currie

Say I construct two numpy arrays:

假设我构造了两个 numpy 数组:

a = np.array([np.NaN, np.NaN])
b = np.array([np.NaN, np.NaN, 3])

Now I find that np.meanreturns nanfor both aand b:

现在我发现和都np.mean返回:nanab

>>> np.mean(a)
nan
>>> np.mean(b)
nan

Since numpy 1.8 (released 20 April 2016), we've been blessed with nanmean, which ignores nanvalues:

自 numpy 1.8(2016 年 4 月 20 日发布)以来,我们一直幸运地使用nanmean,它忽略了nan值:

>>> np.nanmean(b)
3.0

However, when the array has nothing butnanvalues, it raises a warning:

然而,当阵列无关,但nan价值,它提出了一个警告:

>>> np.nanmean(a)
nan
C:\python-3.4.3\lib\site-packages\numpy\lib\nanfunctions.py:598: RuntimeWarning: Mean of empty slice
  warnings.warn("Mean of empty slice", RuntimeWarning)

I don't like suppressing warnings; is there a better function I can use to get the behaviour of nanmeanwithout that warning?

我不喜欢压制警告;有没有更好的函数可以用来获得nanmean没有警告的行为?

采纳答案by ali_m

I really can't see any good reason not to just suppress the warning.

我真的看不出有什么好的理由不只是压制警告。

The safest way would be to use the warnings.catch_warningscontext manager to suppress the warning only where you anticipate it occurring - that way you won't miss any additional RuntimeWarningsthat might be unexpectedly raised in some other part of your code:

最安全的方法是使用warnings.catch_warnings上下文管理器仅在您预期发生警告的地方抑制警告 - 这样您就不会错过任何RuntimeWarnings可能在代码的其他部分意外引发的额外内容:

import numpy as np
import warnings

x = np.ones((1000, 1000)) * np.nan

# I expect to see RuntimeWarnings in this block
with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=RuntimeWarning)
    foo = np.nanmean(x, axis=1)

@dawg's solution would also work, but ultimately any additional steps that you have to take in order to avoid computing np.nanmeanon an array of all NaNs are going to incur some extra overhead that you could avoid by just suppressing the warning. Also your intent will be much more clearly reflected in the code.

@dawg 的解决方案也可以工作,但最终为了避免计算np.nanmean所有 NaN 的数组而必须采取的任何额外步骤都会产生一些额外的开销,您可以通过抑制警告来避免这些开销。此外,您的意图将更清楚地反映在代码中。

回答by dawg

A NaNvalue is defined to not be equal to itself:

一个NaN值被定义为不等于自身:

>>> float('nan') == float('nan')
False
>>> np.NaN == np.NaN
False

You can use a Python conditional and the property of a nan never being equal to itself to get this behavior:

您可以使用 Python 条件和 nan 永远不等于自身的属性来获得此行为:

>>> a = np.array([np.NaN, np.NaN])
>>> b = np.array([np.NaN, np.NaN, 3])
>>> np.NaN if np.all(a!=a) else np.nanmean(a)
nan
>>> np.NaN if np.all(b!=b) else np.nanmean(b)
3.0

You can also do:

你也可以这样做:

import warnings
import numpy as np

a = np.array([np.NaN, np.NaN])
b = np.array([np.NaN, np.NaN, 3])

with warnings.catch_warnings():
    warnings.filterwarnings('error')
    try:
        x=np.nanmean(a)
    except RuntimeWarning:
        x=np.NaN    
print x