How to get rid of specific warning messages in python while keeping all other warnings as normal?
If Scipy is using the warnings
module, then you can suppress specific warnings. Try this at the beginning of your program:
import warnings
warnings.filterwarnings("ignore", message="divide by zero encountered in divide")
If you want this to apply to only one section of code, then use the warnings context manager:
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="divide by zero encountered in divide")
# .. your divide-by-zero code ..
I'd avoid the division-by-zero in the first place:
if a == 0:
# Break out early
# Otherwise the ratio makes sense
If you do want to squash that particular numpy warning on a single line, numpy provides a way:
with numpy.errstate(divide='ignore'):
# The problematic line