You need to enable JavaScript to run this app. 使用Bootstrap法计算置信区间得到双数组而非单区间的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

使用Bootstrap法计算置信区间得到双数组而非单区间的问题求助

问题修正方案

错误原因

  • stats.t.interval参数传值错误:loc参数传入了长度为1000的bootstrap均值数组,该参数要求传入单个均值点估计值,函数会为每个传入的loc值单独计算置信区间,因此传入1000个值会返回1000组区间结果,就是你看到的数组形式输出。
  • 缺少必填的scale参数:该参数需要传入bootstrap均值的标准误,即np.std(means, ddof=1),缺失会导致区间计算结果不符合实际。
  • 冗余代码:np.mean([i for i in sublist])可直接简化为np.mean(sublist),无需额外列表推导。
  • 参数命名不规范:函数参数用list命名会覆盖Python内置的list类型,建议改为data。

修正后代码

import random
import numpy as np
from scipy import stats

def bootstrap(data):
    # 生成1000组bootstrap重抽样样本
    randomize = [[random.choice(data) for _ in data] for _ in range(1000)]
    # 计算每组样本的均值,得到1000个bootstrap均值
    means = [np.mean(sublist) for sublist in randomize]
    # 计算95%置信区间
    ci_left, ci_right = stats.t.interval(
        confidence=0.95,
        df=len(means)-1,
        loc=np.mean(means),
        scale=np.std(means, ddof=1)
    )
    return (float(ci_left), float(ci_right))

输出说明

修正后函数返回值为两个单精度浮点数组成的元组,符合(0.607898431, 0.611159753)这类预期格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 04:36:02