【621】numpy.array 的逻辑运算

参考:【Python】逻辑运算 VS Numpy数组逻辑运算


  主要是在计算 IoU 的时候,通过逻辑运算,可以更容易的计算 Overlap 与 Union。

# 或
a | b

# 与
a & b

# 异或
a ^ b

# 非(只针对 bool)
~a

  举例如下:

>>> a = np.array([[0, 1, 0],
	         [0, 1, 0],
	         [0, 1, 0]])

>>> b = np.array([[0, 1, 1],
	         [0, 0, 1],
	         [1, 0, 1]])

>>> a
array([[0, 1, 0],
       [0, 1, 0],
       [0, 1, 0]])

>>> b
array([[0, 1, 1],
       [0, 0, 1],
       [1, 0, 1]])

# overlap
>>> a & b
array([[0, 1, 0],
       [0, 0, 0],
       [0, 0, 0]])

# union
>>> a | b
array([[0, 1, 1],
       [0, 1, 1],
       [1, 1, 1]])

>>> a ^ b
array([[0, 0, 1],
       [0, 1, 1],
       [1, 1, 1]])

>>> a & b
array([[0, 1, 0],
       [0, 0, 0],
       [0, 0, 0]])

# 针对 0 和 1 的数组,两者效果一样
>>> a * b == a & b
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]])
原文地址:https://www.cnblogs.com/alex-bn-lee/p/15058358.html