找到其中 95% 数据的 numpy 数组的边缘

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

我有一个包含数万个元素的 numpy 数组。我想找到关键元素,其中 95% 的数据都属于该元素。例如,

import numpy as np
a = np.linspace(1,200,200)

那么95%的数据落在其中的两个元素是5和195。numpy函数percentile只有returns一个值,这不是我想要的。有人知道有什么模块可以做到这一点吗?

这将同时 return 这两个值。事实上,第二个参数可以是一个列表,其中包含您想要计算的百分位数。

np.percentile(a, [2.5, 97.5])

然后要捕获所需范围内的所有数据,请执行以下操作:

lower, upper = np.percentile(a, [2.5, 97.5])
a[(a > lower) & (a < upper)]