你如何在 Numpy 中找到 IQR? [英] How do you find the IQR in Numpy?

查看:31
本文介绍了你如何在 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 进行两次调用:

than making two calls to 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆