问题描述
我真的想避免这些烦人的numpy警告,因为我必须处理很多NaNs
.我知道这通常是用seterr完成的,但是由于某些原因,它不起作用:
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:
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])
即使我将numpy设置为忽略所有错误,它也会向我发出运行时警告...任何帮助?
It gives me a runtime warning even though I set numpy to ignore all errors...any help?
编辑(这是收到的警告):
Edit (this is the warning that is recieved):
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/lib/nanfunctions.py:612: RuntimeWarning: All-NaN slice encountered warnings.warn("All-NaN slice encountered", RuntimeWarning)
谢谢:)
推荐答案
警告通常很有用,在大多数情况下我不建议这样做,但是您始终可以使用 Warnings
模块忽略带有filterwarnings
的所有警告:
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('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')
这篇关于为什么我不能抑制numpy警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!