Does anyone know how to use heapq.nlargest with a percentage rather than a number? At the moment I have
heapq.nlargest(187030, y)
But this gets me the top 187030 numbers. I need it to get me the top 10% of numbers for each array because not all arrays have 1.8 million.
Cheers
Yes. Behind the curtains a heap is actually a list with certain properties (it uses what is called an implicit data structure).
So we can first take len(y)
to obtain the number of elements. By dividing by 10
, we get the 10% number of elements. So we can use:
heapq.nlargest(len(y)//10, y)
Or in case you want to use a percentage as parameter:
p = 17 # top 17 procent
heapq.nlargest(len(y)*p//100, y)
You can also use a fraction (for instance the top 0.14
):
from math import round
p = 0.14 # top 14 procent
heapq.nlargest(round(len(y)*p), y)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments