本文整理汇总了Python中pgmpy.models.BayesianModel.copy方法的典型用法代码示例。如果您正苦于以下问题:Python BayesianModel.copy方法的具体用法?Python BayesianModel.copy怎么用?Python BayesianModel.copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.BayesianModel
的用法示例。
在下文中一共展示了BayesianModel.copy方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestBayesianModelMethods
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import copy [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):
#.........这里部分代码省略.........
示例2: TestBaseEstimator
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import copy [as 别名]
class TestBaseEstimator(unittest.TestCase):
def setUp(self):
self.rand_data = pd.DataFrame(np.random.randint(0, 5, size=(5000, 2)), columns=list('AB'))
self.rand_data['C'] = self.rand_data['B']
self.est_rand = HillClimbSearch(self.rand_data, scoring_method=K2Score(self.rand_data))
self.model1 = BayesianModel()
self.model1.add_nodes_from(['A', 'B', 'C'])
self.model2 = self.model1.copy()
self.model2.add_edge('A', 'B')
# link to dataset: "https://www.kaggle.com/c/titanic/download/train.csv"
self.titanic_data = pd.read_csv('pgmpy/tests/test_estimators/testdata/titanic_train.csv')
self.titanic_data1 = self.titanic_data[["Survived", "Sex", "Pclass", "Age", "Embarked"]]
self.titanic_data2 = self.titanic_data[["Survived", "Sex", "Pclass"]]
self.est_titanic1 = HillClimbSearch(self.titanic_data1)
self.est_titanic2 = HillClimbSearch(self.titanic_data2)
def test_legal_operations(self):
model2_legal_ops = list(self.est_rand._legal_operations(self.model2))
model2_legal_ops_ref = [(('+', ('C', 'A')), -28.15602208305154),
(('+', ('A', 'C')), -28.155467430966382),
(('+', ('C', 'B')), 7636.947544933631),
(('+', ('B', 'C')), 7937.805375579936),
(('-', ('A', 'B')), 28.155467430966382),
(('flip', ('A', 'B')), -0.0005546520851567038)]
self.assertSetEqual(set([op for op, score in model2_legal_ops]),
set([op for op, score in model2_legal_ops_ref]))
def test_legal_operations_titanic(self):
est = self.est_titanic1
start_model = BayesianModel([("Survived", "Sex"),
("Pclass", "Age"),
("Pclass", "Embarked")])
legal_ops = est._legal_operations(start_model)
self.assertEqual(len(list(legal_ops)), 20)
tabu_list = [('-', ("Survived", "Sex")),
('-', ("Survived", "Pclass")),
('flip', ("Age", "Pclass"))]
legal_ops_tabu = est._legal_operations(start_model, tabu_list=tabu_list)
self.assertEqual(len(list(legal_ops_tabu)), 18)
legal_ops_indegree = est._legal_operations(start_model, max_indegree=1)
self.assertEqual(len(list(legal_ops_indegree)), 11)
legal_ops_both = est._legal_operations(start_model, tabu_list=tabu_list, max_indegree=1)
legal_ops_both_ref = [(('+', ('Embarked', 'Survived')), 10.050632580087608),
(('+', ('Survived', 'Pclass')), 41.88868046549101),
(('+', ('Age', 'Survived')), -23.635716036430836),
(('+', ('Pclass', 'Survived')), 41.81314459373226),
(('+', ('Sex', 'Pclass')), 4.772261678792802),
(('-', ('Pclass', 'Age')), 11.546515590731815),
(('-', ('Pclass', 'Embarked')), -32.171482832532774),
(('flip', ('Pclass', 'Embarked')), 3.3563814191281836),
(('flip', ('Survived', 'Sex')), 0.039737027979640516)]
self.assertSetEqual(set(legal_ops_both), set(legal_ops_both_ref))
def test_estimate_rand(self):
est1 = self.est_rand.estimate()
self.assertSetEqual(set(est1.nodes()), set(['A', 'B', 'C']))
self.assertTrue(est1.edges() == [('B', 'C')] or est1.edges() == [('C', 'B')])
est2 = self.est_rand.estimate(start=BayesianModel([('A', 'B'), ('A', 'C')]))
self.assertTrue(est2.edges() == [('B', 'C')] or est2.edges() == [('C', 'B')])
def test_estimate_titanic(self):
self.assertSetEqual(set(self.est_titanic2.estimate().edges()),
set([('Survived', 'Pclass'), ('Sex', 'Pclass'), ('Sex', 'Survived')]))
def tearDown(self):
del self.rand_data
del self.est_rand
del self.model1
del self.titanic_data
del self.titanic_data1
del self.titanic_data2
del self.est_titanic1
del self.est_titanic2