Let's say I have a list a = [2, 1, 4, 3, 5]
. I want to do the following:
I define a percentage value r%
. I would like to select r%
of samples having low values and get their indices.
For examples, if r=80
- The output would be the indices of 1, 2, 3, 4
i.e. 1, 0, 3, 2
CodePudding user response:
Use np.percentile
and np.where
a = np.array([2, 1, 4, 3, 5])
r = 80
np.where(a < np.percentile(a, r))
> (array([0, 1, 2, 3]),)
Note: in your example you return the order of the indices as if the elements were sorted. It's not clear if this is important for you but if it is it's easy in NumPy! Just replace the last line with
np.argsort(a)[a < np.percentile(a, r)]
> array([1, 0, 3, 2])
CodePudding user response:
def perc(r, number_list):
# Find number of samples based on the percentage (rounding to closest integer)
number_of_samples = len(number_list) * (r/ 100)
number_list.sort()
return [number_list[index] for index in range(number_of_samples)]