简体   繁体   English

为什么我不能抑制 numpy 警告

[英]Why can't I suppress numpy warnings

I really want to avoid these annoying numpy warnings since I have to deal with a lot of NaNs .我真的很想避免这些烦人的 numpy 警告,因为我必须处理很多NaNs I know this is usually done with seterr, but for some reason here it does not work:我知道这通常是用 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遇到warnings.warn( “遇到全 NaN 切片”,运行时警告)

Warnings can often be useful and in most cases I wouldn't advise this, but you can always make use of the Warnings module 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')

The warnings controlled by seterr() are those issued by the numpy ufunc machinery;seterr()控制的警告是由 numpy ufunc 机器发出的警告; eg when A / B creates a NaN in the C code that implements the division, say because there was an inf/inf somewhere in those arrays.例如,当A / B在实现除法的 C 代码中创建一个NaN时,比如说因为在这些数组的某处有一个inf/inf Other numpy code may issue their own warnings for other reasons.其他 numpy 代码可能会出于其他原因发出自己的警告。 In this case, you are using one of the NaN -ignoring reduction functions, like nanmin() or the like.在这种情况下,您使用的是忽略NaN归约函数之一,例如nanmin()等。 You are passing it an array that contains all NaN s, or at least all NaN s along an axis that you requested the reduction along.您正在向它传递一个包含所有NaN的数组,或者至少沿您请求减少的轴的所有NaN Since the usual reason one uses nanmin() is to not get another NaN out, nanmin() will issue a warning that it has no choice but to give you a NaN .由于使用nanmin()的通常原因是不取出另一个NaNnanmin()将发出警告,表示它别无选择,只能给你一个NaN This goes directly to the standard library warnings machinery and not the numpy ufunc error control machinery since it isn't a ufunc and this production of a NaN isn't the same as what seterr(invalid=...) otherwise deals with.这直接进入标准库warnings机制而不是 numpy ufunc 错误控制机制,因为它不是 ufunc 并且这种NaN的产生与seterr(invalid=...)否则处理的内容不同。

You may want to avoid suppressing the warning, because numpy raises this for a good reason.您可能希望避免抑制警告,因为 numpy 提出这一点是有充分理由的。 If you want to clean up your output, maybe handle it by explicitly returning a pre-defined value when your array is all nan.如果你想清理你的输出,当你的数组都是 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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM