首页 > 解决方案 > Python:Numpy 百分位数令人困惑的结果

问题描述

当使用 Python Numpy 计算分位数/百分位数时,结果有点奇怪,如下所示。我对此感到很困惑,有人可以解释为什么吗?

import numpy as np

x = range(1,1031)
x = np.array(x)

np.percentile(x,1,interpolation='lower')
# 11
np.percentile(x,1,interpolation='higher')
# 12

np.percentile(x,0.972,interpolation='lower')
# 11
np.percentile(x,0.972,interpolation='higher')
# 12

np.percentile(x,0.971,interpolation='lower')
# 10
np.percentile(x,0.971,interpolation='higher')
# 11

我预计从 1 到 1030 的数字的较低和较高的 1% 分位数是 10 和 11,但实际结果是 11 和 12。

标签: pythonnumpy

解决方案


想象一下,您的数组是x = range(1,1002)(即 1 到 1001,包括)。那么 1 是 0% 百分位数,1001 是 100%。中位数是 501,它应该是 50% 的百分位数。从这个模式中,您可以插值得到 p% 百分位数应该是 10p+1。特别是,1% 的百分位数应该是 11。

现在,由于您的实际数组是range(1,1031),所以 1% 百分位数不应该稍高一些吗?


推荐阅读