Python 为什么我不能抑制 numpy 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29347987/
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:27:13  来源:igfitidea点击:

Why can't I suppress numpy warnings

pythonnumpy

提问by HansSnah

I really want to avoid these annoying numpy warnings since I have to deal with a lot of NaNs. I know this is usually done with seterr, but for some reason here it does not work:

我真的很想避免这些烦人的 numpy 警告,因为我必须处理很多NaNs. 我知道这通常是用 seterr 完成的,但由于某种原因,它在这里不起作用:

import numpy as np
data = np.random.random(100000).reshape(10, 100, 100) * np.nan
np.seterr(all="ignore")
np.nanmedian(data, axis=[1, 2])

It gives me a runtime warning even though I set numpy to ignore all errors...any help?

即使我将 numpy 设置为忽略所有错误,它也会给我一个运行时警告......有什么帮助吗?

Edit (this is the warning that is recieved):

编辑(这是收到的警告):

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-p??ackages/numpy/lib/nanfunctions.py:612: RuntimeWarning: All-NaN slice encountered warnings.warn("All-NaN slice encountered", RuntimeWarning)

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-p??ackages/numpy/lib/nanfunctions.py:612: RuntimeWarning: All-NaN slice encountered warnings.warn("All-NaN slice encountered", RuntimeWarning)

Thanks :)

谢谢 :)

采纳答案by miradulo

Warnings can often be useful and in most cases I wouldn't advise this, but you can always make use of the Warningsmodule to ignore all warnings with filterwarnings:

警告通常很有用,在大多数情况下,我不建议这样做,但是您始终可以使用该Warnings模块来忽略所有警告filterwarnings

warnings.filterwarnings('ignore')

Should you want to suppress uniquely your particular error, you could specify it with:

如果您想唯一地抑制您的特定错误,您可以使用以下命令指定它:

with warnings.catch_warnings():
    warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')

回答by Robert Kern

The warnings controlled by seterr()are those issued by the numpy ufunc machinery; e.g. when A / Bcreates a NaNin the C code that implements the division, say because there was an inf/infsomewhere in those arrays. Other numpy code may issue their own warnings for other reasons. In this case, you are using one of the NaN-ignoring reduction functions, like nanmin()or the like. You are passing it an array that contains all NaNs, or at least all NaNs along an axis that you requested the reduction along. Since the usual reason one uses nanmin()is to not get another NaNout, nanmin()will issue a warning that it has no choice but to give you a NaN. This goes directly to the standard library warningsmachinery and not the numpy ufunc error control machinery since it isn't a ufunc and this production of a NaNisn't the same as what seterr(invalid=...)otherwise deals with.

控制的警告seterr()是由 numpy ufunc 机器发出的警告;例如,当在实现除法的 C 代码中A / B创建 a时NaN,比如说因为inf/inf在这些数组中的某个地方。其他 numpy 代码可能会出于其他原因发出自己的警告。在这种情况下,您正在使用NaN-ignoring 归约函数之一,例如nanmin()等。您正在传递一个包含所有NaNs的数组,或者至少包含NaN沿您请求减少的轴的所有s。由于一个人使用的通常原因nanmin()是不让另一个人NaN退出,因此nanmin()会发出警告,指出它别无选择,只能给你一个NaN. 这直接进入标准库warnings机器而不是 numpy ufunc 错误控制机器,因为它不是 ufunc 并且这种 aNaN的产生与seterr(invalid=...)其他处理的不同。

回答by idnavid

You may want to avoid suppressing the warning, because numpy raises this for a good reason. If you want to clean up your output, maybe handle it by explicitly returning a pre-defined value when your array is all nan.

您可能希望避免抑制警告,因为 numpy 提出这一点是有充分理由的。如果你想清理你的输出,当你的数组都是 nan 时,可以通过显式返回一个预定义的值来处理它。

def clean_nanmedian(s):
    if np.all(np.isnan(s)):
        return np.nan
    return np.nanmedian(s)

Also, keep in mind that this RuntimeWarning is raised only the first time that this happens in your run-time.

另外,请记住,只有在您的运行时第一次发生这种情况时才会引发此 RuntimeWarning。