comparing numpy arrays containing NaN

后端 未结 7 1559
情话喂你
情话喂你 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:20

    The easiest way is use numpy.allclose() method, which allow to specify the behaviour when having nan values. Then your example will look like the following:

    a = np.array([1, 2, np.nan])
    b = np.array([1, 2, np.nan])
    
    if np.allclose(a, b, equal_nan=True):
        print 'arrays are equal'
    

    Then arrays are equal will be printed.

    You can find here the related documentation

提交回复
热议问题