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的哪个轴上计算百分位数
  • 相关阅读:
    CUDA_获取指定设备
    CUDA_常量内存
    CUDA_全局内存及访问优化
    CUDA_共享内存、访存机制、访问优化
    CUDA_寄存器和局部存储器
    CUDA_存储器模型
    CUDA常用概念及注意点
    迅雷笔试题_红黑积木求和
    迅雷笔试题_素勾股数的个数
    【Spring源码分析】Bean的生命周期
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/10631660.html
Copyright © 2011-2022 走看看