zoukankan      html  css  js  c++  java
  • np.percentile获取中位数、百分位数

    给定一个递增数组a,求它的中位数。
    np.percentile(a,50)
    中位数就是50%处的数字,也可以获得0%、100%处的数字,0%处的数字就是第一个数字,100%处的数字就是最后一个数字。1/(len(a)-1)*100处的数字就是第2个数字,2/(len(a)-1)*100处的数字就是第3个数字,以此类推。

    import numpy as np
    
    a = np.array([1, 2, 3, 6, 7, 11, 13])
    for ind, v in enumerate(a):
        print("index", ind, "value", v, "percentile", ind / (len(a) - 1) * 100)
        percen = np.percentile(a, ind / (len(a) - 1) * 100)
        assert np.abs(percen - v) < 1e-3, "%s!=%s" % (percen, v)
    
    

    输出为

    index 0 value 1 percentile 0.0
    index 1 value 2 percentile 16.666666666666664
    index 2 value 3 percentile 33.33333333333333
    index 3 value 6 percentile 50.0
    index 4 value 7 percentile 66.66666666666666
    index 5 value 11 percentile 83.33333333333334
    index 6 value 13 percentile 100.0
    

    np.percentile()函数的作用就是求百分位数,从而能够知道数字大致的分布。

    percentile函数原型为:
    numpy.percentile(a, q, axis=None, out=None, overwrite_input=False)

    • a是原始数组,可以是多维数组
    • q为0~100之间的浮点数或者浮点数组,若为浮点数组表示批量查询
    • axis:在a的哪个轴上计算百分位数
  • 相关阅读:
    1. cocos creator 连接服务端
    cocos creator 中的粒子效果
    cocos creator
    5.4 笔记
    事后诸葛亮
    PHP之魔术方法
    结队编程--作业一
    团队作业9——事后分析(Beta版本)
    团队作业8——测试与发布(Beta阶段)
    Beta版本冲刺计划
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/10631660.html
Copyright © 2011-2022 走看看