Ana*_*ory 5 python statistics probability matrix-multiplication pandas
我正在寻找一种在python中存储和使用条件概率的好方法.
我正在考虑使用pandas数据帧.如果某些条件概率X的P(X=A|P1=1, P2=1) = 0.2,P(X=B|P1=2, P2=1) = 0.9等等,我会使用数据框
A B
P1 P2
1 1 0.2 0.8
2 0.5 0.5
2 1 0.9 0.1
2 0.9 0.1
Run Code Online (Sandbox Code Playgroud)
并给予的边缘概率P1和P2系列作为
1 0.4
2 0.6
Name: P1
1 0.7
2 0.3
Name: P2
Run Code Online (Sandbox Code Playgroud)
我想获得系列的边际概率X,即系列
A 0.602
B 0.398
Name: X
Run Code Online (Sandbox Code Playgroud)
我可以得到我想要的东西
X = sum(
sum(
X.xs(i, level="P1")*P1[i]
for i in P1.index
).xs(j)*P2[j]
for j in P2.index
)
X.name="X"
Run Code Online (Sandbox Code Playgroud)
但这是不容易推广到更多的依赖关系,首先之间的不对称xs与level和第二个没有看起来奇怪和往常一样工作时pandas,我非常肯定存在使用它的技巧和方法,更好的解决方案.
pandas如果我以另一种方式表示我的数据,那么这是一个很好的工具,这个计算的最佳方法是什么,它本质上是一个索引张量积pandas?
| 归档时间: |
|
| 查看次数: |
749 次 |
| 最近记录: |