Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy testing assert array NOT equal

We have numpy.testing.assert_array_equal to assert that two arrays are equal.

But what is the best way to do numpy.testing.assert_array_not_equal, that is, to make sure that two arrays are NOT equal?

like image 315
Hallgeir Wilhelmsen Avatar asked Jul 21 '16 13:07

Hallgeir Wilhelmsen


2 Answers

If you want to use specifically NumPy testing, then you can use numpy.testing.assert_array_equal together with numpy.testing.assert_raises for the opposite result. For example:

assert_raises(AssertionError, assert_array_equal, array_1, array_2) 

Also there is numpy.testing.utils.assert_array_compare (it is used by numpy.testing.assert_array_equal), but I don't see it documented anywhere, so use with caution. This one will check that every element is different, so I guess this is not your use case:

import operator  assert_array_compare(operator.__ne__, array_1, array_2) 
like image 189
Eswcvlad Avatar answered Sep 23 '22 17:09

Eswcvlad


I don't think there is anything built directly into the NumPy testing framework but you could just use:

np.any(np.not_equal(a1,a2)) 

and assert true with the built in unittest framework or check with NumPy as assert_equal to True e.g.

np.testing.assert_equal(np.any(np.not_equal(a,a)), True) 
like image 44
Mark Avatar answered Sep 23 '22 17:09

Mark