Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from pgmpy.models import BayesianModel
...: from pgmpy.inference import VariableElimination
...: from pgmpy.factors import TabularCPD
...:
In [2]: restaurant = BayesianModel([('location', 'cost'),
...: ('quality', 'cost'),
...: ('cost', 'no_of_people'),
...: ('location', 'no_of_people')])
...:
In [3]: cpd_location = TabularCPD('location', 2, [[0.6, 0.4]])
In [4]: cpd_quality = TabularCPD('quality', 3, [[0.3, 0.5, 0.2]])
In [5]: cpd_cost = TabularCPD('cost', 2,
...: [[0.8, 0.6, 0.1, 0.6, 0.6, 0.05],
...: [0.2, 0.1, 0.9, 0.4, 0.4, 0.95]],
...: ['location', 'quality'], [2, 3])
In [6]: cpd_no_of_people = TabularCPD('no_of_people', 2,
...: [[0.6, 0.8, 0.1, 0.6],
...: [0.4, 0.2, 0.9, 0.4]],
...: ['cost', 'location'], [2, 2])
In [7]: restaurant.add_cpds(cpd_location, cpd_quality,
...: cpd_cost, cpd_no_of_people)
In [8]: restaurant_inference = VariableElimination(restaurant)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-8-989252db6069> in <module>()
----> 1 restaurant_inference = VariableElimination(restaurant)
~/Research/pythonStudio/pgmpy/pgmpy/inference/base.py in __init__(self, model)
55
56 def __init__(self, model):
---> 57 if not model.check_model():
58 raise ModelError("Model of type {!r} not valid".format(type(model).__name__))
59
~/Research/pythonStudio/pgmpy/pgmpy/models/BayesianModel.py in check_model(self)
241 raise ValueError("CPD associated with %s doesn't have "
242 "proper parents associated with it." % node)
--> 243 if not np.allclose(cpd.marginalize(node, inplace=False).values,
244 np.ones(np.product(cpd.evidence_card)),
245 atol=0.01):
~//Research/pythonStudio/pgmpy/pgmpy/factors/CPD.py in marginalize(self, variables, inplace)
263 self.evidence_card)
264
--> 265 super(TabularCPD, tabular_cpd).marginalize(variables)
266 tabular_cpd.normalize()
267
~/Research/pythonStudio/pgmpy/pgmpy/factors/Factor.py in marginalize(self, variables, inplace)
207 for i in product(*[range(index) for index in assign[assign != -1]]):
208 assign[assign != -1] = i
--> 209 marginalized_values.append(np.sum(factor.values[factor._index_for_assignment(assign)]))
210 factor.values = np.array(marginalized_values)
211 for variable in variables:
~/Research/pythonStudio/pgmpy/pgmpy/factors/Factor.py in _index_for_assignment(self, assignment)
414 """
415 assignment = np.array(assignment)
--> 416 card_cumprod = np.delete(np.concatenate((np.array([1]), np.cumprod(self.cardinality[::-1])), axis=1)[::-1], 0)
417 if -1 in assignment:
418 indexes = np.where(assignment == -1)[0]
IndexError: axis 1 out of bounds [0, 1)
In [9]:
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from pgmpy.models import BayesianModel
...: from pgmpy.inference import VariableElimination
...: from pgmpy.factors import TabularCPD
...:
In [2]: restaurant = BayesianModel([('location', 'cost'),
...: ('quality', 'cost'),
...: ('cost', 'no_of_people'),
...: ('location', 'no_of_people')])
...:
In [3]: cpd_location = TabularCPD('location', 2, [[0.6, 0.4]])
In [4]: cpd_quality = TabularCPD('quality', 3, [[0.3, 0.5, 0.2]])
In [5]: cpd_cost = TabularCPD('cost', 2,
...: [[0.8, 0.6, 0.1, 0.6, 0.6, 0.05],
...: [0.2, 0.1, 0.9, 0.4, 0.4, 0.95]],
...: ['location', 'quality'], [2, 3])
In [6]: cpd_no_of_people = TabularCPD('no_of_people', 2,
...: [[0.6, 0.8, 0.1, 0.6],
...: [0.4, 0.2, 0.9, 0.4]],
...: ['cost', 'location'], [2, 2])
In [7]: restaurant.add_cpds(cpd_location, cpd_quality,
...: cpd_cost, cpd_no_of_people)
In [8]: restaurant_inference = VariableElimination(restaurant)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-8-989252db6069> in <module>()
----> 1 restaurant_inference = VariableElimination(restaurant)
~/Research/pythonStudio/pgmpy/pgmpy/inference/base.py in __init__(self, model)
55
56 def __init__(self, model):
---> 57 if not model.check_model():
58 raise ModelError("Model of type {!r} not valid".format(type(model).__name__))
59
~/Research/pythonStudio/pgmpy/pgmpy/models/BayesianModel.py in check_model(self)
241 raise ValueError("CPD associated with %s doesn't have "
242 "proper parents associated with it." % node)
--> 243 if not np.allclose(cpd.marginalize(node, inplace=False).values,
244 np.ones(np.product(cpd.evidence_card)),
245 atol=0.01):
~//Research/pythonStudio/pgmpy/pgmpy/factors/CPD.py in marginalize(self, variables, inplace)
263 self.evidence_card)
264
--> 265 super(TabularCPD, tabular_cpd).marginalize(variables)
266 tabular_cpd.normalize()
267
~/Research/pythonStudio/pgmpy/pgmpy/factors/Factor.py in marginalize(self, variables, inplace)
207 for i in product(*[range(index) for index in assign[assign != -1]]):
208 assign[assign != -1] = i
--> 209 marginalized_values.append(np.sum(factor.values[factor._index_for_assignment(assign)]))
210 factor.values = np.array(marginalized_values)
211 for variable in variables:
~/Research/pythonStudio/pgmpy/pgmpy/factors/Factor.py in _index_for_assignment(self, assignment)
414 """
415 assignment = np.array(assignment)
--> 416 card_cumprod = np.delete(np.concatenate((np.array([1]), np.cumprod(self.cardinality[::-1])), axis=1)[::-1], 0)
417 if -1 in assignment:
418 indexes = np.where(assignment == -1)[0]
IndexError: axis 1 out of bounds [0, 1)
In [9]: