numpy ndarray 取出滿足特定條件的某些行例項
阿新 • • 發佈:2020-01-09
在進行物體檢測的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,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 取出滿足特定條件的某些行例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。