[英]Python: Numpy array, Take every 100th timepoint for each cell ID
我有以下問題。 我有一個數組(前3個元素是x,y,z坐標,第4個元素是時間,第5個元素是單元格ID(1-9)。現在我只希望每個時間點的第50個或第100個元素FOR EACH CELL ID。這不是重復的問題!因此,對於單元格ID 1,我想要時間點為50,100,150的行,對於單元格ID2,我希望時間點為50,100,150,以此類推!
示例數組
arr = np.array([['2.0', '29.0', '24.0', '0.0', '1'],
['0.0', '18.0', '4.0', '0.0', '2'],
['16.0', '9.0', '0.0', '9990.0', '7'],
['20.0', '23.0', '31.0', '9990.0', '8'],
['65.0', '30.0', '20.0', '0.0', '9']
['16.0', '9.0', '0.0', '9990.0', '9']])
你可以做這樣的事情-
# Convert the fourth col to int type
a = arr[:,3].astype(float).astype(int)
steps = [15,50] # Define step intervals
# Get time-stepped array intervaled at either of the elements from steps
range_arr = np.arange(a.max()+1)
r = range_arr[((np.arange(a.max()+1)[:,None]%steps)==0).any(1)]
# Get mask of matches. Index into input array for final output.
out = arr[np.in1d(a,r)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.