在進行物體檢測的ground truth boxes annotations包圍框坐標數據整理時,需要實現這樣的功能:
numpy里面,對于N*4的數組,要實現對于每一行,如果第3列和第1列數值相等或者第2列和第0列數值相等,就刪除這一行,要返回保留下來的numpy數組 shape M*4
對于numpy數組的操作要盡量避免for循環,因為numpy數組支持布爾索引。
import numpy as npa1=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 取出滿足特定條件的某些行實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網之家。
新聞熱點
疑難解答