1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import copy
import numpy as np
from beliefs.factors.discrete_factor import DiscreteFactor
class TabularCPD(DiscreteFactor):
"""
Defines the conditional probability table for a discrete variable
whose parents are also discrete.
"""
def __init__(self, variable, variable_card, parents=[], parents_card=[],
values=[], state_names=None):
"""
Args
variable: int or string
variable_card: int
parents: list,
(optional) list of int and/or strings
parents_card: list,
(optional) list of int
values: 2-d list or array (optional)
state_names: dictionary (optional),
mapping variables to their states, of format {label_name: ['state1', 'state2']}
"""
super().__init__(variables=[variable] + parents,
cardinality=[variable_card] + parents_card,
values=values,
state_names=state_names)
self.variable = variable
self.parents = list(parents)
def get_values(self):
"""
Returns the tabular cpd form of the values.
"""
if len(self.cardinality) == 1:
return self.values.reshape(1, np.prod(self.cardinality))
else:
return self.values.reshape(self.cardinality[0], np.prod(self.cardinality[1:]))
def copy(self):
return copy.deepcopy(self)
|