Issue
What is the most straightforward way to do the following in python/numpy?
- begin with random array
x
- filter out elements
x < .5
- sort the remaining values by size
- return indices of (original)
x
corresponding to these values
Solution
One solution:
- created sorted index array (argsort)
- create mask for sorted
x
less than threshold - apply mask to sorted index array
example:
import numpy as np
# x = np.random.rand(4)
x = np.array([0.96924269, 0.30592608, 0.03338015, 0.64815553])
solution = np.array([2, 1])
sorted_idx = np.argsort(x)
idx_mask = (x[sorted_idx] < 0.5)
sorted_filtered_idx = sorted_idx[idx_mask]
assert np.all(sorted_filtered_idx == solution)
Answered By - anon01
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.