小编典典

您如何在Numpy中找到IQR?

python

是否有内置的Numpy / Scipy函数来查找四分位数范围?我自己可以很容易地做到这一点,但是mean()基本上存在sum/len

def IQR(dist):
    return np.percentile(dist, 75) - np.percentile(dist, 25)

阅读 219

收藏
2020-12-20

共1个答案

小编典典

np.percentile 接受多个百分位参数,这样做会稍微好一些:

q75, q25 = np.percentile(x, [75 ,25])
iqr = q75 - q25

要么

iqr = np.subtract(*np.percentile(x, [75, 25]))

而不是两次致电percentile

In [8]: x = np.random.rand(1e6)

In [9]: %timeit q75, q25 = np.percentile(x, [75 ,25]); iqr = q75 - q25
10 loops, best of 3: 24.2 ms per loop

In [10]: %timeit iqr = np.subtract(*np.percentile(x, [75, 25]))
10 loops, best of 3: 24.2 ms per loop

In [11]: %timeit iqr = np.percentile(x, 75) - np.percentile(x, 25)
10 loops, best of 3: 33.7 ms per loop
2020-12-20