gpt4 book ai didi

python - 如何使用pandas中的DataFrame实现概率边缘化功能?

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:57 24 4
gpt4 key购买 nike

我有一个像这样的概率表:

        BC_array =[np.array(['B=n','B=m','B=s','B=n','B=m','B=s']),np.array(['C=F', 'C=F', 'C=F', 'C=T', 'C=T', 'C=T'])]
pD_BC_array=np.array([[0.9,0.8,0.1,0.3,0.4,0.01],[0.08,0.17,0.01,0.05,0.05,0.01],[0.01,0.01,0.87,0.05,0.15,0.97],[0.01,0.02,0.02,0.6,0.4,0.01]])
pD_BC=pd.DataFrame(pD_BC_array,index=['D=h','D=c','D=s','D=r'],columns=BC_array)
B=n B=m B=s B=n B=m B=s
C=F C=F C=F C=T C=T C=T
D=h 0.90 0.80 0.10 0.30 0.40 0.01
D=c 0.08 0.17 0.01 0.05 0.05 0.01
D=s 0.01 0.01 0.87 0.05 0.15 0.97
D=r 0.01 0.02 0.02 0.60 0.40 0.01

我怎样才能边缘化'C'(将所有'C=F'和'C=T'加在一起)并得到表格:

      B=n   B=m   B=s 
D=h 1.20 1.20 0.11
D=c 0.13 0.22 0.02
D=s 0.06 0.16 1.84
D=r 0.61 0.42 0.03

像这样吗?

最佳答案

您可以调用sum在 df 上并传递参数 axis=1 逐行和 level=0 沿该级别求和:

In [259]:

pD_BC.sum(axis=1, level=0)
Out[259]:
B=m B=n B=s
D=h 1.20 1.20 0.11
D=c 0.22 0.13 0.02
D=s 0.16 0.06 1.84
D=r 0.42 0.61 0.03

关于python - 如何使用pandas中的DataFrame实现概率边缘化功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30097549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com