4

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
  • 343
  • 1
  • 13
  • 1
    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

4 Answers4

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
  • 26,763
  • 6
  • 73
  • 90
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
  • 9,961
  • 1
  • 40
  • 41
1

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
  • 11
  • 1
-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,035
  • 7
  • 10