Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

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 section 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 as follows:

    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]))

    The test fails with an AssertionError:

    Fail
    Traceback (most recent call last):
    
    assert_array_compare
        raise AssertionError(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.