Ejemplo n.º 1
0
class TestGraphSkeleton(unittest.TestCase):
    def setUp(self):
        self.instance = GraphSkeleton()
        self.instance.V = [1, 2, 3, 4, 5]
        self.instance.E = [[5, 1], [1, 2]]

    def test_getparents(self):
        self.assertEqual(self.instance.getparents(1), [5])
        self.assertEqual(self.instance.getparents(4), [])

    def test_getchildren(self):
        self.assertEqual(self.instance.getchildren(5), [1])
        self.assertEqual(self.instance.getchildren(4), [])

    def test_toporder(self):
        self.instance.toporder()
        self.assertTrue(self.instance.V.index(5) < self.instance.V.index(1))
        self.assertTrue(self.instance.V.index(5) < self.instance.V.index(2))
Ejemplo n.º 2
0
class TestGraphSkeleton(unittest.TestCase):

    def setUp(self):
        self.instance = GraphSkeleton()
        self.instance.V = [1,2,3,4,5]
        self.instance.E = [[5,1],[1,2]]

    def test_getparents(self):
        self.assertEqual(self.instance.getparents(1), [5])
        self.assertEqual(self.instance.getparents(4), [])

    def test_getchildren(self):
        self.assertEqual(self.instance.getchildren(5), [1])
        self.assertEqual(self.instance.getchildren(4), [])

    def test_toporder(self):
        self.instance.toporder()
        self.assertTrue(self.instance.V.index(5)<self.instance.V.index(1))
        self.assertTrue(self.instance.V.index(5)<self.instance.V.index(2))
Ejemplo n.º 3
0
# we've calculated them previously and we use them to define the net.

# In[ ]:


nd       = NodeData()
skel     = GraphSkeleton()
jsonpath_skel ="titanic_skel.json"
jsonpath_node ="titanic_nodes.json"
nd.load(jsonpath_node)
skel.load(jsonpath_skel)

# load bayesian network
bn       = DiscreteBayesianNetwork(skel, nd)

print (skel.getchildren("Class"),skel.getchildren("Sex"),skel.getchildren("Fare"),skel.getchildren("Surv"))
([u'Surv'], [u'Surv'], [u'Class'], [])
# In[ ]:


# We can now start querying our network. We provide a query (first dictionary in the arguments)
# and an evidence (second dictionary in the args))

tablecpd=TableCPDFactorization(bn)
print ("P(Surv=0) = {}".format(tablecpd.specificquery(dict(Surv='0'),dict())))


# In[ ]:


tablecpd=TableCPDFactorization(bn)