这是一个计算均值的例子
import numpy as np
def average(data):
return sum(data) / len(data)
def bootstrap(data, B, c, func):
"""
计算bootstrap置信区间
:param data: array 保存样本数据
:param B: 抽样次数 通常B>=1000
:param c: 置信水平
:param func: 样本估计量
:return: bootstrap置信区间上下限
"""
array = np.array(data)
n = 50
sample_result_arr = []
for i in range(B):
index_arr = np.random.randint(0, len(array), size=n) # 随机抽取n个从0到len(array)的值作为下标
data_sample = array[index_arr] # 选取对应下标的值,返回的是一个数组
sample_result = func(data_sample)
sample_result_arr.append(sample_result)
a = 1 - c
k1 = int(B * a / 2)# 下界
k2 = int(B * (1 - a / 2))# 上界
auc_sample_arr_sorted = sorted(sample_result_arr)
lower = auc_sample_arr_sorted[k1]
higher = auc_sample_arr_sorted[k2]
return lower, higher
if __name__ == '__main__':
a = range(10000)
a = [item + 10000 for item in a] # 10000.....20000的整数值
data = np.array(a)
result = bootstrap(data, 1000, 0.95, average)
print(result)