5

I have a list of numpy arrays, and want to check if all the arrays are equal. What is the quickest way of doing this?

I am aware of the numpy.array_equal function (https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array_equal.html), however as far as I am aware this only applies to two arrays and I want to check N arrays against each other.

I also found this answer to test all elements in a list: check if all elements in a list are identical. However, when I try each method in the accepted answer I get an exception (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())

Thanks,

Community
  • 1
  • 1
EngStan
  • 383
  • 2
  • 13
  • 2
    Are your arrays integer or float? There are lots of questions about that `ValueError`. Look at a few of those. – hpaulj May 17 '16 at 16:27

5 Answers5

3

You could simply adapt a general iterator method for your array comparison

def all_equal(iterator):
  try:
     iterator = iter(iterator)
     first = next(iterator)
     return all(np.array_equal(first, rest) for rest in iterator)
  except StopIteration:
     return True

If this is not working, it means that your arrays are not equal.

Demo:

>>> i = [np.array([1,2,3]),np.array([1,2,3]),np.array([1,2,3])]
>>> print(all_equal(i))
True
>>> j = [np.array([1,2,4]),np.array([1,2,3]),np.array([1,2,3])]
>>> print(all_equal(j))
False
Community
  • 1
  • 1
miradulo
  • 28,857
  • 6
  • 80
  • 93
3

You can use np.array_equal() in a list comprehension to compare each array to the first one:

all([np.array_equal(list_of_arrays[0], arr) for arr in list_of_arrays])
jtr
  • 31
  • 2
1

If your arrays are of equal size, this solution using numpy_indexed (disclaimer: I am its author) should work and be very efficient:

import numpy_indexed as npi
npi.all_unique(list_of_arrays)
Eelco Hoogendoorn
  • 10,459
  • 1
  • 44
  • 42
0

@jtr's answer is great, but I would like to suggest a slightly different alternative.

First of all, I think using array_equal is not a great idea, because you could have two arrays of floats and maybe you can end up having very small differences that you are willing to tolerate, but array_equal returns True if and only if the two arrays have the same shape and exact same elements. So let's use allclose instead, which allows to select the absolute and relative tolerances that meet your needs.

Then, I would use the built-in zip function, which makes the code more elegant.

Here is the code:

all([np.allclose(array, array_expected), for array, array_expected in zip(array_list, array_list_expected)])
blunova
  • 2,122
  • 3
  • 9
  • 21
-1

I guess you can use the function unique.

http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.unique.html#numpy.unique

if all sub-arrays in the array is the same, it should return only one item.

Here's better described how to use it.

Find unique rows in numpy.array

Community
  • 1
  • 1
Richard
  • 1,045
  • 7
  • 11