Python | numpy.assert_allclose() method

With the help of numpy.assert_allclose() method, we can get the assertion errors when two array objects are not equal upto the mark by using numpy.assert_allclose().

Syntax : numpy.assert_allclose(actual_array, desired_array)

Return : Return the Assertion error if two array objects are not equal.



Example #1 :
In this example we can see that using numpy.assert_allclose() method, we are able to get the assertion error if two arrays are not equal.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy
import numpy as np
  
# using numpy.assert_allclose() method
gfg1 = [1, 2, 3]
gfg2 = np.array(gfg1)
  
if np.testing.assert_allclose(gfg1, gfg2):
     print("Matched")

chevron_right


Output :

Matched

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# import numpy
import numpy as np
  
# using numpy.assert_allclose() method
gfg1 = [1, 2, 3]
gfg2 = np.array([4, 5, 6])
  
print(np.testing.assert_allclose(gfg1, gfg2))

chevron_right


Output :

Mismatch: 100%
Max absolute difference: 3
Max relative difference: 0.75
gfg1: array([1, 2, 3])
gfg2: array([4, 5, 6])



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.