本文整理汇总了Python中pgmpy.models.MarkovModel.neighbors方法的典型用法代码示例。如果您正苦于以下问题:Python MarkovModel.neighbors方法的具体用法?Python MarkovModel.neighbors怎么用?Python MarkovModel.neighbors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.MarkovModel
的用法示例。
在下文中一共展示了MarkovModel.neighbors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestMarkovModelCreation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import neighbors [as 别名]
class TestMarkovModelCreation(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_class_init_without_data(self):
self.assertIsInstance(self.graph, MarkovModel)
def test_class_init_with_data_string(self):
self.g = MarkovModel([('a', 'b'), ('b', 'c')])
self.assertListEqual(sorted(self.g.nodes()), ['a', 'b', 'c'])
self.assertListEqual(hf.recursive_sorted(self.g.edges()),
[['a', 'b'], ['b', 'c']])
def test_class_init_with_data_nonstring(self):
self.g = MarkovModel([(1, 2), (2, 3)])
def test_add_node_string(self):
self.graph.add_node('a')
self.assertListEqual(self.graph.nodes(), ['a'])
def test_add_node_nonstring(self):
self.graph.add_node(1)
def test_add_nodes_from_string(self):
self.graph.add_nodes_from(['a', 'b', 'c', 'd'])
self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'c', 'd'])
def test_add_nodes_from_non_string(self):
self.graph.add_nodes_from([1, 2, 3, 4])
def test_add_edge_string(self):
self.graph.add_edge('d', 'e')
self.assertListEqual(sorted(self.graph.nodes()), ['d', 'e'])
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['d', 'e']])
self.graph.add_nodes_from(['a', 'b', 'c'])
self.graph.add_edge('a', 'b')
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['d', 'e']])
def test_add_edge_nonstring(self):
self.graph.add_edge(1, 2)
def test_add_edge_selfloop(self):
self.assertRaises(ValueError, self.graph.add_edge, 'a', 'a')
def test_add_edges_from_string(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c')])
self.assertListEqual(sorted(self.graph.nodes()), ['a', 'b', 'c'])
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['b', 'c']])
self.graph.add_nodes_from(['d', 'e', 'f'])
self.graph.add_edges_from([('d', 'e'), ('e', 'f')])
self.assertListEqual(sorted(self.graph.nodes()),
['a', 'b', 'c', 'd', 'e', 'f'])
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
hf.recursive_sorted([('a', 'b'), ('b', 'c'),
('d', 'e'), ('e', 'f')]))
def test_add_edges_from_nonstring(self):
self.graph.add_edges_from([(1, 2), (2, 3)])
def test_add_edges_from_self_loop(self):
self.assertRaises(ValueError, self.graph.add_edges_from,
[('a', 'a')])
def test_number_of_neighbors(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c')])
self.assertEqual(len(self.graph.neighbors('b')), 2)
def tearDown(self):
del self.graph
示例2: TestUndirectedGraphTriangulation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import neighbors [as 别名]
#.........这里部分代码省略.........
phi4 = DiscreteFactor(['d', 'a'], [5, 2], np.random.random(10))
self.graph.add_factors(phi1, phi2, phi3, phi4)
H = self.graph.triangulate(heuristic='H5', inplace=True)
self.assertListEqual(hf.recursive_sorted(H.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h6_create_new(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)
H = self.graph.triangulate(heuristic='H6', inplace=True)
self.assertListEqual(hf.recursive_sorted(H.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_copy(self):
# Setup the original graph
self.graph.add_nodes_from(['a', 'b'])
self.graph.add_edges_from([('a', 'b')])
# Generate the copy
copy = self.graph.copy()
# Ensure the copied model is correct
self.assertTrue(copy.check_model())
# Basic sanity checks to ensure the graph was copied correctly
self.assertEqual(len(copy.nodes()), 2)
self.assertListEqual(copy.neighbors('a'), ['b'])
self.assertListEqual(copy.neighbors('b'), ['a'])
# Modify the original graph ...
self.graph.add_nodes_from(['c'])
self.graph.add_edges_from([('c', 'b')])
# ... and ensure none of those changes get propagated
self.assertEqual(len(copy.nodes()), 2)
self.assertListEqual(copy.neighbors('a'), ['b'])
self.assertListEqual(copy.neighbors('b'), ['a'])
with self.assertRaises(nx.NetworkXError):
copy.neighbors('c')
# Ensure the copy has no factors at this point
self.assertEqual(len(copy.get_factors()), 0)
# Add factors to the original graph
phi1 = DiscreteFactor(['a', 'b'], [2, 2], [[0.3, 0.7], [0.9, 0.1]])
self.graph.add_factors(phi1)
# The factors should not get copied over
with self.assertRaises(AssertionError):
self.assertListEqual(copy.get_factors(), self.graph.get_factors())
# Create a fresh copy
del copy
copy = self.graph.copy()
self.assertListEqual(copy.get_factors(), self.graph.get_factors())
# If we change factors in the original, it should not be passed to the clone
phi1.values = np.array([[0.5, 0.5], [0.5, 0.5]])
self.assertNotEqual(self.graph.get_factors(), copy.get_factors())