官方注释
where(condition, [x, y])
Return elements chosen from x
or y
depending on condition
.
condition是一个布尔数组类型的变量,如果为True,yield x
,否则 yield y
这里x和y和condition如果形状不一致,需要广播成为同一个形状。
返回值 是一个数组 里面的元素是 来自x(对应condition为true的位置的元素)或者y(赌赢condition为False的位置的元素)
np.where([[True,False],[True,False]],
[[1,2],[3,4]],
[[9,8],[7,6]])
array([[1,8],[3,4]])
另外一种情况,只有condition,没有x和y,则输出满足条件(即非0)元素的坐标
等价于numpy.nonzero.这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标
最后举个nms代码的例子
def nms(dets,thresh):
x1=dets[:,0]
x2=dets[:,2]
y1=dets[:,1]
y2=dets[:,3]
scores=dets[:,4]
order=scores.argsort()[::-1]
eps=2e-27
area=(x2-x1+eps)*(y2-y1+eps)
res=[]
while order:
i=order[0]
res.append(i)
xx1=np.maximum(x1[i],x1[order[1:]])
xx2=np.minimum(x2[i],x2[order[1:]])
yy1=np.maximum(y1[i],y1[order[1:]])
yy2=np.minimum(y2[i],y2[order[1:]])
inter=max(0,(yy2-yy1+eps)*(xx2-xx1+eps))
iou=inter/(area[i]+area[order[1:]]-inter)
inds=np.where(iou<=thresh)[0]//这里是个一维的iou,就是返回iou小于阈值的dets的序号 因为返回值是一个tuple所以要取[0]
order=order[inds+1]