>>> newaxis is None
True
>>> x = np.arange(3)
>>> x
array([0, 1, 2])
>>> x[:, newaxis]
array([[0],
[1],
[2]])
将一个矩阵增加一维,在矩阵乘法中把两个一位矩阵相乘
此外在切片中numpy.newaxis效果和None是一样的,None是它的别名
参考
附一个写的非常好的代码:
bbox_a:shape(N,4)
bbox_b:shape(K,4)
tl = np.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
#(N,K,2)
br = np.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:])
#(N,K,2)
area_i = np.prod(br - tl, axis=2) * (tl < br).all(axis=2)
#(N,K)
area_a = np.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1)
#(N,)
area_b = np.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1)
#(K,)
return area_i / (area_a[:, None] + area_b - area_i)
#(N,K)