Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I suppress numpy warnings

Tags:

python

numpy

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]) 

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-p‌​ackages/numpy/lib/nanfunctions.py:612: RuntimeWarning: All-NaN slice encountered warnings.warn("All-NaN slice encountered", RuntimeWarning)

like image 651
HansSnah Avatar asked Mar 30 '15 13:03

HansSnah


People also ask

How do I ignore runtime warnings in Python?

In order to temporarily suppress warnings, set simplefilter to 'ignore'.

How do you ignore Division by zero in Python?

In Python, we use a try block that contains a return statement to divide 2 numbers. If there is no division by zero error, then it will return the result. Otherwise, the except line will check if the specified exception name is a match, and then it will execute the code under the except block.


1 Answers

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') 
like image 103
miradulo Avatar answered Oct 19 '22 23:10

miradulo