用Python实现tp99等中位数统计的功能


=Start=

缘由:

之前在博客中记录过一篇文章「为什么性能监控指标用TP50/90/99等百分位数而非平均数」,简单说明了tp90/tp99等指标的含义和作用,但是没有用代码实现相应的功能,前段时间刚好有响应时间统计的需要,根据网上的一些代码整理了一下,放在这里,方便以后使用。

正文:

参考解答:
# 方法一
def percentile(N, percent, key=lambda x:x):
    """
    Find the percentile of a list of values.

    @parameter N - is a list of values. Note N MUST BE already sorted.
    @parameter percent - a float value from 0.0 to 1.0.
    @parameter key - optional key function to compute value from each element of N.

    @return - the percentile of the values
    """
    if not N:
        return None
    k = (len(N)-1) * percent
    f = math.floor(k)
    c = math.ceil(k)
    if f == c:
        return key(N[int(k)])
    d0 = key(N[int(f)]) * (c-k)
    d1 = key(N[int(c)]) * (k-f)
    return d0+d1

# 测试demo
time_list = [10, 8, 3, 5, 4, 9, 6, 2, 1, 7]
time_list = sorted(time_list)
time_tp90 = percentile(time_list, 0.9)
time_tp99 = percentile(time_list, 0.99)
time_tp999 = percentile(time_list, 0.999)

&&

# 方法二
import numpy as np
a = np.array([1,2,3,4,5])
p = np.percentile(a, 50) # 中位数
print p

==

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

# 方法一
sum(l) / float(len(l))
# 方法二
print reduce(lambda x, y: x + y, l) / float(len(l))
# 方法三
import numpy as np
print np.mean(l)

 

参考链接:

=END=

,

《“用Python实现tp99等中位数统计的功能”》 有 2 条评论

  1. hive函数—-集合统计函数
    https://my.oschina.net/crxy/blog/420695
    `
    中位数函数: percentile
    语法: percentile(BIGINT col, p)
    返回值: double
    说明: 求准确的第pth个百分位数,p必须介于0和1之间,但是col字段目前只支持整数,不支持浮点数类型

    近似中位数函数: percentile_approx
    语法: percentile_approx(DOUBLE col, p [, B])
    返回值: double
    说明: 求近似的第pth个百分位数,p必须介于0和1之间,返回类型为double,但是col字段支持浮点类型。参数B控制内存消耗的近似精度,B越大,结果的准确度越高。默认为10,000。当col字段中的distinct值的个数小于B时,结果为准确的百分位数
    `

    hive计算分位数
    https://blog.csdn.net/sinat_27339001/article/details/52189843

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注