Python Pandas:bootstrap 按行而不是整个数据框的置信限

Python Pandas: bootstrap confidence limits by row rather than entire dataframe

我想做的是无论行数如何都按行获得 bootstrap 置信限度,并从 output.I 当前可以对整个数据帧执行此操作的新数据帧,但不是按行。我在实际程序中拥有的数据看起来与我拥有的以下内容相似:

    0   1   2
0   1   2   3
1   4   1   4
2   1   2   3
3   4   1   4

我希望新数据框看起来像这样,具有上下限置信限:

    0   1   
0   1   2   
1   1   5.5 
2   1   4.5 
3   1   4.2 

当前生成的输出如下所示:

     0   1
 0  2.0 2.75

下面的 python 3 代码生成模拟数据帧并为整个数据帧生成 bootstrap 置信限。结果是一个只有 2 个值的新数据框,一个置信上限和一个置信下限,而不是 4 组 2(每行一个)。

import pandas as pd
import numpy as np
import scikits.bootstrap as sci

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
               [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])
print(zz)

x= zz.dtypes
print(x)

a = pd.DataFrame(np.array(zz.values.tolist())[:, :, 0],zz.index, zz.columns)
print(a)
b = sci.ci(a)
b = pd.DataFrame(b)
b = b.T
print(b)

感谢您的帮助。

以下是我最终想出按行创建 bootstrap ci 的答案。

import pandas as pd
import numpy as np
import numpy.random as npr

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
                  [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])

x= zz.dtypes

a = pd.DataFrame(np.array(zz.values.tolist())[:, :, 0],zz.index, zz.columns)
print(a)

def bootstrap(data, num_samples, statistic, alpha):
    n = len(data)
    idx = npr.randint(0, n, (num_samples, n))
    samples = data[idx]
    stat = np.sort(statistic(samples, 1))
    return (stat[int((alpha/2.0)*num_samples)],
            stat[int((1-alpha/2.0)*num_samples)])

cc = list(a.index.values) # informs generator of the number of rows

def bootbyrow(cc):
    for xx in range(1):
            xx = list(a.index.values)
            for xx in range(len(cc)):
                k = a.apply(lambda y: y[xx])
                k = k.values
                for xx in range(1):
                    kk = list(bootstrap(k,10000,np.mean,0.05))   
                    yield list(kk)


abc = pd.DataFrame(list(bootbyrow(cc))) #bootstrap ci by row

# the next 4 just show that its working correctly
a0 = bootstrap((a.loc[0,].values),10000,np.mean,0.05)   
a1 = bootstrap((a.loc[1,].values),10000,np.mean,0.05)
a2 = bootstrap((a.loc[2,].values),10000,np.mean,0.05)  
a3 = bootstrap((a.loc[3,].values),10000,np.mean,0.05)  

print(abc)
print(a0)
print(a1)
print(a2)
print(a3)

scikits.bootstrap 假设数据样本按行排列,而不是按列排列。如果您想要相反的行为,只需使用转置和不组合列的 statfunction

import pandas as pd
import numpy as np
import scikits.bootstrap as sci

zz = pd.DataFrame([[[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]],
               [[1,2],[2,3],[3,6]],[[4,2],[1,4],[4,6]]])
print(zz)

x= zz.dtypes
print(x)

a = pd.DataFrame(np.array(zz.values.tolist())[:, :, 0],zz.index, zz.columns)
print(a)
b = sci.ci(a.T, statfunction=lambda x: np.average(x, axis=0))
print(b.T)