用PYTHON实现TP99等中位数统计的功能

# 中位数计算原理

1、先按升序排列 [2s, 10s, 100s, 1000s]

2、找到你需要用做统计的最后一个条目(向高取整)对应的数值,比如:TP50就是第 ceil(4*0.5)=2 个,即 10s ;TP90就是第 ceil(4*0.9)=4 个,即 1000s 。


# 方法一

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


# 方法二

import numpy as np

a = np.array([1,2,3,4,5])

p = np.percentile(a, 50) # 中位数

print p

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pandas 数据分析【转】 frompandasimportSeries, DataFrameimportpan...
    gongdiwudu阅读 1,337评论 0 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,603评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,225评论 4 8
  • 步骤:发微博01-导航栏内容 -> 发微博02-自定义TextView -> 发微博03-完善TextView和...
    dibadalu阅读 3,181评论 1 3