本文整理汇总了Python中pgmpy.models.MarkovModel.edges方法的典型用法代码示例。如果您正苦于以下问题:Python MarkovModel.edges方法的具体用法?Python MarkovModel.edges怎么用?Python MarkovModel.edges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.MarkovModel
的用法示例。
在下文中一共展示了MarkovModel.edges方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import edges [as 别名]
class generate(object):
def __init__(self, adj_mat=None, struct=None):
DEBUG = False
self.G = MarkovModel()
self.n_nodes = adj_mat.shape[0]
if DEBUG: print 'struct', struct
if struct == 'complete':
self._complete_graph(adj_mat)
if struct == 'nodes':
self._nodes_only(adj_mat)
if struct is None:
self._import_adj(adj_mat)
self._ising_factors(Wf=5, Wi=5, f_type='mixed')
if DEBUG: print 'generate_init', self.G, self.G.nodes()
def get_model(self):
return self.G
def _complete_graph(self, adj_mat):
"""
generate the complete graph over len(adj_mat)
"""
self._nodes_only(adj_mat)
for i in range(self.n_nodes):
self.G.add_edges_from([(i, j)
for j in range(self.n_nodes)])
def _import_adj(self, adj_mat):
"""
add nodes and edges to graph
adj_mat - square matrix, numpy array like
"""
DEBUG = False
assert (adj_mat is not None), "can't import empty adj mat"
# add nodes
self._nodes_only(adj_mat)
# add edges
for i in range(self.n_nodes):
edges_list = ([(i, j)
for j in range(self.n_nodes)
if adj_mat[i][j]])
if DEBUG: print edges_list
self.G.add_edges_from(edges_list)
if DEBUG: print len(self.G)
def _nodes_only(self, adj_mat):
"""
add nodes to graph
adj_mat - aquare matrix, numpy array like
"""
global DEBUG
assert (adj_mat is not None), "can't import empty adj mat"
assert (self.n_nodes == adj_mat.shape[1]), "adj_mat is not sqaure"
self.G.add_nodes_from([i for i in range(self.n_nodes)])
if DEBUG: print '_nodes_only', [i for i in range(self.n_nodes)]
if DEBUG: print '_nodes_only print G', self.G.nodes()
assert (self.n_nodes == len(self.G)), "graph size is incosistent with adj_mat"
def _ising_factors(self, Wf=1, Wi=1, f_type='mixed'):
"""
Add ising-like factors to model graph
cardinality is the number of possible values
in our case we have boolean nodes, thus cardinality = 2
Wf = \theta_i = ~U[-Wf, Wf]
type = 'mixed' = ~U[-Wi,Wi]
'attractive' = ~U[0,Wi]
"""
self._field_factors(Wf)
self._interact_factors(Wi, f_type)
def _field_factors(self, w, states=2):
"""
this function assigns factor for single node
currently states=2 for ising model generation
"""
for i in self.G.nodes():
phi_i = Factor([i], [states], self._wf(w, states))
self.G.add_factors(phi_i)
def _interact_factors(self, w, f_type, states=2):
"""
this function assigns factor for two interacting nodes
currently states=2 for ising model generation
"""
for e in self.G.edges():
# if DEBUG: print 'interact_factors edges,states, values',e,[e[0],
# e[1]],len(e)*[states], self._wi(w, f_type, states)
phi_ij = Factor([e[0], e[1]], [states] * len(e), self._wi(w, f_type, states))
self.G.add_factors(phi_ij)
def _wf(self, w, k):
"""
generate field factor
"""
# if DEBUG: print 'w',type(w),w
return np.random.uniform(low=-1 * w, high=w, size=k)
#.........这里部分代码省略.........
示例2: MarkovModel
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import edges [as 别名]
import numpy as np
import pandas as pd
from pgmpy.models import MarkovModel
from pgmpy.estimators import MaximumLikelihoodEstimator
# Generating random data
raw_data = np.random.randint(low=0, high=2, size=(1000, 2))
data = pd.DataFrame(raw_data, columns=['X', 'Y'])
model = MarkovModel()
model.fit(data, estimator=MaximumLikelihoodEstimator)
model.get_factors()
model.nodes()
model.edges()
示例3: TestMarkovModelMethods
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import edges [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))
#.........这里部分代码省略.........
示例4: TestUndirectedGraphTriangulation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import edges [as 别名]
class TestUndirectedGraphTriangulation(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_check_clique(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.check_clique(['a', 'b', 'c']))
def test_is_triangulated(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.is_triangulated())
def test_triangulation_h1_inplace(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)
self.graph.triangulate(heuristic='H1', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h2_inplace(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)
self.graph.triangulate(heuristic='H2', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h3_inplace(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)
self.graph.triangulate(heuristic='H3', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h4_inplace(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)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h5_inplace(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)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h6_inplace(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)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_cardinality_mismatch_raises_error(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
factor_list = [Factor(edge, [2, 2], np.random.rand(4)) for edge in
#.........这里部分代码省略.........
示例5: TestMarkovModelCreation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import edges [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
示例6: TestUndirectedGraphTriangulation
# 需要导入模块: from pgmpy.models import MarkovModel [as 别名]
# 或者: from pgmpy.models.MarkovModel import edges [as 别名]
class TestUndirectedGraphTriangulation(unittest.TestCase):
def setUp(self):
self.graph = MarkovModel()
def test_check_clique(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.is_clique(['a', 'b', 'c']))
def test_is_triangulated(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
self.assertTrue(self.graph.is_triangulated())
def test_triangulation_h1_inplace(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)
self.graph.triangulate(heuristic='H1', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h2_inplace(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)
self.graph.triangulate(heuristic='H2', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'c'], ['a', 'd'],
['b', 'c'], ['c', 'd']])
def test_triangulation_h3_inplace(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)
self.graph.triangulate(heuristic='H3', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h4_inplace(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)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h5_inplace(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)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_triangulation_h6_inplace(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)
self.graph.triangulate(heuristic='H4', inplace=True)
self.assertTrue(self.graph.is_triangulated())
self.assertListEqual(hf.recursive_sorted(self.graph.edges()),
[['a', 'b'], ['a', 'd'], ['b', 'c'],
['b', 'd'], ['c', 'd']])
def test_cardinality_mismatch_raises_error(self):
self.graph.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'),
('d', 'a')])
factor_list = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in
#.........这里部分代码省略.........