本文整理汇总了Python中pgmpy.models.BayesianModel.is_active_trail方法的典型用法代码示例。如果您正苦于以下问题:Python BayesianModel.is_active_trail方法的具体用法?Python BayesianModel.is_active_trail怎么用?Python BayesianModel.is_active_trail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.BayesianModel
的用法示例。
在下文中一共展示了BayesianModel.is_active_trail方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestBayesianModelCPD
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import is_active_trail [as 别名]
class TestBayesianModelCPD(unittest.TestCase):
def setUp(self):
self.G = BayesianModel([('d', 'g'), ('i', 'g'), ('g', 'l'),
('i', 's')])
def test_active_trail_nodes(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d')), ['d', 'g', 'l'])
self.assertEqual(sorted(self.G.active_trail_nodes('i')), ['g', 'i', 'l', 's'])
def test_active_trail_nodes_args(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d', observed='g')), ['d', 'i', 's'])
self.assertEqual(sorted(self.G.active_trail_nodes('l', observed='g')), ['l'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['i', 'l'])), ['s'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['d', 'l'])), ['g', 'i', 's'])
def test_is_active_trail_triplets(self):
self.assertTrue(self.G.is_active_trail('d', 'l'))
self.assertTrue(self.G.is_active_trail('g', 's'))
self.assertFalse(self.G.is_active_trail('d', 'i'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='g'))
self.assertFalse(self.G.is_active_trail('d', 'l', observed='g'))
self.assertFalse(self.G.is_active_trail('i', 'l', observed='g'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='l'))
self.assertFalse(self.G.is_active_trail('g', 's', observed='i'))
def test_is_active_trail(self):
self.assertFalse(self.G.is_active_trail('d', 's'))
self.assertTrue(self.G.is_active_trail('s', 'l'))
self.assertTrue(self.G.is_active_trail('d', 's', observed='g'))
self.assertFalse(self.G.is_active_trail('s', 'l', observed='g'))
def test_is_active_trail_args(self):
self.assertFalse(self.G.is_active_trail('s', 'l', 'i'))
self.assertFalse(self.G.is_active_trail('s', 'l', 'g'))
self.assertTrue(self.G.is_active_trail('d', 's', 'l'))
self.assertFalse(self.G.is_active_trail('d', 's', ['i', 'l']))
def test_get_cpds(self):
cpd_d = TabularCPD('d', 2, np.random.rand(2, 1))
cpd_i = TabularCPD('i', 2, np.random.rand(2, 1))
cpd_g = TabularCPD('g', 2, np.random.rand(2, 4), ['d', 'i'], [2, 2])
cpd_l = TabularCPD('l', 2, np.random.rand(2, 2), ['g'], 2)
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)
self.assertEqual(self.G.get_cpds('d').variable, 'd')
def test_get_cpds1(self):
self.model = BayesianModel([('A', 'AB')])
cpd_a = TabularCPD('A', 2, np.random.rand(2, 1))
cpd_ab = TabularCPD('AB', 2, np.random.rand(2, 2), evidence=['A'],
evidence_card=[2])
self.model.add_cpds(cpd_a, cpd_ab)
self.assertEqual(self.model.get_cpds('A').variable, 'A')
self.assertEqual(self.model.get_cpds('AB').variable, 'AB')
def test_add_single_cpd(self):
from pgmpy.factors import TabularCPD
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_s)
self.assertListEqual(self.G.get_cpds(), [cpd_s])
def test_add_multiple_cpds(self):
from pgmpy.factors import TabularCPD
cpd_d = TabularCPD('d', 2, np.random.rand(2, 1))
cpd_i = TabularCPD('i', 2, np.random.rand(2, 1))
cpd_g = TabularCPD('g', 2, np.random.rand(2, 4), ['d', 'i'], [2, 2])
cpd_l = TabularCPD('l', 2, np.random.rand(2, 2), ['g'], 2)
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)
self.assertEqual(self.G.get_cpds('d'), cpd_d)
self.assertEqual(self.G.get_cpds('i'), cpd_i)
self.assertEqual(self.G.get_cpds('g'), cpd_g)
self.assertEqual(self.G.get_cpds('l'), cpd_l)
self.assertEqual(self.G.get_cpds('s'), cpd_s)
def tearDown(self):
del self.G
示例2: TestBayesianModelCPD
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import is_active_trail [as 别名]
class TestBayesianModelCPD(unittest.TestCase):
def setUp(self):
self.G = BayesianModel([('d', 'g'), ('i', 'g'), ('g', 'l'),
('i', 's')])
def test_active_trail_nodes(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d')), ['d', 'g', 'l'])
self.assertEqual(sorted(self.G.active_trail_nodes('i')), ['g', 'i', 'l', 's'])
def test_active_trail_nodes_args(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d', observed='g')), ['d', 'i', 's'])
self.assertEqual(sorted(self.G.active_trail_nodes('l', observed='g')), ['l'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['i', 'l'])), ['s'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['d', 'l'])), ['g', 'i', 's'])
def test_is_active_trail_triplets(self):
self.assertTrue(self.G.is_active_trail('d', 'l'))
self.assertTrue(self.G.is_active_trail('g', 's'))
self.assertFalse(self.G.is_active_trail('d', 'i'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='g'))
self.assertFalse(self.G.is_active_trail('d', 'l', observed='g'))
self.assertFalse(self.G.is_active_trail('i', 'l', observed='g'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='l'))
self.assertFalse(self.G.is_active_trail('g', 's', observed='i'))
def test_is_active_trail(self):
self.assertFalse(self.G.is_active_trail('d', 's'))
self.assertTrue(self.G.is_active_trail('s', 'l'))
self.assertTrue(self.G.is_active_trail('d', 's', observed='g'))
self.assertFalse(self.G.is_active_trail('s', 'l', observed='g'))
def test_is_active_trail_args(self):
self.assertFalse(self.G.is_active_trail('s', 'l', 'i'))
self.assertFalse(self.G.is_active_trail('s', 'l', 'g'))
self.assertTrue(self.G.is_active_trail('d', 's', 'l'))
self.assertFalse(self.G.is_active_trail('d', 's', ['i', 'l']))
def test_get_cpds(self):
cpd_d = TabularCPD('d', 2, np.random.rand(2, 1))
cpd_i = TabularCPD('i', 2, np.random.rand(2, 1))
cpd_g = TabularCPD('g', 2, np.random.rand(2, 4), ['d', 'i'], [2, 2])
cpd_l = TabularCPD('l', 2, np.random.rand(2, 2), ['g'], 2)
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)
self.assertEqual(self.G.get_cpds('d').variable, 'd')
def test_get_cpds1(self):
self.model = BayesianModel([('A', 'AB')])
cpd_a = TabularCPD('A', 2, np.random.rand(2, 1))
cpd_ab = TabularCPD('AB', 2, np.random.rand(2, 2), evidence=['A'],
evidence_card=[2])
self.model.add_cpds(cpd_a, cpd_ab)
self.assertEqual(self.model.get_cpds('A').variable, 'A')
self.assertEqual(self.model.get_cpds('AB').variable, 'AB')
def test_add_single_cpd(self):
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_s)
self.assertListEqual(self.G.get_cpds(), [cpd_s])
def test_add_multiple_cpds(self):
cpd_d = TabularCPD('d', 2, np.random.rand(2, 1))
cpd_i = TabularCPD('i', 2, np.random.rand(2, 1))
cpd_g = TabularCPD('g', 2, np.random.rand(2, 4), ['d', 'i'], [2, 2])
cpd_l = TabularCPD('l', 2, np.random.rand(2, 2), ['g'], 2)
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)
self.assertEqual(self.G.get_cpds('d'), cpd_d)
self.assertEqual(self.G.get_cpds('i'), cpd_i)
self.assertEqual(self.G.get_cpds('g'), cpd_g)
self.assertEqual(self.G.get_cpds('l'), cpd_l)
self.assertEqual(self.G.get_cpds('s'), cpd_s)
def test_check_model(self):
cpd_g = TabularCPD('g', 2,
np.array([[0.2, 0.3, 0.4, 0.6],
[0.8, 0.7, 0.6, 0.4]]),
['d', 'i'], [2, 2])
cpd_s = TabularCPD('s', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
['i'], 2)
cpd_l = TabularCPD('l', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
['g'], 2)
self.G.add_cpds(cpd_g, cpd_s, cpd_l)
self.assertTrue(self.G.check_model())
def test_check_model1(self):
cpd_g = TabularCPD('g', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
#.........这里部分代码省略.........
示例3: TestBayesianModelCPD
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import is_active_trail [as 别名]
#.........这里部分代码省略.........
# self.assertRaises(ValueError, self.G.set_observations, {'d': 'unknow_state'})
#
# def test_reset_observations_single_state(self):
# self.G.reset_observations({'d': 'easy'})
# # TODO change this as the function has changed
# self.G.reset_observations({'d': 'easy'})
# for state in self.G.node['d']['_states']:
# if state['name'] == 'easy':
# break
# self.assertFalse(state['observed_status'])
# self.assertFalse(self.G.node['g']['_observed'])
#
# def test_reset_observations_multiple_state(self):
# self.G.set_observations({'d': 'easy', 'g': 'A', 'i': 'dumb'})
# self.G.reset_observations({'d': 'easy', 'i': 'dumb'})
# for state in self.G.node['d']['_states']:
# if state['name'] == 'easy':
# break
# self.assertFalse(state['observed_status'])
# self.assertFalse(self.G.node['d']['_observed'])
# for state in self.G.node['g']['_states']:
# if state['name'] == 'A':
# break
# self.assertTrue(state['observed_status'])
# self.assertTrue(self.G.node['g']['_observed'])
#
# def test_reset_observation_node_none(self):
# self.G.set_observations({'d': 'easy', 'g': 'A'})
# self.G.reset_observations()
# self.assertFalse(self.G.node['d']['_observed'])
# for state in self.G.node['d']['_states']:
# self.assertFalse(state['observed_status'])
# self.assertFalse(self.G.node['g']['_observed'])
# for state in self.G.node['g']['_states']:
# self.assertFalse(state['observed_status'])
#
# def test_reset_observations_node_not_none(self):
# self.G.set_observations({'d': 'easy', 'g': 'A'})
# self.G.reset_observations('d')
# self.assertFalse(self.G.node['d']['_observed'])
# for state in self.G.node['d']['_states']:
# self.assertFalse(state['observed_status'])
# self.assertTrue(self.G.node['g']['_observed'])
# for state in self.G.node['g']['_states']:
# if state['name'] == 'A':
# self.assertTrue(state['observed_status'])
# else:
# self.assertFalse(state['observed_status'])
#
# def test_reset_observations_node_error(self):
# self.assertRaises(KeyError, self.G.reset_observations, 'j')
#
# def test_is_observed(self):
# self.G.set_observations({'d': 'easy'})
# self.assertTrue(self.G.is_observed('d'))
# self.assertFalse(self.G.is_observed('i'))
#
# # def test_get_ancestros_observation(self):
# # self.G.set_observations({'d': 'easy', 'g': 'A'})
# # self.assertListEqual(list(self.G._get_ancestors_observation(['d'])), [])
# # self.assertListEqual(list(sorted(self.G._get_ancestors_observation(['d', 'g']))), ['d', 'i'])
#
# def test_get_observed_list(self):
# self.G.set_observations({'d': 'hard', 'i': 'smart'})
# self.assertListEqual(sorted(self.G._get_observed_list()), ['d', 'i'])
def test_active_trail_nodes(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d')), ['d', 'g', 'l'])
self.assertEqual(sorted(self.G.active_trail_nodes('i')), ['g', 'i', 'l', 's'])
def test_active_trail_nodes_args(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d', observed='g')), ['d', 'i', 's'])
self.assertEqual(sorted(self.G.active_trail_nodes('l', observed='g')), ['l'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['i', 'l'])), ['s'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['d', 'l'])), ['g', 'i', 's'])
def test_is_active_trail_triplets(self):
self.assertTrue(self.G.is_active_trail('d', 'l'))
self.assertTrue(self.G.is_active_trail('g', 's'))
self.assertFalse(self.G.is_active_trail('d', 'i'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='g'))
self.assertFalse(self.G.is_active_trail('d', 'l', observed='g'))
self.assertFalse(self.G.is_active_trail('i', 'l', observed='g'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='l'))
self.assertFalse(self.G.is_active_trail('g', 's', observed='i'))
def test_is_active_trail(self):
self.assertFalse(self.G.is_active_trail('d', 's'))
self.assertTrue(self.G.is_active_trail('s', 'l'))
self.assertTrue(self.G.is_active_trail('d', 's', observed='g'))
self.assertFalse(self.G.is_active_trail('s', 'l', observed='g'))
def test_is_active_trail_args(self):
self.assertFalse(self.G.is_active_trail('s', 'l', 'i'))
self.assertFalse(self.G.is_active_trail('s', 'l', 'g'))
self.assertTrue(self.G.is_active_trail('d', 's', 'l'))
self.assertFalse(self.G.is_active_trail('d', 's', ['i', 'l']))
def tearDown(self):
del self.G