用numpy的ma模块用nan计算

时间:2011-10-27 16:36:05

标签: python numpy

我不明白这个numpy.ma.max(min,mean等)

的行为
import numpy as np
arr = np.ma.array([0,np.nan,1])
np.ma.max(arr)
-> nan

我认为这应该返回一个不包括nan的值?我能获得真正价值的唯一方法是

np.nanmax(np.asarray(arr))

这是对的,还是我错误地使用numpy.ma.max

2 个答案:

答案 0 :(得分:1)

您需要创建遮罩:

import numpy as np
arr = np.ma.array([0,np.nan,1])
print(np.ma.max(arr))
# >>>nan    # since there is no mask
marr = np.ma.masked_array([0,np.nan,1], np.isnan(arr))
print(np.ma.max(marr))
# >>>1.0    # since the mask tells mask to ignore the nan. The max of the rest (0,1) is 1.

答案 1 :(得分:1)

创建掩码的直接方法是使用np.ma.masked_invalid函数(请参阅。http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.masked_invalid.html#numpy.ma.masked_invalid

以下是一个例子:

# Makes example reproducible 
np.random.seed(seed=1337)
# Generate some data
X = np.random.random((5,5))
X[X > .5] = np.nan
print X
array([[ 0.26202468,  0.15868397,  0.27812652,  0.45931689,  0.32100054],
       [        nan,  0.26194293,         nan,         nan,  0.11527423],
       [ 0.38627507,         nan,  0.12505793,         nan,  0.44322487],
       [        nan,         nan,  0.36126157,  0.41610394,         nan],
       [        nan,  0.18780841,  0.28816715,         nan,  0.49964826]])
# Mask will hide both np.nan and np.inf values
masked_X = np.ma.masked_invalid(X, copy=False)
# Voila
print np.max(masked_X, axis=0)
masked_array(data = [0.38627506863435945 0.26194292556514465 0.36126157241743073
 0.45931688721456665 0.49964826137201246],
             mask = [False False False False False],
       fill_value = 1e+20)