Time for action – comparing arrays

Let's compare two arrays with the functions we just mentioned. We will reuse the arrays from the previous Time for action tutorial and add a NaN to them:

  1. Call the array_allclose function:
    print "Pass", np.testing.assert_allclose([0, 0.123456789, np.nan], [0, 0.123456780, np.nan], rtol=1e-7, atol=0)

    The result is:

    Pass None
  2. Call the array_equal function:
    print "Fail", np.testing.assert_array_equal([0, 0.123456789, np.nan], [0, 0.123456780, np.nan])

    An exception is thrown:

    Fail
    Traceback (most recent call last):
      …
    assert_array_compare
    raiseAssertionError(msg)
    AssertionError:
    Arrays are not equal
    
    (mismatch 50.0%)
    x: array([ 0.        ,  0.12345679,         nan])
     y: array([ 0.        ,  0.12345678,         nan])

What just happened?

We compared two arrays with the array_allclose function and the array_equal function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset