Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing multiple numpy arrays

how should i compare more than 2 numpy arrays?

import numpy 
a = numpy.zeros((512,512,3),dtype=numpy.uint8)
b = numpy.zeros((512,512,3),dtype=numpy.uint8)
c = numpy.zeros((512,512,3),dtype=numpy.uint8)
if (a==b==c).all():
     pass

this give a valueError, and i am not interested in comparing arrays two at a time.

like image 885
Jayanth Reddy Avatar asked Jun 12 '16 18:06

Jayanth Reddy


People also ask

Can you compare two arrays in Python?

Compare Two Arrays in Python Using the numpy. array_equiv() Method. The numpy. array_equiv(a1, a2) method takes array a1 and a2 as input and returns True if both arrays' shape and elements are the same; otherwise, returns False .

How do you compare 3 arrays in Python?

Combine everything into one array, calculate the absolute diff along the new axis and check if the maximum element along the new dimension is equal 0 or lower than some threshold. This should be quite fast. Show activity on this post. This might work.

How do you check if two NumPy arrays are equal?

To check if two NumPy arrays A and B are equal: Use a comparison operator (==) to form a comparison array. Check if all the elements in the comparison array are True.

How do I compare two NumPy arrays in Python?

Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.


1 Answers

To expand on previous answers, I would use combinations from itertools to construct all pairs, then run your comparison on each pair. For example, if I have three arrays and want to confirm that they're all equal, I'd use:

from itertools import combinations

for pair in combinations([a, b, c], 2):
    assert np.array_equal(pair[0], pair[1])
like image 109
Elsewhere Avatar answered Sep 19 '22 07:09

Elsewhere