You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用numpy计算百分位数时如何舍弃低位值而非高位值

实现方法

你原来调用np.percentile默认计算的是升序排列下90%的数值都小于等于该分位值,对应舍弃最高10%的数值,所以得到9.1。你要实现舍弃最低10%、取剩余数值的最小值得到2,有两种常用方案:

方案1:直接调整np.percentile参数

把分位值改为10,同时指定插值方法为higher即可:

import numpy as np

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# method='higher'表示取大于等于分位位置的最小实际元素值
p = np.percentile(a, 10, method='higher')
print(p)
# 输出:2

方案2:手动排序截断(适配自定义截断逻辑)

如果后续需要调整截断规则,可以先排序再手动截取低位后取首个元素:

import numpy as np

def get_min_after_truncate_low(arr, truncate_percent):
    sorted_arr = np.sort(arr)
    # 计算需要截断的低位元素数量
    truncate_count = int(np.ceil(len(sorted_arr) * truncate_percent / 100))
    return sorted_arr[truncate_count]

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 传入要舍弃的低位占比,这里填10即舍弃最低10%
p = get_min_after_truncate_low(a, 10)
print(p)
# 输出:2

内容的提问来源于stack exchange,提问作者Agenobarb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:45:03