I have a broadcasted array, which is sorted rowly, and a masked array. I want to get the last n elements (or the first n ones) of each row which are True i.e.:
JavaScript
x
11
11
1
a = np.array([[0.00298, 0.00455, 0.00767, 0.00939, 0.01104, 0.02351, 0.03370],
2
[0.00298, 0.00455, 0.00767, 0.00939, 0.01104, 0.02351, 0.03370],
3
[0.00298, 0.00455, 0.00767, 0.00939, 0.01104, 0.02351, 0.03370]])
4
5
mask = np.array([[1, 0, 0, 1, 1, 0, 1], [0, 1, 0, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0, 0]], dtype=bool)
6
7
# a[mask] --> [0.00298 0.00939 0.01104 0.0337 0.00455 0.02351 0.0337 0.00767 0.00939]
8
9
# needed last two --> [[0.01104 0.0337 ] [0.02351 0.0337 ] [0.00767 0.00939]]
10
# needed first two --> [[0.00298 0.00939] [0.00455 0.02351] [0.00767 0.00939]]
11
Do we have to split the array (using np.cumsum(np.sum(mask, axis=1))
), pad and …?
What will be the best way to do this just with NumPy?
Advertisement
Answer
Using numpy to get the first n True:
JavaScript
1
3
1
n=2
2
a[(np.cumsum(mask, axis=1)<=n)&mask].reshape(-1,n)
3
Output:
JavaScript
1
4
1
array([[0.00298, 0.00939],
2
[0.00455, 0.02351],
3
[0.00767, 0.00939]])
4
Last n:
JavaScript
1
3
1
n=2
2
a[(np.cumsum(mask[:,::-1], axis=1)<=n)[:,::-1]&mask].reshape(-1,n)
3
Output:
JavaScript
1
4
1
array([[0.01104, 0.0337 ],
2
[0.02351, 0.0337 ],
3
[0.00767, 0.00939]])
4
NB. There must be at least n True per row to have the correct final shape