opened
marketneutral
username_0
### Subject of the issue
I can no longer get a basic Bayesian Network to work. I recently updated to the latest dev build.

### Your environment
* pgmpy version: Latest dev (I did a git clone)
* Python 3.6.6 :: Anaconda, Inc.
* Linux Debian

### Steps to reproduce

```python
from pgmpy.models import BayesianModel
from pgmpy.factors.discrete import TabularCPD

olympic_model = BayesianModel([
    ('Genetics', 'OlympicTrials'),
    ('Practice', 'OlympicTrials'),
    ('OlympicTrials', 'Offer')
])

genetics_cpd = TabularCPD(
    variable = 'Genetics',
    variable_card = 2,   # cardinality
    values = [[.2, .8]]   # ['good', 'bad']
)

practice_cpd = TabularCPD(
    variable = 'Practice',
    variable_card = 2,
    values = [[.7, .3]]   # ['yes', 'no']
)

offer_cpd = TabularCPD(
    variable = 'Offer',
    variable_card = 2,
    values = [[0.95, 0.8, 0.5],
              [.05, .2, .5]],
    evidence = ['OlympicTrials'],
    evidence_card = [3]
)

olympic_trials_cpd = TabularCPD(
    variable = 'OlympicTrials',
    variable_card = 3,
    values = [[.5, .8, .8, .9],
              [.3, .15, .1, .08],
              [.2, .05, .1, .02]],
    evidence = ['Genetics', 'Practice'],
    evidence_card = [2,2]
)

olympic_model.add_cpds(genetics_cpd, practice_cpd, offer_cpd, olympic_trials_cpd)

from pgmpy.inference import VariableElimination

olympic_infer = VariableElimination(olympic_model)

prob_offer = olympic_infer.query(variables = ['Offer', 'Practice', 'Genetics', 'OlympicTrials'])
print(prob_offer['Offer'])

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-f9ca2db5b78b> in <module>()
      1 # If you knew nothing, what's the probabilty of an offer?
      2 prob_offer = olympic_infer.query(variables = ['Offer', 'Practice', 'Genetics', 'OlympicTrials'])
----> 3 print(prob_offer['Offer'])
      4 print(prob_offer['Genetics'])
      5 print(prob_offer['Practice'])

TypeError: 'DiscreteFactor' object is not subscriptable
```






```


### Expected behaviour
I should get the inferred value. This was working prior to me updating.

### Actual behaviour
Error is thrown
New error in latest build: TypeError: 'DiscreteFactor' object is not subscriptable
issue
created
MamtaVenugopal
username_1
I have the same issue. Is there any solution?
comment
created
ankurankan
username_2
@username_0 @username_1 The behaviour of query methods has been changed. Not the query method returns a joint distribution on all the variables given in `variables`. To get the distribution of individual variables, you will need to call the method separately or marginalize the joint distribution. Something like:
```python
prob_offer = olympic_infer.query(variables = ['Offer'])
prob_practice = olympic_infer.query(variables=['Practice'])
prob_genetics = olympic_infer.query(variables=['Genetics'])
prob_oly = olympic_infer.query(variables=['OlympicTrials'])
```
or
```python
joint = olympic_infer.query(variables = ['Offer', 'Practice', 'Genetics', 'OlympicTrials'])
prob_offer = joint.marginalize(['Practice', 'Genetics', 'OlympicTrials'])
```
comment
created
MamtaVenugopal
username_1
query = fraud_infer.query(variables=['F'], evidence={'J': 0})
print(query['F']) 
This is the query I ran and I am getting 'DiscreteFactor' object is not subscriptable'. 

Additionally, how do I know what version of pgmpy has been installed in my system. Any suggestions would be helpful
comment
created
ankurankan
username_2
@username_1 Now it returns a single `DiscreteFactor` instance, so doing just `print(query)` would work. You can use `pgmpy.__version__` to check the version you are using.
comment
created
marketneutral
username_0
I see... you changed the API for `query` in [this commit](https://github.com/pgmpy/pgmpy/commit/94a59f4d631d27ebcb456a1f90b2d3017e33070b). I think the best way to get back to the old API is simply to pass in `joint=False`. As in

```
prob_offer = olympic_infer.query(
    variables = ['Offer', 'Practice', 'Genetics', 'OlympicTrials'],
     joint=False
)
print(prob_offer['Offer'])                                             
+----------+--------------+
| Offer    |   phi(Offer) |
+==========+==============+
| Offer(0) |       0.8898 |
+----------+--------------+
| Offer(1) |       0.1102 |
+----------+--------------+
```

and 

```python
prob_offer['Offer'].values 
array([0.88976, 0.11024])
```

With regard to the version, I also seeing some problems. `pgmpy.__version__` is not consistent with the `version` tag in `setup.py`. I see you made a new commit to partially fix that. Currently, `pgmpy.__version__` gives `v0.1.8.dev15`, but `setup.py` says `version="0.1.7"`.
comment
created
ankurankan
username_2
My bad, using the `joint` argument would be the simplest way. It totally slipped my mind.

Also, you are right about the version thing. I have fixed it now. Thanks for pointing it out.
comment
created
MamtaVenugopal
username_1
First, I did git clone to install the package. It failed to install pytorch. Then, I installed pytorch. Then again I reinstalled pgympy and then it worked. Hope you can figure what went wrong.
comment
created
marketneutral
username_0
Great. Thank you @username_2 !

@username_1 : There is a bug in the `requirements.txt` file. It lists `pytorch` as a dependency, but the package name for PyTorch from PyPy is `torch` (it's called `pytorch` on conda).
comment
closed
khalibartan
username_3
issue