本文整理汇总了Python中pgmpy.models.BayesianModel类的典型用法代码示例。如果您正苦于以下问题:Python BayesianModel类的具体用法?Python BayesianModel怎么用?Python BayesianModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BayesianModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
self.sn2 = {'grade': ['A', 'B', 'F'], 'diff': ['high', 'low'],
'intel': ['poor', 'good', 'very good']}
self.sn1 = {'speed': ['low', 'medium', 'high'],
'switch': ['on', 'off'], 'time': ['day', 'night']}
self.phi1 = DiscreteFactor(['speed', 'switch', 'time'],
[3, 2, 2], np.ones(12))
self.phi2 = DiscreteFactor(['speed', 'switch', 'time'],
[3, 2, 2], np.ones(12), state_names=self.sn1)
self.cpd1 = TabularCPD('grade', 3, [[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.cpd2 = TabularCPD('grade', 3, [[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],
state_names=self.sn2)
student = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
diff_cpd = TabularCPD('diff', 2, [[0.2, 0.8]])
intel_cpd = TabularCPD('intel', 2, [[0.3, 0.7]])
grade_cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1],
[0.1, 0.1, 0.1, 0.1],
[0.8, 0.8, 0.8, 0.8]],
evidence=['diff', 'intel'],
evidence_card=[2, 2])
student.add_cpds(diff_cpd, intel_cpd, grade_cpd)
self.model1 = Inference(student)
self.model2 = Inference(student, state_names=self.sn2)
示例2: main
def main():
# Defining the network structure
model = BayesianModel([('C', 'H'), ('P', 'H')])
# H: host
# P: prize
# C: contestant
# Defining the CPDs:
cpd_c = TabularCPD('C', 3, [[0.33, 0.33, 0.33]])
cpd_p = TabularCPD('P', 3, [[0.33, 0.33, 0.33]])
cpd_h = TabularCPD('H', 3, [[0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 0.0, 1.0, 0.5],
[0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.5],
[0.5, 1.0, 0.0, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0]],
evidence=['C', 'P'], evidence_card=[3, 3])
# Associating the CPDs with the network structure.
model.add_cpds(cpd_c, cpd_p, cpd_h)
# Some other methods
# model.get_cpds()
# check_model check for the model structure and the associated CPD and
# returns True if everything is correct otherwise throws an exception
# print model.check_model()
# Infering the posterior probability
infer = VariableElimination(model)
posterior_p = infer.query(['H'], evidence={'C': 0, 'P': 0})
print(posterior_p['H'])
示例3: TestBayesianModelMethods
class TestBayesianModelMethods(unittest.TestCase):
def setUp(self):
self.G = BayesianModel([('a', 'd'), ('b', 'd'),
('d', 'e'), ('b', 'c')])
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_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']))
def tearDown(self):
del self.G
示例4: estimate
def estimate(self):
"""
Estimates the `BayesianModel` structure that fits best to the given data set,
according to the scoring method supplied in the constructor.
Exhaustively searches through all models. Only estimates network structure, no parametrization.
Returns
-------
model: `BayesianModel` instance
A `BayesianModel` with maximal score.
Examples
--------
>>> import pandas as pd
>>> import numpy as np
>>> from pgmpy.estimators import ExhaustiveSearch
>>> # create random data sample with 3 variables, where B and C are identical:
>>> data = pd.DataFrame(np.random.randint(0, 5, size=(5000, 2)), columns=list('AB'))
>>> data['C'] = data['B']
>>> est = ExhaustiveSearch(data)
>>> best_model = est.estimate()
>>> best_model
<pgmpy.models.BayesianModel.BayesianModel object at 0x7f695c535470>
>>> best_model.edges()
[('B', 'C')]
"""
best_dag = max(self.all_dags(), key=self.scoring_method.score)
best_model = BayesianModel()
best_model.add_nodes_from(sorted(best_dag.nodes()))
best_model.add_edges_from(sorted(best_dag.edges()))
return best_model
示例5: test_moral_graph_with_edge_present_over_parents
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')])
示例6: __init__
def __init__(self, model):
self.model = model
model.check_model()
if isinstance(model, JunctionTree):
self.variables = set(chain(*model.nodes()))
else:
self.variables = model.nodes()
self.cardinality = {}
self.factors = defaultdict(list)
if isinstance(model, BayesianModel):
for node in model.nodes():
cpd = model.get_cpds(node)
cpd_as_factor = cpd.to_factor()
self.cardinality[node] = cpd.variable_card
for var in cpd.variables:
self.factors[var].append(cpd_as_factor)
elif isinstance(model, (MarkovModel, FactorGraph, JunctionTree)):
self.cardinality = model.get_cardinality()
for factor in model.get_factors():
for var in factor.variables:
self.factors[var].append(factor)
elif isinstance(model, DynamicBayesianNetwork):
self.start_bayesian_model = BayesianModel(model.get_intra_edges(0))
self.start_bayesian_model.add_cpds(*model.get_cpds(time_slice=0))
cpd_inter = [model.get_cpds(node) for node in model.get_interface_nodes(1)]
self.interface_nodes = model.get_interface_nodes(0)
self.one_and_half_model = BayesianModel(model.get_inter_edges() + model.get_intra_edges(1))
self.one_and_half_model.add_cpds(*(model.get_cpds(time_slice=1) + cpd_inter))
示例7: TestInferenceBase
class TestInferenceBase(unittest.TestCase):
def setUp(self):
self.bayesian = BayesianModel([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e')])
a_cpd = TabularCPD('a', 2, [[0.4, 0.6]])
b_cpd = TabularCPD('b', 2, [[0.2, 0.4], [0.3, 0.4]], evidence='a', evidence_card=[2])
c_cpd = TabularCPD('c', 2, [[0.1, 0.2], [0.3, 0.4]], evidence='b', evidence_card=[2])
d_cpd = TabularCPD('d', 2, [[0.4, 0.3], [0.2, 0.1]], evidence='c', evidence_card=[2])
e_cpd = TabularCPD('e', 2, [[0.3, 0.2], [0.4, 0.1]], evidence='d', evidence_card=[2])
self.bayesian.add_cpd([a_cpd, b_cpd, c_cpd, d_cpd, e_cpd])
self.markov = MarkovModel([('a', 'b'), ('b', 'd'), ('a', 'c'), ('c', 'd')])
factor_1 = Factor(['a', 'b'], [2, 2], np.array([100, 1, 1, 100]))
factor_2 = Factor(['a', 'c'], [2, 2], np.array([40, 30, 100, 20]))
factor_3 = Factor(['b', 'd'], [2, 2], np.array([1, 100, 100, 1]))
factor_4 = Factor(['c', 'd'], [2, 2], np.array([60, 60, 40, 40]))
self.markov.add_factors(factor_1, factor_2, factor_3, factor_4)
def test_bayesian_inference_init(self):
infer_bayesian = Inference(self.bayesian)
self.assertEqual(set(infer_bayesian.variables), {'a', 'b', 'c', 'd', 'e'})
self.assertEqual(infer_bayesian.cardinality, {'a': 2, 'b': 2, 'c': 2, 'd': 2, 'e': 2})
# self.assertEqual(infer_bayesian.factors, {'a': [self.bayesian.get_cpd('a').to_factor(),
# self.bayesian.get_cpd('b').to_factor()],
# 'b': [self.bayesian.get_cpd('b').to_factor(),
# self.bayesian.get_cpd('c').to_factor()],
# 'c': [self.bayesian.get_cpd('c').to_factor(),
# self.bayesian.get_cpd('d').to_factor()],
# 'd': [self.bayesian.get_cpd('d').to_factor(),
# self.bayesian.get_cpd('e').to_factor()],
# 'e': [self.bayesian.get_cpd('e').to_factor()]})
def test_markov_inference_init(self):
infer_markov = Inference(self.markov)
self.assertEqual(set(infer_markov.variables), {'a', 'b', 'c', 'd'})
self.assertEqual(infer_markov.cardinality, {'a': 2, 'b': 2, 'c': 2, 'd': 2})
示例8: test_is_iequivalent
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))
示例9: setUp
def setUp(self):
nodes = {'c': {'STATES': ['Present', 'Absent'],
'DESCRIPTION': '(c) Brain Tumor',
'YPOS': '11935',
'XPOS': '15250',
'TYPE': 'discrete'},
'a': {'STATES': ['Present', 'Absent'],
'DESCRIPTION': '(a) Metastatic Cancer',
'YPOS': '10465',
'XPOS': '13495',
'TYPE': 'discrete'},
'b': {'STATES': ['Present', 'Absent'],
'DESCRIPTION': '(b) Serum Calcium Increase',
'YPOS': '11965',
'XPOS': '11290',
'TYPE': 'discrete'},
'e': {'STATES': ['Present', 'Absent'],
'DESCRIPTION': '(e) Papilledema',
'YPOS': '13240',
'XPOS': '17305',
'TYPE': 'discrete'},
'd': {'STATES': ['Present', 'Absent'],
'DESCRIPTION': '(d) Coma',
'YPOS': '12985',
'XPOS': '13960',
'TYPE': 'discrete'}}
model = BayesianModel([('b', 'd'), ('a', 'b'), ('a', 'c'), ('c', 'd'), ('c', 'e')])
cpd_distribution = {'a': {'TYPE': 'discrete', 'DPIS': np.array([[0.2, 0.8]])},
'e': {'TYPE': 'discrete', 'DPIS': np.array([[0.8, 0.2],
[0.6, 0.4]]), 'CONDSET': ['c'], 'CARDINALITY': [2]},
'b': {'TYPE': 'discrete', 'DPIS': np.array([[0.8, 0.2],
[0.2, 0.8]]), 'CONDSET': ['a'], 'CARDINALITY': [2]},
'c': {'TYPE': 'discrete', 'DPIS': np.array([[0.2, 0.8],
[0.05, 0.95]]), 'CONDSET': ['a'], 'CARDINALITY': [2]},
'd': {'TYPE': 'discrete', 'DPIS': np.array([[0.8, 0.2],
[0.9, 0.1],
[0.7, 0.3],
[0.05, 0.95]]), 'CONDSET': ['b', 'c'], 'CARDINALITY': [2, 2]}}
tabular_cpds = []
for var, values in cpd_distribution.items():
evidence = values['CONDSET'] if 'CONDSET' in values else []
cpd = values['DPIS']
evidence_card = values['CARDINALITY'] if 'CARDINALITY' in values else []
states = nodes[var]['STATES']
cpd = TabularCPD(var, len(states), cpd,
evidence=evidence,
evidence_card=evidence_card)
tabular_cpds.append(cpd)
model.add_cpds(*tabular_cpds)
for var, properties in nodes.items():
model.node[var] = properties
self.maxDiff = None
self.writer = XMLBeliefNetwork.XBNWriter(model=model)
示例10: to_bayesian_model
def to_bayesian_model(self):
"""
Creates a Bayesian Model which is a minimum I-Map for this markov model.
The ordering of parents may not remain constant. It would depend on the
ordering of variable in the junction tree (which is not constant) all the
time.
Examples
--------
>>> from pgmpy.models import MarkovModel
>>> from pgmpy.factors.discrete import DiscreteFactor
>>> 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')])
>>> phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in mm.edges()]
>>> mm.add_factors(*phi)
>>> bm = mm.to_bayesian_model()
"""
from pgmpy.models import BayesianModel
bm = BayesianModel()
var_clique_dict = defaultdict(tuple)
var_order = []
# Create a junction tree from the markov model.
# Creation of clique tree involves triangulation, finding maximal cliques
# and creating a tree from these cliques
junction_tree = self.to_junction_tree()
# create an ordering of the nodes based on the ordering of the clique
# in which it appeared first
root_node = junction_tree.nodes()[0]
bfs_edges = nx.bfs_edges(junction_tree, root_node)
for node in root_node:
var_clique_dict[node] = root_node
var_order.append(node)
for edge in bfs_edges:
clique_node = edge[1]
for node in clique_node:
if not var_clique_dict[node]:
var_clique_dict[node] = clique_node
var_order.append(node)
# create a bayesian model by adding edges from parent of node to node as
# par(x_i) = (var(c_k) - x_i) \cap {x_1, ..., x_{i-1}}
for node_index in range(len(var_order)):
node = var_order[node_index]
node_parents = (set(var_clique_dict[node]) - set([node])).intersection(
set(var_order[:node_index]))
bm.add_edges_from([(parent, node) for parent in node_parents])
# TODO : Convert factor into CPDs
return bm
示例11: BaseEliminationTest
class BaseEliminationTest(TestCase):
def setUp(self):
self.model = BayesianModel([('diff', 'grade'), ('intel', 'grade'), ('intel', 'sat'),
('grade', 'reco')])
raw_data = np.random.randint(low=0, high=2, size=(1000, 5))
data = pd.DataFrame(raw_data, columns=['diff', 'grade', 'intel', 'sat', 'reco'])
self.model.fit(data)
def tearDown(self):
del self.model
del self.elimination_order
示例12: setUp
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')])
示例13: test_estimate_from_independencies
def test_estimate_from_independencies(self):
ind = Independencies(['B', 'C'], ['A', ['B', 'C'], 'D'])
ind = ind.closure()
model = ConstraintBasedEstimator.estimate_from_independencies("ABCD", ind)
self.assertSetEqual(set(model.edges()),
set([('B', 'D'), ('A', 'D'), ('C', 'D')]))
model1 = BayesianModel([('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'E')])
model2 = ConstraintBasedEstimator.estimate_from_independencies(
model1.nodes(),
model1.get_independencies())
self.assertTrue(set(model2.edges()) == set(model1.edges()) or
set(model2.edges()) == set([('B', 'C'), ('A', 'C'), ('C', 'E'), ('D', 'B')]))
示例14: bayesnet_examples
def bayesnet_examples():
from pgmpy.factors import TabularCPD
from pgmpy.models import BayesianModel
import pandas as pd
student_model = BayesianModel([('D', 'G'),
('I', 'G'),
('G', 'L'),
('I', 'S')])
# we can generate some random data.
raw_data = np.random.randint(low=0, high=2, size=(1000, 5))
data = pd.DataFrame(raw_data, columns=['D', 'I', 'G', 'L', 'S'])
data_train = data[: int(data.shape[0] * 0.75)]
student_model.fit(data_train)
student_model.get_cpds()
data_test = data[int(0.75 * data.shape[0]): data.shape[0]]
data_test.drop('D', axis=1, inplace=True)
student_model.predict(data_test)
grade_cpd = TabularCPD(
variable='G',
variable_card=3,
values=[[0.3, 0.05, 0.9, 0.5],
[0.4, 0.25, 0.08, 0.3],
[0.3, 0.7, 0.02, 0.2]],
evidence=['I', 'D'],
evidence_card=[2, 2])
difficulty_cpd = TabularCPD(
variable='D',
variable_card=2,
values=[[0.6, 0.4]])
intel_cpd = TabularCPD(
variable='I',
variable_card=2,
values=[[0.7, 0.3]])
letter_cpd = TabularCPD(
variable='L',
variable_card=2,
values=[[0.1, 0.4, 0.99],
[0.9, 0.6, 0.01]],
evidence=['G'],
evidence_card=[3])
sat_cpd = TabularCPD(
variable='S',
variable_card=2,
values=[[0.95, 0.2],
[0.05, 0.8]],
evidence=['I'],
evidence_card=[2])
student_model.add_cpds(grade_cpd, difficulty_cpd,
intel_cpd, letter_cpd,
sat_cpd)
示例15: setUp
def setUp(self):
self.junction_tree = JunctionTree([(('A', 'B'), ('B', 'C')),
(('B', 'C'), ('C', 'D'))])
phi1 = Factor(['A', 'B'], [2, 3], range(6))
phi2 = Factor(['B', 'C'], [3, 2], range(6))
phi3 = Factor(['C', 'D'], [2, 2], range(4))
self.junction_tree.add_factors(phi1, phi2, phi3)
self.bayesian_model = BayesianModel([('A', 'J'), ('R', 'J'), ('J', 'Q'),
('J', 'L'), ('G', 'L')])
cpd_a = TabularCPD('A', 2, [[0.2], [0.8]])
cpd_r = TabularCPD('R', 2, [[0.4], [0.6]])
cpd_j = TabularCPD('J', 2,
[[0.9, 0.6, 0.7, 0.1],
[0.1, 0.4, 0.3, 0.9]],
['R', 'A'], [2, 2])
cpd_q = TabularCPD('Q', 2,
[[0.9, 0.2],
[0.1, 0.8]],
['J'], [2])
cpd_l = TabularCPD('L', 2,
[[0.9, 0.45, 0.8, 0.1],
[0.1, 0.55, 0.2, 0.9]],
['G', 'J'], [2, 2])
cpd_g = TabularCPD('G', 2, [[0.6], [0.4]])
self.bayesian_model.add_cpds(cpd_a, cpd_g, cpd_j, cpd_l, cpd_q, cpd_r)