pgmpy / pgmpy_notebook

Short Tutorial to Probabilistic Graphical Models(PGM) and pgmpy
http://pgmpy.org/
MIT License
368 stars 213 forks source link

print(infer.query('G') ['G']) causes TypeError: variables must be a list of strings #12

Closed djinnome closed 7 years ago

djinnome commented 7 years ago

In 2. Bayesian Networks on cell 20, the following error occurs:

print( infer.query('G')['G'])

results in the following TypeError:

TypeError: variables must be a list of strings

The code cell should be:

print( infer.query(['G']))

and then it prints the following result:

{'G': <DiscreteFactor representing phi(G:3) at 0x117bb9630>}

djinnome commented 7 years ago

Also on Cell 23:

print(infer.query('G', evidence={'D': 0, 'I': 1})['G'] )

should be:

print(infer.query(['G'], evidence={'D': 0, 'I': 1}) )

djinnome commented 7 years ago

Actually, code cell 20 should be:

print( infer.query(['G'])['G']) which results in:

╒═════╤══════════╕
│ G   │   phi(G) │
╞═════╪══════════╡
│ G_0 │   0.3620 │
├─────┼──────────┤
│ G_1 │   0.2884 │
├─────┼──────────┤
│ G_2 │   0.3496 │
╘═════╧══════════╛

And likewise code cell 23 should be:

print(infer.query(['G'],evidence={'D': 0, 'I': 1})['G'] )

which results in:

╒═════╤══════════╕
│ G   │   phi(G) │
╞═════╪══════════╡
│ G_0 │   0.9000 │
├─────┼──────────┤
│ G_1 │   0.0800 │
├─────┼──────────┤
│ G_2 │   0.0200 │
╘═════╧══════════╛
djinnome commented 7 years ago

Interestingly, the TypeError I received when executing infer.query('G') does not happen when executing infer.map_query('G')

yashu-seth commented 7 years ago

@djionme Yes, you are right. This needs to be fixed. Besides this, I think query and map_query should have a consistent API. (They both should accept list of strings). If you want you can fix this.