How do you find the IQR in Numpy?

前端 未结 3 566
夕颜
夕颜 2020-12-08 08:56

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<

3条回答
  •  时光取名叫无心
    2020-12-08 09:34

    np.percentile takes multiple percentile arguments, and you are slightly better off doing:

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

    or

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

    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
    

提交回复
热议问题