numpy “TypeError: ufunc 'bitwise_and' not supported for the input types” when using a dynamically created boolean mask

后端 未结 1 1608
無奈伤痛
無奈伤痛 2021-01-12 05:20

In numpy, if I have an array of floats, dynamically create a boolean mask of where this array equals a particular value and do a bitwise AND with a boolean array, I get an e

相关标签:
1条回答
  • 2021-01-12 05:53

    & has higher precedence than ==, so the expression

    a == 2.0 & b
    

    is the same as

    a == (2.0 & b)
    

    You get the error because bitwise and is not defined for a floating point scalar and a boolean array.

    Add parentheses to get what you expected:

    (a == 2.0) & b
    
    0 讨论(0)
提交回复
热议问题