Home > Net >  ^ operator in numpy python
^ operator in numpy python

Time:10-24

I am not able to understand what is ^ operator in NumPy array with boolean values.

print("----------train_mask before-------------------------------------------")
print (train_mask[10:30])
print(len(train_mask))
train_mask ^= eval_mask
print("----------eval -------------------------------------------")
print (eval_mask[10:30])
print(len(eval_mask))
print("----------train_mask -------------------------------------------")
print (train_mask[10:30])
print(len(train_mask))

output is:

enter image description here

I am not able to understand how that True value is converted to False

CodePudding user response:

^ is the bitwise XOR operator in Python as others have said.

Check out the documentation for numpy.logical_xor, which reads in part:

Compute the truth value of x1 XOR x2, element-wise.

The key point here is “element-wise,” so in your example, since there’s one only one True value in your eval_mask array, only one value is changed in the output.

It is the same as (for these simple 1d arrays):

a = [True, True]
b = [True, False]
for i in range(len(a)):
     a[i] ^= b[i]

CodePudding user response:

^ is a bitwise XOR operator in Python.

Here train_mask ^= eval_mask is equivalent to (train_mask and not eval_mask) or (not train_mask and eval_mask), can also be written as bool(train_mask) ^ bool(eval_mask).

Reference: docs

  • Related