Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

python - mean, nanmean and warning: Mean of empty slice

Say I construct two numpy arrays:

a = np.array([np.NaN, np.NaN])
b = np.array([np.NaN, np.NaN, 3])

Now I find that np.mean returns nan for both a and b:

>>> np.mean(a)
nan
>>> np.mean(b)
nan

Since numpy 1.8 (released 20 April 2016), we've been blessed with nanmean, which ignores nan values:

>>> np.nanmean(b)
3.0

However, when the array has nothing but nan values, it raises a warning:

>>> np.nanmean(a)
nan
C:python-3.4.3libsite-packages
umpylib
anfunctions.py:598: RuntimeWarning: Mean of empty slice
  warnings.warn("Mean of empty slice", RuntimeWarning)

I don't like suppressing warnings; is there a better function I can use to get the behaviour of nanmean without that warning?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I really can't see any good reason not to just suppress the warning.

The safest way would be to use the warnings.catch_warnings context manager to suppress the warning only where you anticipate it occurring - that way you won't miss any additional RuntimeWarnings that might be unexpectedly raised in some other part of your code:

import numpy as np
import warnings

x = np.ones((1000, 1000)) * np.nan

# I expect to see RuntimeWarnings in this block
with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=RuntimeWarning)
    foo = np.nanmean(x, axis=1)

@dawg's solution would also work, but ultimately any additional steps that you have to take in order to avoid computing np.nanmean on an array of all NaNs are going to incur some extra overhead that you could avoid by just suppressing the warning. Also your intent will be much more clearly reflected in the code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...