Method 1 (same idea as Tai method, but using integer indexing)
Too late to the party, and if my decision is a repeat of the already published decision - ping me and I will delete it.
def meth_agn_v1(x, thresh): idx = np.arange(x.size)[x > thresh] return idx[np.argsort(x[idx])]
Then
In [143]: meth_agn_v1(x, 0.5) Out[143]: array([0, 3, 2])
Method 2 (significant performance improvement)
This uses the same idea as in the last section of my answer (comparison with the Tai method) that integer indexing is faster than logical indexing (to select a small number of expected elements) and eliminates the need to create an initial index.
def meth_agn_v2(x, thresh): idx, = np.where(x > thresh) return idx[np.argsort(x[idx])]
Timing
In [144]: x = np.random.rand(100000) In [145]: timeit meth_jp(x, 0.99) 100 loops, best of 3: 7.43 ms per loop In [146]: timeit meth_alex(x, 0.99) 1000 loops, best of 3: 498 µs per loop In [147]: timeit meth_tai(x, 0.99) 1000 loops, best of 3: 298 µs per loop In [148]: timeit meth_agn_v1(x, 0.99) 1000 loops, best of 3: 232 µs per loop In [161]: timeit meth_agn_v2(x, 0.99) 10000 loops, best of 3: 95 µs per loop
Comparison of v1 method with Tai
My first version of the answer is very similar to Tai's answer, but not identical.
Tai Method, originally published:
def meth_tai(x, thresh): y = np.arange(x.shape[0]) y = y [x > thresh] x = x [x > thresh]
So, my method is different in that indexing of whole arrays is used instead of the Boolean indexing used by Tai. For a small number of selected elements, integer indexing is faster than logical indexing, which makes this method more efficient than the Tai method, even after Ty optimized his code.