本文整理汇总了Python中pgmpy.models.BayesianModel.remove_cpds方法的典型用法代码示例。如果您正苦于以下问题:Python BayesianModel.remove_cpds方法的具体用法?Python BayesianModel.remove_cpds怎么用?Python BayesianModel.remove_cpds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.BayesianModel
的用法示例。
在下文中一共展示了BayesianModel.remove_cpds方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestBayesianModelCPD
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import remove_cpds [as 别名]
#.........这里部分代码省略.........
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]]),
['i'], 2)
self.G.add_cpds(cpd_g)
self.assertRaises(ValueError, self.G.check_model)
self.G.remove_cpds(cpd_g)
cpd_g = TabularCPD('g', 2,
np.array([[0.2, 0.3, 0.4, 0.6],
[0.8, 0.7, 0.6, 0.4]]),
['d', 's'], [2, 2])
self.G.add_cpds(cpd_g)
self.assertRaises(ValueError, self.G.check_model)
self.G.remove_cpds(cpd_g)
cpd_g = TabularCPD('g', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
['l'], 2)
self.G.add_cpds(cpd_g)
self.assertRaises(ValueError, self.G.check_model)
self.G.remove_cpds(cpd_g)
cpd_l = TabularCPD('l', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
['d'], 2)
self.G.add_cpds(cpd_l)
self.assertRaises(ValueError, self.G.check_model)
self.G.remove_cpds(cpd_l)
cpd_l = TabularCPD('l', 2,
np.array([[0.2, 0.3, 0.4, 0.6],
[0.8, 0.7, 0.6, 0.4]]),
['d', 'i'], [2, 2])
self.G.add_cpds(cpd_l)
self.assertRaises(ValueError, self.G.check_model)
self.G.remove_cpds(cpd_l)
示例2: TestDirectedGraphCPDOperations
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import remove_cpds [as 别名]
class TestDirectedGraphCPDOperations(unittest.TestCase):
def setUp(self):
self.graph = BayesianModel()
def test_add_single_cpd(self):
cpd = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd)
self.assertListEqual(self.graph.get_cpds(), [cpd])
def test_add_multiple_cpds(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.assertListEqual(self.graph.get_cpds(), [cpd1, cpd2, cpd3])
def test_remove_single_cpd(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.graph.remove_cpds(cpd1)
self.assertListEqual(self.graph.get_cpds(), [cpd2, cpd3])
def test_remove_multiple_cpds(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.graph.remove_cpds(cpd1, cpd3)
self.assertListEqual(self.graph.get_cpds(), [cpd2])
def test_remove_single_cpd_string(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.graph.remove_cpds('diff')
self.assertListEqual(self.graph.get_cpds(), [cpd2, cpd3])
def test_remove_multiple_cpds_string(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.graph.remove_cpds('diff', 'grade')
self.assertListEqual(self.graph.get_cpds(), [cpd2])
def test_get_cpd_for_node(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.assertEqual(self.graph.get_cpds('diff'), cpd1)
self.assertEqual(self.graph.get_cpds('intel'), cpd2)
self.assertEqual(self.graph.get_cpds('grade'), cpd3)
def test_get_cpd_raises_error(self):
cpd1 = TabularCPD('diff', 2, np.random.rand(2, 1))
cpd2 = TabularCPD('intel', 2, np.random.rand(2, 1))
cpd3 = TabularCPD('grade', 2, np.random.rand(2, 4),
['diff', 'intel'], [2, 2])
self.graph.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
self.graph.add_cpds(cpd1, cpd2, cpd3)
self.assertRaises(ValueError, self.graph.get_cpds, 'sat')
def tearDown(self):
del self.graph
示例3: TestBayesianModelMethods
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import remove_cpds [as 别名]
class TestBayesianModelMethods(unittest.TestCase):
def setUp(self):
self.G = BayesianModel([('a', 'd'), ('b', 'd'),
('d', 'e'), ('b', 'c')])
self.G1 = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
diff_cpd = TabularCPD('diff', 2, values=[[0.2], [0.8]])
intel_cpd = TabularCPD('intel', 3, values=[[0.5], [0.3], [0.2]])
grade_cpd = TabularCPD('grade', 3, values=[[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
[0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
evidence=['diff', 'intel'], evidence_card=[2, 3])
self.G1.add_cpds(diff_cpd, intel_cpd, grade_cpd)
self.G2 = BayesianModel([('d', 'g'), ('g', 'l'), ('i', 'g'), ('i', 'l')])
def test_moral_graph(self):
moral_graph = self.G.moralize()
self.assertListEqual(sorted(moral_graph.nodes()), ['a', 'b', 'c', 'd', 'e'])
for edge in moral_graph.edges():
self.assertTrue(edge in [('a', 'b'), ('a', 'd'), ('b', 'c'), ('d', 'b'), ('e', 'd')] or
(edge[1], edge[0]) in [('a', 'b'), ('a', 'd'), ('b', 'c'), ('d', 'b'), ('e', 'd')])
def test_moral_graph_with_edge_present_over_parents(self):
G = BayesianModel([('a', 'd'), ('d', 'e'), ('b', 'd'), ('b', 'c'), ('a', 'b')])
moral_graph = G.moralize()
self.assertListEqual(sorted(moral_graph.nodes()), ['a', 'b', 'c', 'd', 'e'])
for edge in moral_graph.edges():
self.assertTrue(edge in [('a', 'b'), ('c', 'b'), ('d', 'a'), ('d', 'b'), ('d', 'e')] or
(edge[1], edge[0]) in [('a', 'b'), ('c', 'b'), ('d', 'a'), ('d', 'b'), ('d', 'e')])
def test_get_ancestors_of_success(self):
ancenstors1 = self.G2._get_ancestors_of('g')
ancenstors2 = self.G2._get_ancestors_of('d')
ancenstors3 = self.G2._get_ancestors_of(['i', 'l'])
self.assertEqual(ancenstors1, {'d', 'i', 'g'})
self.assertEqual(ancenstors2, {'d'})
self.assertEqual(ancenstors3, {'g', 'i', 'l', 'd'})
def test_get_ancestors_of_failure(self):
self.assertRaises(ValueError, self.G2._get_ancestors_of, 'h')
def test_local_independencies(self):
self.assertEqual(self.G.local_independencies('a'), Independencies(['a', ['b', 'c']]))
self.assertEqual(self.G.local_independencies('c'), Independencies(['c', ['a', 'd', 'e'], 'b']))
self.assertEqual(self.G.local_independencies('d'), Independencies(['d', 'c', ['b', 'a']]))
self.assertEqual(self.G.local_independencies('e'), Independencies(['e', ['c', 'b', 'a'], 'd']))
self.assertEqual(self.G.local_independencies('b'), Independencies(['b', 'a']))
self.assertEqual(self.G1.local_independencies('grade'), Independencies())
def test_get_independencies(self):
chain = BayesianModel([('X', 'Y'), ('Y', 'Z')])
self.assertEqual(chain.get_independencies(), Independencies(('X', 'Z', 'Y'), ('Z', 'X', 'Y')))
fork = BayesianModel([('Y', 'X'), ('Y', 'Z')])
self.assertEqual(fork.get_independencies(), Independencies(('X', 'Z', 'Y'), ('Z', 'X', 'Y')))
collider = BayesianModel([('X', 'Y'), ('Z', 'Y')])
self.assertEqual(collider.get_independencies(), Independencies(('X', 'Z'), ('Z', 'X')))
def test_is_imap(self):
val = [0.01, 0.01, 0.08, 0.006, 0.006, 0.048, 0.004, 0.004, 0.032,
0.04, 0.04, 0.32, 0.024, 0.024, 0.192, 0.016, 0.016, 0.128]
JPD = JointProbabilityDistribution(['diff', 'intel', 'grade'], [2, 3, 3], val)
fac = DiscreteFactor(['diff', 'intel', 'grade'], [2, 3, 3], val)
self.assertTrue(self.G1.is_imap(JPD))
self.assertRaises(TypeError, self.G1.is_imap, fac)
def test_get_immoralities(self):
G = BayesianModel([('x', 'y'), ('z', 'y'), ('x', 'z'), ('w', 'y')])
self.assertEqual(G.get_immoralities(), {('w', 'x'), ('w', 'z')})
G1 = BayesianModel([('x', 'y'), ('z', 'y'), ('z', 'x'), ('w', 'y')])
self.assertEqual(G1.get_immoralities(), {('w', 'x'), ('w', 'z')})
G2 = BayesianModel([('x', 'y'), ('z', 'y'), ('x', 'z'), ('w', 'y'), ('w', 'x')])
self.assertEqual(G2.get_immoralities(), {('w', 'z')})
def test_is_iequivalent(self):
G = BayesianModel([('x', 'y'), ('z', 'y'), ('x', 'z'), ('w', 'y')])
self.assertRaises(TypeError, G.is_iequivalent, MarkovModel())
G1 = BayesianModel([('V', 'W'), ('W', 'X'), ('X', 'Y'), ('Z', 'Y')])
G2 = BayesianModel([('W', 'V'), ('X', 'W'), ('X', 'Y'), ('Z', 'Y')])
self.assertTrue(G1.is_iequivalent(G2))
G3 = BayesianModel([('W', 'V'), ('W', 'X'), ('Y', 'X'), ('Z', 'Y')])
self.assertFalse(G3.is_iequivalent(G2))
def test_copy(self):
model_copy = self.G1.copy()
self.assertEqual(sorted(self.G1.nodes()), sorted(model_copy.nodes()))
self.assertEqual(sorted(self.G1.edges()), sorted(model_copy.edges()))
self.assertNotEqual(id(self.G1.get_cpds('diff')),
id(model_copy.get_cpds('diff')))
self.G1.remove_cpds('diff')
diff_cpd = TabularCPD('diff', 2, values=[[0.3], [0.7]])
self.G1.add_cpds(diff_cpd)
self.assertNotEqual(self.G1.get_cpds('diff'),
model_copy.get_cpds('diff'))
self.G1.remove_node('intel')
self.assertNotEqual(sorted(self.G1.nodes()), sorted(model_copy.nodes()))
self.assertNotEqual(sorted(self.G1.edges()), sorted(model_copy.edges()))
def test_remove_node(self):
#.........这里部分代码省略.........