pgmpy / pgmpy

Python Library for learning (Structure and Parameter), inference (Probabilistic and Causal), and simulations in Bayesian Networks.

Home Page:https://pgmpy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

meet a question

yjyjddddddd opened this issue · comments

from pgmpy.models import BayesianModel
from pgmpy.estimators import BayesianEstimator

#model = BayesianModel([('Age', 'Pri'), ('Sex', 'Pri'),('Pri','Survived'),('Fare','Pclass'),('Pclass','Survived'),('Cabin','Survived')])
model = BayesianModel([('Age', 'Survived'), ('Sex', 'Survived'),('Fare','Pclass'),('Pclass','Survived'),('Cabin','Survived')])

def showBN(model,save = True):
from graphviz import Digraph
node_attr = dict(
style='filled',
shape='box',
align='left',
fontsize='12',
ranksep='0.1',
height='0.2'
)
dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))
seen = set()
edges=model.edges()
for a,b in edges:
dot.edge(a,b)
if save:
dot.view(cleanup=True)
return dot
showBN(model)
model.fit(train, estimator=BayesianEstimator, prior_type="BDeu") # default equivalent_sample_size=5
from pgmpy.inference import VariableElimination
from pgmpy.inference import CausalInference
model_infer = CausalInference(model)
q = model_infer.query(variables=['Survived'], evidence={'Fare': 0})
print(q['Survived'])

TypeError Traceback (most recent call last)
in
2 from pgmpy.inference import CausalInference
3 model_infer = CausalInference(model)
----> 4 q = model_infer.query(variables=['Survived'], evidence={'Fare': 0})
5 print(q['Survived'])

C:\ProgramData\Anaconda3\lib\site-packages\pgmpy\inference\CausalInference.py in query(self, variables, do, evidence, adjustment_set, inference_algo, show_progress, **kwargs)
625 # Step 3.1: If no do variable specified, do a normal probabilistic inference.
626 if do == {}:
--> 627 return infer.query(variables, evidence, show_progress=False)
628 # Step 3.2: If no adjustment is required, do a normal probabilistic
629 # inference with do variables as the evidence.

C:\ProgramData\Anaconda3\lib\site-packages\pgmpy\inference\ExactInference.py in query(self, variables, evidence, virtual_evidence, elimination_order, joint, show_progress)
314 # Step 3: Prune the network based on variables and evidence.
315 if isinstance(self.model, BayesianNetwork):
--> 316 model_reduced, evidence = self._prune_bayesian_model(variables, evidence)
317 factors = model_reduced.cpds
318 else:

C:\ProgramData\Anaconda3\lib\site-packages\pgmpy\inference\base.py in _prune_bayesian_model(self, variables, evidence)
158
159 # Step 2: Reduce the model to ancestral graph of [variables + evidence]
--> 160 bn = bn.get_ancestral_graph(list(variables) + list(evidence.keys()))
161
162 # Step 3: Since all the CPDs are lost, add them back. Also marginalize them if some

C:\ProgramData\Anaconda3\lib\site-packages\pgmpy\base\DAG.py in get_ancestral_graph(self, nodes)
886 OutEdgeView([('D', 'A'), ('D', 'B')])
887 """
--> 888 return self.subgraph(nodes=self._get_ancestors_of(nodes=nodes))
889
890 def to_daft(

TypeError: subgraph() got an unexpected keyword argument 'nodes'