您的位置:首页 > 编程语言 > Python开发

numpy ndarray 取出满足特定条件的某些行实例

2019-12-06 12:09 2735 查看

在进行物体检测的ground truth boxes annotations包围框坐标数据整理时,需要实现这样的功能:

numpy里面,对于N*4的数组,要实现对于每一行,如果第3列和第1列数值相等或者第2列和第0列数值相等,就删除这一行,要返回保留下来的numpy数组 shape M*4

对于numpy数组的操作要尽量避免for循环,因为numpy数组支持布尔索引。

import numpy as np

a1=np.array(
[1,0,1,5]
)
a2=np.array(
[0,8,5,8]
)
center=np.random.randint(0,10,size=(3,4))
# print(a1.shape,a2.shape,center.shape)
b=np.vstack((a1,center,a2))
'''

numpy vstack 所输入的参数必须是list或者tuple的iterable对象,在竖直方向上进行数组拼接

其中list或者tuple中的每个元素是numpy.ndarray类型

它们必须具有相同的列数,拼接完成后行数增加

numpy hstack 在水平方向上进行数组拼接

进行拼接的数组必须具有相同的行数,拼接完成后列数增加

'''
print(b.shape,b)
out=b[b[:,3]!=b[:,1]]
out2=out[out[:,2]!=out[:,0]]
print(out2.shape,out2)
'''
(5, 4)
[[1 0 1 5]
[6 9 9 1]
[9 1 6 5]
[2 8 8 1]
[0 8 5 8]]
(3, 4)
[[6 9 9 1]
[9 1 6 5]
[2 8 8 1]]
'''
b1=a1.reshape(-1,1)
b2=a2.reshape(-1,1)
before_list=[]
before_list.append(b1)
before_list.append(center.reshape(4,3))
before_list.append(b2)
out3=np.hstack(before_list)
print(out3.shape)#(4, 5)

以上这篇numpy ndarray 取出满足特定条件的某些行实例就是小编分享给大家的全部内容了,希望能给大家一个参考

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐