python - 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。
解决方案
想象一下,您的数组是x = range(1,1002)
(即 1 到 1001,包括)。那么 1 是 0% 百分位数,1001 是 100%。中位数是 501,它应该是 50% 的百分位数。从这个模式中,您可以插值得到 p% 百分位数应该是 10p+1。特别是,1% 的百分位数应该是 11。
现在,由于您的实际数组是range(1,1031)
,所以 1% 百分位数不应该稍高一些吗?
推荐阅读
- mongodb - 使用展开创建单独的文档后如何计算值的总和?
- php - 使用 PHP 编码的网站搜索(使用 .txt 文件作为索引文件)是否容易受到任何攻击(如 SQL 注入和 XSS)?
- arrays - 如何在Ruby中按其中的项目将数组分组
- javascript - 如何将数组的实例传递给函数
- ansible - oneliner 用于压缩列表和字典
- inheritance - 无法调用受保护的基本方法
- javascript - 如何从 json 数据中公开图像
- angular - 可观察的缓存和缓存清除,而不会使 Angular 中的订阅无效
- django - 我无法将 ListModelMixin 类作为动作装饰器进行分页
- autodesk-forge - Revit IFC 导出缺少某些内容