[英]Fastest way to filter values in np array based on changing threshold
我想基於某些閾值過濾數組arr
。
arr = np.array([2,2,2,2,2,5,5,5,1])
thresholds = np.array([4,1])
我想篩選arr
基於值thresholds
時在值arr
是大於閾值
我的想法是為每個閾值創建一個掩碼
預期結果:
# [[False False False False False True True True False]
# [ True True True True True True True True False]]
在Python中實現它的一種方法:
mask = [True if x>condi else False for condi in thresholds for x in arr]
mask = np.reshape(mask,(2,9))
然后通過filteredarr = arr[mask[i]]
得到過濾后的數組,其中i
是相關閾值的索引
在Python中有沒有更好的方法(性能明智)? 特別是我正在處理大數組(對於arr來說len約為250000,還沒有針對thresholds
特定len,但我期待一個大陣列)?
編輯:數據上預期的最終輸出是[array([5, 5, 5]), array([2, 2, 2, 2, 2, 5, 5, 5])]
使用可以很容易地獲得掩模
mask = arr[None,:]>thresholds[:,None]
mask
# Output
# array([[False, False, False, False, False, True, True, True, False],
# [ True, True, True, True, True, True, True, True, False]], dtype=bool)
我們的想法是通過使用None
(與np.newaxis
相同)添加一個額外的軸來炸毀維度,然后逐個元素地比較數組。
一旦我們有了掩碼,我們就可以使用各種方法過濾數據,其中選擇很大程度上取決於您的問題:
當然可以
res = [arr[m] for m in mask] # [array([5, 5, 5]), array([2, 2, 2, 2, 2, 5, 5, 5])]
為了獲得帶有過濾數據的列表,但它通常很慢。
如果你有進一步的數值計算,我會創建一個masked array
,其中只考慮過濾后的數據:
m = np.zeros_like(mask).astype(np.int) m[:] = arr res = np.ma.masked_where(~mask,m)
現在,每條線根據相應的閾值對應於過濾的數據。 蒙版數組允許您繼續使用諸如mean
或std
類的許多函數
res.mean(axis=1) # masked_array(data = [5.0 3.125], # mask = [False False], # fill_value = 1e+20) res.mean(axis=1).compressed() # array([ 5. , 3.125])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.