[英]Randomly select rows from numpy array based on a condition
假設我有 2 個數組數組,標簽是 1D,數據是 5D請注意,兩個數組具有相同的第一維。
為了簡化事情,假設標簽只包含 3 個數組:
labels=np.array([[0,0,0,1,1,2,0,0],[0,4,0,0,0],[0,3,0,2,1,0,0,1,7,0]])
讓我們說我有數據陣列(長度= 3),其中每個陣列具有5D形狀,其中每一個的第一尺寸是相同的標簽陣列的陣列的數據列表。
在這個例子中, datalist有 3 個形狀數組:( 8 ,3,100,10,1), ( 5 ,3,100,10,1) 和 ( 10 ,3,100,10,1) 。 這里,每個數組的第一個維度與label中每個數組的長度相同。
現在我想減少每個標簽數組中的零數量並保留其他值。 假設我只想為每個數組保留3 個零。 因此,標簽中每個數組的長度以及數據中每個數組的第一維將是6 、 4和8 。
為了減少每個標簽數組中零的數量,我想隨機選擇並只保留3 個。 現在將使用這些相同的隨機選擇的索引從數據中選擇相應的行。
對於這個例子, new_labels數組將是這樣的:
new_labels=np.array([[0,0,1,1,2,0],[4,0,0,0],[0,3,2,1,0,1,7,0]])
這是我迄今為止嘗試過的:
all_ind=[] #to store indexes where value=0 for all arrays
indexes_to_keep=[] #to store the random selected indexes
new_labels=[] #to store the final results
for i in range(len(labels)):
ind=[] #to store indexes where value=0 for one array
for j in range(len(labels[i])):
if (labels[i][j]==0):
ind.append(j)
all_ind.append(ind)
for k in range(len(labels)):
indexes_to_keep.append(np.random.choice(all_ind[i], 3))
aux= np.zeros(len(labels[i]) - len(all_ind[i]) + 3)
....
....
Here, how can I fill **aux** with the values ?
....
....
new_labels.append(aux)
有什么建議 ?
使用不同長度的 numpy 數組不是一個好主意,因此您需要迭代每個項目並對其執行一些方法。 假設您只想優化該方法,屏蔽在這里可能會很好地工作:
def specific_choice(x, n):
'''leaving n random zeros of the list x'''
x = np.array(x)
mask = x != 0
idx = np.flatnonzero(~mask)
np.random.shuffle(idx) #dynamical change of idx value, quite fast
idx = idx[:n]
mask[idx] = True
return x[mask] # or mask if you need it
列表的迭代比數組的迭代快,因此有效的用法是:
labels = [[0,0,0,1,1,2,0,0],[0,4,0,0,0],[0,3,0,2,1,0,0,1,7,0]]
output = [specific_choice(n, 3) for n in labels]
輸出:
[array([0, 1, 1, 2, 0, 0]), array([0, 4, 0, 0]), array([0, 3, 0, 2, 1, 1, 7, 0])]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.