Huanian Zhang - 1 year ago 69

Python Question

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

`percentile`

Answer Source

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