1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Rep Power

    Help with numpy error: ambiguous truth value


    I have this operation on numpy arrays

    non_zero_idx = (target_std > 0) and (warped_std > 0) and (mask.data > 0)
    I thought this would return me the indexes where the elements in all the three arrays is greater than zero.

    However, I get the following error.
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    I googled the error but was unable to understand exactly what the problem is. I was wondering if someone has any idea on how to resolve this.

    Thanks a lot for any help you can give me.

  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Rep Power
    This puzzled me too, first time I saw this ambiguity message.
    Do you want
    array((0,1)) and array((1,1))
    to evaluate to
    array((0,1)) or to True?

    You probably want:

    import numpy
    A = numpy.array((0,1,2,3))
    print((1<A) * (A%2))

    The complete table of dyadic Boolean operations written as common operators <, *, GCD, etceteras is shown here. It also appears in, as I recall, the gtk operations for combining bit maps.

    non_zero_idx = ((target_std > 0) *(warped_std > 0) *(mask.data))
    Last edited by b49P23TIvg; September 12th, 2013 at 07:53 AM. Reason: remove unneeded assignment.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo