What is the most straightforward way to do the following in python/numpy?
x
x < .5
x
corresponding to these valuesWe can get the indices of the sorted elements of a given array with the help of argsort() method. This function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword.
argwhere() function is used to find the indices of array elements that are non-zero, grouped by element. Parameters : arr : [array_like] Input array. Return : [ndarray] Indices of elements that are non-zero.
Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
NumPy's np. argsort is able to do stable sorting through passing kind = 'stable' argument. Also np. argsort doesn't support reverse (descending) order.
Finding the mask of x < 0.5
and x.argsort()
seemed like compulsory here. Once you have those two, you can sort the mask array using the sort indices and use this mask on the sort indices to get back the indices corresponding to sorted indices that satisfy the masked condition. Thus, you would be adding one more line of code, like so -
mask = x < 0.5
sort_idx = x.argsort()
out = sort_idx[mask[sort_idx]]
Sample step-by-step run -
In [56]: x
Out[56]: array([ 0.8974009 , 0.30127187, 0.71187137, 0.04041124])
In [57]: mask
Out[57]: array([False, True, False, True], dtype=bool)
In [58]: sort_idx
Out[58]: array([3, 1, 2, 0])
In [59]: mask[sort_idx]
Out[59]: array([ True, True, False, False], dtype=bool)
In [60]: sort_idx[mask[sort_idx]]
Out[60]: array([3, 1])
Masked arrays are concise (but maybe not especially efficient)
x = np.random.rand(4);
inverse_mask = x < 0.5
m_x = np.ma.array(x, mask=np.logical_not(inverse_mask))
sorted_indeces = m_x.argsort(fill_value=1)
filtered_sorted_indeces = sorted_indeces[:np.sum(inverse_mask)]
One solution:
x
less than thresholdexample:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With