comparing numpy arrays containing NaN

后端 未结 7 1563
情话喂你
情话喂你 2020-11-28 09:27

For my unittest, I want to check if two arrays are identical. Reduced example:

a = np.array([1, 2, np.NaN])
b = np.array([1, 2, np.NaN])
if np.all(a==b):
          


        
7条回答
  •  情歌与酒
    2020-11-28 10:02

    You could use numpy masked arrays, mask the NaN values and then use numpy.ma.all or numpy.ma.allclose:

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.all.html

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.allclose.html

    For example:

    a=np.array([1, 2, np.NaN])
    b=np.array([1, 2, np.NaN])
    np.ma.all(np.ma.masked_invalid(a) == np.ma.masked_invalid(b)) #True
    

提交回复
热议问题