gpt4 book ai didi

python: numpy - 使用线性插值计算百分位数

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:26 25 4
gpt4 key购买 nike

在阅读维基百科后,我尝试计算百分位,我实现了简单的公式

def _percentile(numList, percentile):
numList.sort()
n = int(round(percentile * len(numList) + 0.5))
if n > 1:
return numList[n-2]
else:
return 0

但是我想做的是维基百科中提到的插值版本:( http://en.wikipedia.org/wiki/Percentile#Linear_interpolation_between_closest_ranks ) 我在谷歌中搜索并找到了 numpy 但我不认为当我使用它时我得到了正确的值,即使是简单的公式。当我尝试传入值进行插值时,它给了我错误。 (http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.percentile.html)

让我们从以下列表开始:

B = [15, 20, 35, 40, 50]

根据我的方法:我得到代表我正在寻找的百分位数的原始列表的实际值:

>>> print percentile(B, P=0.)
0
>>> print percentile(B, P=0.1)
0
>>> print percentile(B, P=0.2)
15
>>> print percentile(B, P=0.3)
15
>>> print percentile(B, P=0.4)
20
>>> print percentile(B, P=0.5)
20
>>> print percentile(B, P=0.6)
35
>>> print percentile(B, P=0.7)
35
>>> print percentile(B, P=0.8)
40
>>> print percentile(B, P=0.9)
40
>>> print percentile(B, P=0.95)
40
>>> print percentile(B, P=1.0)
50

但是如果我使用 numpy,我不会得到代表原始列表的实际值。

>>> np.percentile(B, 0.1)
15.02
>>> np.percentile(B, 0.2)
15.039999999999999
>>> np.percentile(B, 0.3)
15.06
>>> np.percentile(B, 0.4)
15.08
>>> np.percentile(B, 0.5)
15.1
>>> np.percentile(B, 0.6)
15.120000000000001
>>> np.percentile(B, 0.7)
15.140000000000001
>>> np.percentile(B, 0.8)
15.16
>>> np.percentile(B, 0.9)
15.18
>>> np.percentile(B, 1)
15.199999999999999
>>> np.percentile(B, 10)
17.0
>>> np.percentile(B, 20)
19.0
>>> np.percentile(B, 30)
23.0
>>> np.percentile(B, 40)
29.0
>>> np.percentile(B, 50)
35.0

我的问题是给定一个数组,如何通过使用线性插值技术计算百分位数,从该数组中获取表示百分位数(例如 10、20...100)的值?

最佳答案

我也遇到了同样的问题。对我来说,这很简单...我认为百分位数参数(您称之为 P)是 0.0-1.0 之间的 float ,其中 1.0 代表 100%-百分位数。

我刚刚阅读了手册,发现P的范围是0-100,其中100代表100%百分比。

numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear')

q : float in range of [0,100] (or sequence of floats) Percentile to compute which must be between 0 and 100 inclusive.

http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.percentile.html

希望有帮助!

关于python: numpy - 使用线性插值计算百分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19193018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com