本文整理汇总了Python中pgmpy.models.MarkovModel.is_triangulated方法的典型用法代码示例。如果您正苦于以下问题:Python MarkovModel.is_triangulated方法的具体用法?Python MarkovModel.is_triangulated怎么用?Python MarkovModel.is_triangulated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.MarkovModel
的用法示例。
在下文中一共展示了MarkovModel.is_triangulated方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUndirectedGraphTriangulation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import is_triangulated [as 别名]
class TestUndirectedGraphTriangulation(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_check_clique(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.check_clique(['a', 'b', 'c']))
def test_is_triangulated(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.is_triangulated())
def test_triangulation_h1_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = Factor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = Factor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = Factor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H1', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h2_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = Factor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = Factor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = Factor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H2', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h3_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = Factor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = Factor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = Factor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H3', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h4_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = Factor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = Factor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = Factor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h5_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = Factor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = Factor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = Factor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h6_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = Factor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = Factor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = Factor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_cardinality_mismatch_raises_error(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
factor_list = [Factor(edge, [2, 2], np.random.rand(4)) for edge in
#.........这里部分代码省略.........
示例2: TestUndirectedGraphTriangulation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import is_triangulated [as 别名]
class TestUndirectedGraphTriangulation(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_check_clique(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.is_clique(['a', 'b', 'c']))
def test_is_triangulated(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.is_triangulated())
def test_triangulation_h1_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H1', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h2_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H2', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h3_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H3', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h4_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h5_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h6_inplace(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = DiscreteFactor(['a', 'b'], [2, 3], np.random.rand(6))
phi2 = DiscreteFactor(['b', 'c'], [3, 4], np.random.rand(12))
phi3 = DiscreteFactor(['c', 'd'], [4, 5], np.random.rand(20))
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_cardinality_mismatch_raises_error(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
factor_list = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in
#.........这里部分代码省略.........