43

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?

3
  • How unequal are the arrays? Must they differ in every element, or at least in one? Commented Jul 21, 2016 at 14:39
  • At least one element should be unequal. That is, the content of the two arrays may not be the same. If one element differ, everything is OK. Commented Jul 21, 2016 at 17:13
  • 1
    Then @Eswcvlad's answer is probably the most elegant hack you will find. Commented Jul 21, 2016 at 17:17

6 Answers 6

41

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Neat hack for first method. Second one will only succeed if every element, not just some, is different as far as I can tell. Not sure if that is what OP wants. +1 either way.
Yes, you are right about the second one. Will edit the answer.
The first one does it for me! Thanks!
13

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)

1 Comment

You should probably just use TestCase.assertTrue(np.any(…)) instead (or nose.tools.assert_true(np.any(…)) if you are outside of a TestCase).
10

Not sure why this hasn't been posted, may be I didn't understand the question properly, but what about:

assert not np.array_equal(array1 , array2)

Any reason why you would like to keep it exclusively in the testing module of numpy?

1 Comment

assert_array_equal handles NaN as values, not as uncomparable (always unequal to anything) like usual for floating point
2

Cleaner syntax to @Eswcvlad's answer:

import numpy as np

with np.testing.assert_raises(AssertionError):
    np.testing.assert_array_equal(expected, actual)

Comments

0

Perhaps you usually want to test if something is almost equal (considering decimal precision) and consequently want to test if something is NOT almost equal in some cases. Building on @Mikhail answer (and also using pytest.raises) this would give:

import numpy as np
import pytest
 
with pytest.raises(AssertionError):
   np.testing.assert_almost_equal(...)

Comments

0

Improving the accepted answer, we can make assert_array_not_equal as follows

from numpy.testing import assert_array_equal, assert_raises


def assert_array_not_equal(x, y):
    return assert_raises(AssertionError, assert_array_equal, x, y)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.