本文整理汇总了Python中pgmpy.models.MarkovModel.get_local_independencies方法的典型用法代码示例。如果您正苦于以下问题:Python MarkovModel.get_local_independencies方法的具体用法?Python MarkovModel.get_local_independencies怎么用?Python MarkovModel.get_local_independencies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.MarkovModel
的用法示例。
在下文中一共展示了MarkovModel.get_local_independencies方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestMarkovModelMethods
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import get_local_independencies [as 别名]
#.........这里部分代码省略.........
phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
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, 1], np.random.rand(4))
phi5 = Factor(['d', 'b'], [4, 2], np.random.rand(8))
self.graph.add_factors(phi1, phi2, phi3, phi4, phi5)
self.assertRaises(ValueError, self.graph.check_model)
self.graph.remove_factors(phi1, phi2, phi3, phi4, phi5)
def test_factor_graph(self):
from pgmpy.models import FactorGraph
phi1 = Factor(['Alice', 'Bob'], [3, 2], np.random.rand(6))
phi2 = Factor(['Bob', 'Charles'], [2, 2], np.random.rand(4))
self.graph.add_edges_from([('Alice', 'Bob'), ('Bob', 'Charles')])
self.graph.add_factors(phi1, phi2)
factor_graph = self.graph.to_factor_graph()
self.assertIsInstance(factor_graph, FactorGraph)
self.assertListEqual(sorted(factor_graph.nodes()),
['Alice', 'Bob', 'Charles', 'phi_Alice_Bob',
'phi_Bob_Charles'])
self.assertListEqual(hf.recursive_sorted(factor_graph.edges()),
[['Alice', 'phi_Alice_Bob'], ['Bob', 'phi_Alice_Bob'],
['Bob', 'phi_Bob_Charles'], ['Charles', 'phi_Bob_Charles']])
self.assertListEqual(factor_graph.get_factors(), [phi1, phi2])
def test_factor_graph_raises_error(self):
self.graph.add_edges_from([('Alice', 'Bob'), ('Bob', 'Charles')])
self.assertRaises(ValueError, self.graph.to_factor_graph)
def test_junction_tree(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)
junction_tree = self.graph.to_junction_tree()
self.assertListEqual(hf.recursive_sorted(junction_tree.nodes()),
[['a', 'b', 'd'], ['b', 'c', 'd']])
self.assertEqual(len(junction_tree.edges()), 1)
def test_junction_tree_single_clique(self):
from pgmpy.factors import factor_product
self.graph.add_edges_from([('x1','x2'), ('x2', 'x3'), ('x1', 'x3')])
phi = [Factor(edge, [2, 2], np.random.rand(4)) for edge in self.graph.edges()]
self.graph.add_factors(*phi)
junction_tree = self.graph.to_junction_tree()
self.assertListEqual(hf.recursive_sorted(junction_tree.nodes()),
[['x1', 'x2', 'x3']])
factors = junction_tree.get_factors()
self.assertEqual(factors[0], factor_product(*phi))
def test_markov_blanket(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c')])
self.assertListEqual(self.graph.markov_blanket('a'), ['b'])
self.assertListEqual(sorted(self.graph.markov_blanket('b')),
['a', 'c'])
def test_local_independencies(self):
from pgmpy.independencies import Independencies
self.graph.add_edges_from([('a', 'b'), ('b', 'c')])
independencies = self.graph.get_local_independencies()
self.assertIsInstance(independencies, Independencies)
self.assertEqual(len(independencies.get_assertions()), 2)
string = ''
for assertion in sorted(independencies.get_assertions(),
key=lambda x: list(x.event1)):
string += str(assertion) + '\n'
self.assertEqual(string, '(a _|_ c | b)\n(c _|_ a | b)\n')
def test_bayesian_model(self):
from pgmpy.models import BayesianModel
import networkx as nx
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)
bm = self.graph.to_bayesian_model()
self.assertIsInstance(bm, BayesianModel)
self.assertListEqual(sorted(bm.nodes()), ['a', 'b', 'c', 'd'])
self.assertTrue(nx.is_chordal(bm.to_undirected()))
def tearDown(self):
del self.graph
示例2: MarkovModel
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import get_local_independencies [as 别名]
from pgmpy.models import MarkovModel
mm = MarkovModel()
mm.add_nodes_from(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'])
mm.add_edges_from([('x1', 'x3'), ('x1', 'x4'), ('x2', 'x4'),
('x2', 'x5'), ('x3', 'x6'), ('x4', 'x6'),
('x4', 'x7'), ('x5', 'x7')])
mm.get_local_independencies()