在python中使用numpy获得避免nan的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19852586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get mean value avoiding nan using numpy in python
提问by 2964502
How to calculate mean value of an array (A) avoiding nan?
如何计算避免nan的数组(A)的平均值?
import numpy as np
A = [5 nan nan nan nan 10]
M = np.mean(A[A!=nan]) does not work
Any idea?
采纳答案by falsetru
Use numpy.isnan
:
使用numpy.isnan
:
>>> import numpy as np
>>> A = np.array([5, np.nan, np.nan, np.nan, np.nan, 10])
>>> np.isnan(A)
array([False, True, True, True, True, False], dtype=bool)
>>> ~np.isnan(A)
array([ True, False, False, False, False, True], dtype=bool)
>>> A[~np.isnan(A)]
array([ 5., 10.])
>>> A[~np.isnan(A)].mean()
7.5
because you cannot compare nan
with nan
:
因为你无法nan
与nan
:
>>> np.nan == np.nan
False
>>> np.nan != np.nan
True
>>> np.isnan(np.nan)
True
回答by usethedeathstar
An other possibility is the following:
另一种可能性如下:
import numpy
from scipy.stats import nanmean # nanmedian exists too, if you need it
A = numpy.array([5, numpy.nan, numpy.nan, numpy.nan, numpy.nan, 10])
print nanmean(A) # gives 7.5 as expected
i guess this looks more elegant (and readable) than the other solution already given
我想这看起来比已经给出的其他解决方案更优雅(和可读)
edit: apparently (@Jaime) reports that this functionality already exists directly in the latest numpy
(1.8) as well, so no need to import scipy.stats
anymore if you have that version of numpy
:
编辑:显然(@Jaime)报告说这个功能也直接存在于最新的numpy
(1.8)中,所以import scipy.stats
如果你有那个版本,就不需要了numpy
:
import numpy
A = numpy.array([5, numpy.nan, numpy.nan, numpy.nan, numpy.nan, 10])
print numpy.nanmean(A)
the first solution works also for people who dont have the latest version of numpy
(like me)
第一个解决方案也适用于没有最新版本的人numpy
(比如我)