Huanian Zhang Huanian Zhang - 6 months ago 13x
Python Question

Find the edges of a numpy array that 95% data within them

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)

then the two elements within which 95% data fall are 5 and 195. The numpy function
only returns one value, which is not what I want. Any one knows any module to do that?


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)]