I have a numpy array with tens of thousand elements. I want to find the critical elements, within which 95% of the data falls. For example,
import numpy as np
a = np.linspace(1,200,200)
This will return both values at once. In fact, the second argument can be a list with as many percentiles as you'd like to calculate.
np.percentile(a, [2.5, 97.5])
Then to capture all the data within the desired range do this:
lower, upper = np.percentile(a, [2.5, 97.5]) a[(a > lower) & (a < upper)]