pgmpy/pgmpy_tutorials

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

Closed this issue · 4 comments

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>}

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}) )

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 │
╘═════╧══════════╛

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

@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.