Numpy : The truth value of an array with more than one element is ambiguous

后端 未结 2 1195
北恋
北恋 2020-12-11 22:34

I am really confused on why this error is showing up. Here is my code:

import numpy as np

x = np.array([0, 0])
y = np.array([10, 10])
a = np.array([1, 6])
b         


        
2条回答
  •  忘掉有多难
    2020-12-11 22:45

    Numpy arrays define a custom equality operator, i.e. they are objects that implement the __eq__ magic function. Accordingly, the == operator and all other functions/operators that rely on such an equality call this custom equality function.

    Numpy's equality is based on element-wise comparison of arrays. Thus, in return you get another numpy array with boolean values. For instance:

    x = np.array([1,2,3])
    y = np.array([1,4,5])
    x == y
    

    returns

    array([ True, False, False], dtype=bool)
    

    However, the in operator in combination with lists requires equality comparisons that only return a single boolean value. This is the reason why the error asks for all or any. For instance:

    any(x==y)
    

    returns True because at least one value of the resulting array is True. In contrast

    all(x==y) 
    

    returns False because not all values of the resulting array are True.

    So in your case, a way around the problem would be the following:

    other_pairs = [p for p in points if all(any(p!=q) for q in max_pair)]
    

    and print other_pairs prints the expected result

    [array([1, 6]), array([3, 7])]
    

    Why so? Well, we look for an item p from points where any of its entries are unequal to the entries of all items q from max_pair.

提交回复
热议问题