Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if all values in a numpy array are equal [duplicate]

Tags:

I have a numpy one dimensional array c that is supposed to be filled with the contents of a + b. I'm first executing a + b on a device using PyOpenCL.

I want to quickly determine the correctness of the result array c in python using numpy slicing.

This is what I currently have

def python_kernel(a, b, c):     temp = a + b     if temp[:] != c[:]:         print "Error"     else:         print "Success!" 

But I get the error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

But it seems a.any or a.all will just determine whether the values aren't 0.

What should I do if I want to test if all of the scalers in the numpy array temp are equal to every value in the numpy array c?

like image 691
Robert Avatar asked Aug 18 '11 00:08

Robert


People also ask

How do you check if all values the same in NumPy array?

Check if all elements are equal in a 1D Numpy Array using numpy. all() This confirms that all values in the array are the same.

How do you check if all values in an array are equal in Python?

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.

What is the function to check whether two arrays are equal in NumPy?

True if two arrays have the same shape and elements, False otherwise.

Are two NumPy arrays equal?

Comparing two NumPy arrays determines whether they are equivalent by checking if every element at each corresponding index is the same. Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object.


1 Answers

Why not just use numpy.array_equal(a1, a2)[docs] from NumPy's functions?

like image 113
Amber Avatar answered Oct 13 '22 21:10

Amber