本文整理汇总了Python中pgmpy.models.MarkovModel.remove_factors方法的典型用法代码示例。如果您正苦于以下问题:Python MarkovModel.remove_factors方法的具体用法?Python MarkovModel.remove_factors怎么用?Python MarkovModel.remove_factors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.MarkovModel
的用法示例。
在下文中一共展示了MarkovModel.remove_factors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUndirectedGraphFactorOperations
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import remove_factors [as 别名]
class TestUndirectedGraphFactorOperations(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_add_factor_raises_error(self):
self.graph.add_edges_from([('Alice', 'Bob'), ('Bob', 'Charles'),
('Charles', 'Debbie'), ('Debbie', 'Alice')])
factor = Factor(['Alice', 'Bob', 'John'], [2, 2, 2], np.random.rand(8))
self.assertRaises(ValueError, self.graph.add_factors, factor)
def test_add_single_factor(self):
self.graph.add_nodes_from(['a', 'b', 'c'])
phi = Factor(['a', 'b'], [2, 2], range(4))
self.graph.add_factors(phi)
self.assertListEqual(self.graph.get_factors(), [phi])
def test_add_multiple_factors(self):
self.graph.add_nodes_from(['a', 'b', 'c'])
phi1 = Factor(['a', 'b'], [2, 2], range(4))
phi2 = Factor(['b', 'c'], [2, 2], range(4))
self.graph.add_factors(phi1, phi2)
self.assertListEqual(self.graph.get_factors(), [phi1, phi2])
def test_remove_single_factor(self):
self.graph.add_nodes_from(['a', 'b', 'c'])
phi1 = Factor(['a', 'b'], [2, 2], range(4))
phi2 = Factor(['b', 'c'], [2, 2], range(4))
self.graph.add_factors(phi1, phi2)
self.graph.remove_factors(phi1)
self.assertListEqual(self.graph.get_factors(), [phi2])
def test_remove_multiple_factors(self):
self.graph.add_nodes_from(['a', 'b', 'c'])
phi1 = Factor(['a', 'b'], [2, 2], range(4))
phi2 = Factor(['b', 'c'], [2, 2], range(4))
self.graph.add_factors(phi1, phi2)
self.graph.remove_factors(phi1, phi2)
self.assertListEqual(self.graph.get_factors(), [])
def test_partition_function(self):
self.graph.add_nodes_from(['a', 'b', 'c'])
phi1 = Factor(['a', 'b'], [2, 2], range(4))
phi2 = Factor(['b', 'c'], [2, 2], range(4))
self.graph.add_factors(phi1, phi2)
self.graph.add_edges_from([('a', 'b'), ('b', 'c')])
self.assertEqual(self.graph.get_partition_function(), 22.0)
def test_partition_function_raises_error(self):
self.graph.add_nodes_from(['a', 'b', 'c', 'd'])
phi1 = Factor(['a', 'b'], [2, 2], range(4))
phi2 = Factor(['b', 'c'], [2, 2], range(4))
self.graph.add_factors(phi1, phi2)
self.assertRaises(ValueError,
self.graph.get_partition_function)
def tearDown(self):
del self.graph
示例2: TestMarkovModelMethods
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import remove_factors [as 别名]
class TestMarkovModelMethods(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_get_cardinality(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
self.assertDictEqual(self.graph.get_cardinality(), {})
phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
self.graph.add_factors(phi1)
self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2})
self.graph.remove_factors(phi1)
self.assertDictEqual(self.graph.get_cardinality(), {})
phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
phi2 = Factor(['c', 'd'], [1, 2], np.random.rand(2))
self.graph.add_factors(phi1, phi2)
self.assertDictEqual(self.graph.get_cardinality(), {'d': 2, 'a': 2, 'b': 2, 'c': 1})
phi3 = Factor(['d', 'a'], [1, 2], np.random.rand(2))
self.graph.add_factors(phi3)
self.assertDictEqual(self.graph.get_cardinality(), {'d': 1, 'c': 1, 'b': 2, 'a': 2})
self.graph.remove_factors(phi1, phi2, phi3)
self.assertDictEqual(self.graph.get_cardinality(), {})
def test_get_cardinality_check_cardinality(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
self.graph.add_factors(phi1)
self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True)
phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
self.graph.add_factors(phi2)
self.assertRaises(ValueError, self.graph.get_cardinality, check_cardinality=True)
phi3 = Factor(['c', 'd'], [2, 2], np.random.rand(4))
self.graph.add_factors(phi3)
self.assertDictEqual(self.graph.get_cardinality(check_cardinality=True), {'d': 2, 'c': 2, 'b': 2, 'a': 1})
def test_check_model(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
phi2 = Factor(['c', 'b'], [3, 2], np.random.rand(6))
phi3 = Factor(['c', 'd'], [3, 4], np.random.rand(12))
phi4 = Factor(['d', 'a'], [4, 1], np.random.rand(4))
self.graph.add_factors(phi1, phi2, phi3, phi4)
self.assertTrue(self.graph.check_model())
self.graph.remove_factors(phi1, phi4)
phi1 = Factor(['a', 'b'], [4, 2], np.random.rand(8))
self.graph.add_factors(phi1)
self.assertTrue(self.graph.check_model())
def test_check_model1(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
phi2 = Factor(['b', 'c'], [3, 3], np.random.rand(9))
self.graph.add_factors(phi1, phi2)
self.assertRaises(ValueError, self.graph.check_model)
self.graph.remove_factors(phi2)
phi3 = Factor(['c', 'a'], [4, 4], np.random.rand(16))
self.graph.add_factors(phi3)
self.assertRaises(ValueError, self.graph.check_model)
self.graph.remove_factors(phi3)
phi2 = Factor(['b', 'c'], [2, 3], np.random.rand(6))
phi3 = Factor(['c', 'd'], [3, 4], np.random.rand(12))
phi4 = Factor(['d', 'a'], [4, 3], np.random.rand(12))
self.graph.add_factors(phi2, phi3, phi4)
self.assertRaises(ValueError, self.graph.check_model)
self.graph.remove_factors(phi2, phi3, phi4)
phi2 = Factor(['a', 'b'], [1, 3], np.random.rand(3))
self.graph.add_factors(phi1, phi2)
self.assertRaises(ValueError, self.graph.check_model)
self.graph.remove_factors(phi2)
def test_check_model2(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
phi1 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
#.........这里部分代码省略.........