-
Notifications
You must be signed in to change notification settings - Fork 1
/
TabularCPD.py
30 lines (27 loc) · 1.06 KB
/
TabularCPD.py
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
import numpy as np
class TabularCPD():
def __init__(self, variable, values, evidence=[]):
self.cpd = self.create_cpd(values)
self.target = variable
self.parents = list(reversed(evidence))
def create_cpd(self, prob):
'''
:param prob:
:return: cpd table, ndarray
'''
'''
create cpd, the order is for tmp is (target, parent(reversed order of evidence))
for this example, [pollution, smoker]
cpd_cancer = TabularCPD(variable='Cancer',
values=[[0.03, 0.05, 0.001, 0.02],
[0.97, 0.95, 0.999, 0.98]],
evidence=['Smoker', 'Pollution'],)
'''
prob_arr = np.array(prob)
prob_fla = prob_arr.flatten("F")
para_size = np.log2(prob_fla.size)
tmp = []
for i in range(int(para_size)):
tmp.append(np.array([[True] * (2 ** i) + [False] * (2 ** i)] * (int(prob_fla.size) // (2 ** (i + 1)))).flatten())
tmp.append(prob_fla)
return np.column_stack(tmp)