Python 为什么percentile()方法没有计算适当的百分比?与此类似,如果四舍五入,此数据的第25个百分位应为1.5和2

Python 为什么percentile()方法没有计算适当的百分比?与此类似,如果四舍五入,此数据的第25个百分位应为1.5和2,python,numpy,Python,Numpy,我正在使用此代码计算百分位以进行交叉验证 import numpy as np value = [1, 2, 3, 4, 5, 6] x = np.percentile(value, 25) print(x) 计算四分位数的方法不止一种 numpy的默认计算返回的值与R的summary()函数返回的值匹配 你需要做其中一件事 切换到numpy.percentile计算四分位数的默认方式 为numpy.percentile的参数插值提供一个值,或 编写自己的自定义函数 以numpy.pe

我正在使用此代码计算百分位以进行交叉验证

import numpy as np

value = [1, 2, 3, 4, 5, 6]
x = np.percentile(value, 25)

print(x)

计算四分位数的方法不止一种

numpy的默认计算返回的值与R的
summary()
函数返回的值匹配

你需要做其中一件事

  • 切换到numpy.percentile计算四分位数的默认方式
  • 为numpy.percentile的参数
    插值提供一个值,或
  • 编写自己的自定义函数
以numpy.percentile为单位的
插值的有效值为

我没有建议
插值的值,因为您的问题中没有包含预期的输出。你需要考虑你的决定对所有四分位数的影响,而不仅仅是一个。

(我认为
scipy.stats.percentileofscore()
不适合你。

你是如何确定第25个百分位应该是1.5的?想想这个表达式的结果:
np.percentile(值,[0,25,50,75100])
。检查更新的代码,这就是我使用传统数学公式计算百分位的方式。
import sys
import numpy as np
from numpy import math

def my_percentile(data, percentile):
    n = len(data)
    p = n * percentile / 100
    if p.is_integer():
        return sorted(data)[int(p)]
    else:
        return sorted(data)[int(math.ceil(p)) - 1]

t = [1, 2, 3, 4, 5, 6]
per = my_percentile(t, 25)

print(per)