是否有内置的Numpy / Scipy函数来查找四分位数范围?我自己可以很容易地做到这一点,但是mean()基本上存在sum/len…
mean()
sum/len
def IQR(dist): return np.percentile(dist, 75) - np.percentile(dist, 25)
np.percentile 接受多个百分位参数,这样做会稍微好一些:
np.percentile
q75, q25 = np.percentile(x, [75 ,25]) iqr = q75 - q25
要么
iqr = np.subtract(*np.percentile(x, [75, 25]))
而不是两次致电percentile:
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