您如何在Numpy中找到IQR? [英] How do you find the IQR in Numpy?
本文介绍了您如何在Numpy中找到IQR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有内置的Numpy/Scipy函数来查找四分位数间距?我自己可以很容易地做到这一点,但是mean()
存在,基本上就是sum/len
...
Is there a baked-in Numpy/Scipy function to find the interquartile range? I can do it pretty easily myself, but mean()
exists which is basically sum/len
...
def IQR(dist):
return np.percentile(dist, 75) - np.percentile(dist, 25)
推荐答案
np.percentile
采用多个百分位数参数,这样做会更好:
np.percentile
takes multiple percentile arguments, and you are slightly better off doing:
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
这篇关于您如何在Numpy中找到IQR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文