【问题标题】:calculate 95 percentile on multiple lists在多个列表上计算 95 个百分位
【发布时间】:2019-12-05 19:57:27
【问题描述】:

我想在这样的多个列表中计算 95 个百分位数:

import numpy as np
list_a = [0, 5, 10, 2, 3, 5]
list_b = [0, 0, 6, 5, 4, 4]

list_t = []
list_t.extend(list_a)
list_t.extend(list_b)
list_t_pc = np.percentile(list_t, 95)
print('percentile of list total percentile : '+str(list_t_pc))

输出:

percentile of list total : 7.799999999999997

但就我而言,我每天都在收集数据,我无法将所有值保存在列表中。我可以按天保存一个值(例如平均值、最大值或百分比)。所以要得到最接近的 95% 的值:

import numpy as np
day_1 = [0, 5, 10, 2, 3, 5]
day_2 = [0, 0, 6, 5, 4, 4]

day_1_pc = np.percentile(day_1, 95)
day_2_pc = np.percentile(day_2, 95)

list_pc = [day_1_pc, day_2_pc]
print('percentile of percentile : '+str(np.percentile(list_pc, 95)))

输出:

percentile of percentile : 8.6

有没有办法计算出更接近的值?

【问题讨论】:

  • 如果我正确理解您的问题,请在计算百分位数之前加入列表。 np.percentile(list_a + list_b,95)
  • 我不明白你的问题。请改写它以使其更易于解析,并制定一个更准确的问题,即您想要计算什么。
  • 我编辑的帖子可能更清晰
  • 为什么不能保存之前的结果?你不能将它存储在文件或类似的东西中吗?以及如何只保存前几天的单个值?
  • 以前的结果保存在数据库中,但我受到条目大小的限制。所以我只保存了几个值,以免达到存储限制。

标签: python python-3.6 percentile


【解决方案1】:

有多种计算百分位数的方法。 如果您考虑在 numpy 中计算百分位数的方式,也许会有所帮助。 这里是方法的描述 https://en.wikipedia.org/wiki/Percentile#The_linear_interpolation_between_closest_ranks_method

我编写了一个简单的程序来模拟所使用的算法。

#! -*- coding: utf-8 -*-
import sys
import math

def percentile(inList,value):
    sList=sorted(inList)
    x=len(sList)
    rank=(value/100.0*(x-1))+1
    frac,whole=math.modf(rank)
    a=sList[int(whole)-1]
    b=sList[int(whole)]
    c=frac*(b-a)
    p=a+c
    return p


list_a = [0, 5, 10, 2, 3, 5]
list_b = [0, 0, 6, 5, 4, 4]

print percentile(list_a,95)
print percentile(list_b,95)
print percentile(list_a+list_b,95)
listc=[8.75,5.75]
print percentile(listc,95)

【讨论】:

  • 这对用户有什么帮助?他已经在使用 numpy 的百分位函数了,他的问题不在这里。
  • numpys 百分位函数是一个黑盒子。如果他明白,里面发生了什么,他更有可能找到一种方法来得到他想要的东西。也许他可以尝试不同的变体来计算百分位数,或者调整算法。但恐怕他的想法根本行不通。
  • 我不知道它是如此的黑匣子。但我们都同意用户正在做的事情根本不起作用。
猜你喜欢
  • 2011-10-10
  • 2022-12-13
  • 1970-01-01
  • 2020-02-14
  • 2013-06-08
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
相关资源
最近更新 更多