BayesianModel 的变量消除 inference.map_query 错误
variable elimination inference.map_query error for BayesianModel
我试图根据证据计算变量的 MAP 查询。
from pgmpy.inference import VariableElimination
from pgmpy.models import BayesianModel
import numpy as np
import pandas as pd
values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 5)),
columns=['A', 'B', 'C', 'D', 'E'])
model = BayesianModel([('A', 'B'), ('C', 'B'), ('C', 'D'), ('B', 'E')])
model.fit(values)
inference = VariableElimination(model)
phi_query = inference.map_query(['A', 'B'], evidence= {'B':1})
这给了我一个错误:
Finding Elimination Order: : 100%|██████████| 3/3 [00:00<00:00, 651.66it/s]
Eliminating: E: 100%|██████████| 3/3 [00:00<00:00, 309.08it/s]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-0e47cda916c1> in <module>()
8 model.fit(values)
9 inference = VariableElimination(model)
---> 10 phi_query = inference.map_query(['A', 'B'], evidence= {'B':1})
/usr/local/lib/python3.6/dist-packages/pgmpy/inference/ExactInference.py in map_query(self, variables, evidence, elimination_order, show_progress)
360 return_dict = {}
361 for var in variables:
--> 362 return_dict[var] = map_query_results[var]
363 return return_dict
364
KeyError: 'B'
根据文档:
Parameters variables (list) – list of variables over which we want to
compute the max-marginal.
evidence (dict) – a dict key, value pair as {var:
state_of_var_observed} None if no evidence
elimination_order (list) – order of variable eliminations (if nothing
is provided) order is computed automatically
那么我哪里出错了,为什么会出现这个错误?
编辑:
pgmpy 版本:0.1.9
可能是因为你使用的pgmpy版本。试试这个
!pip install pgmpy==0.1.6
然后是 运行 代码。
问题是您也在查询变量中传递了证据变量,并且没有任何检查来正确处理这种情况。您已经知道 B
的状态为 1
因为它是证据,您只需要查询 A
为:
>>> phi_query = inference.map_query(['A'], evidence= {'B':1})
>>> print(phi_query)
{'A': 1}
我试图根据证据计算变量的 MAP 查询。
from pgmpy.inference import VariableElimination
from pgmpy.models import BayesianModel
import numpy as np
import pandas as pd
values = pd.DataFrame(np.random.randint(low=0, high=2, size=(1000, 5)),
columns=['A', 'B', 'C', 'D', 'E'])
model = BayesianModel([('A', 'B'), ('C', 'B'), ('C', 'D'), ('B', 'E')])
model.fit(values)
inference = VariableElimination(model)
phi_query = inference.map_query(['A', 'B'], evidence= {'B':1})
这给了我一个错误:
Finding Elimination Order: : 100%|██████████| 3/3 [00:00<00:00, 651.66it/s]
Eliminating: E: 100%|██████████| 3/3 [00:00<00:00, 309.08it/s]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-0e47cda916c1> in <module>()
8 model.fit(values)
9 inference = VariableElimination(model)
---> 10 phi_query = inference.map_query(['A', 'B'], evidence= {'B':1})
/usr/local/lib/python3.6/dist-packages/pgmpy/inference/ExactInference.py in map_query(self, variables, evidence, elimination_order, show_progress)
360 return_dict = {}
361 for var in variables:
--> 362 return_dict[var] = map_query_results[var]
363 return return_dict
364
KeyError: 'B'
根据文档:
Parameters variables (list) – list of variables over which we want to compute the max-marginal.
evidence (dict) – a dict key, value pair as {var: state_of_var_observed} None if no evidence
elimination_order (list) – order of variable eliminations (if nothing is provided) order is computed automatically
那么我哪里出错了,为什么会出现这个错误?
编辑: pgmpy 版本:0.1.9
可能是因为你使用的pgmpy版本。试试这个
!pip install pgmpy==0.1.6
然后是 运行 代码。
问题是您也在查询变量中传递了证据变量,并且没有任何检查来正确处理这种情况。您已经知道 B
的状态为 1
因为它是证据,您只需要查询 A
为:
>>> phi_query = inference.map_query(['A'], evidence= {'B':1})
>>> print(phi_query)
{'A': 1}