本文整理汇总了Python中pgmpy.models.BayesianModel.check_model方法的典型用法代码示例。如果您正苦于以下问题:Python BayesianModel.check_model方法的具体用法?Python BayesianModel.check_model怎么用?Python BayesianModel.check_model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmpy.models.BayesianModel
的用法示例。
在下文中一共展示了BayesianModel.check_model方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestBayesianModelCPD
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import check_model [as 别名]
class TestBayesianModelCPD(unittest.TestCase):
def setUp(self):
self.G = BayesianModel([('d', 'g'), ('i', 'g'), ('g', 'l'),
('i', 's')])
def test_active_trail_nodes(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d')), ['d', 'g', 'l'])
self.assertEqual(sorted(self.G.active_trail_nodes('i')), ['g', 'i', 'l', 's'])
def test_active_trail_nodes_args(self):
self.assertEqual(sorted(self.G.active_trail_nodes('d', observed='g')), ['d', 'i', 's'])
self.assertEqual(sorted(self.G.active_trail_nodes('l', observed='g')), ['l'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['i', 'l'])), ['s'])
self.assertEqual(sorted(self.G.active_trail_nodes('s', observed=['d', 'l'])), ['g', 'i', 's'])
def test_is_active_trail_triplets(self):
self.assertTrue(self.G.is_active_trail('d', 'l'))
self.assertTrue(self.G.is_active_trail('g', 's'))
self.assertFalse(self.G.is_active_trail('d', 'i'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='g'))
self.assertFalse(self.G.is_active_trail('d', 'l', observed='g'))
self.assertFalse(self.G.is_active_trail('i', 'l', observed='g'))
self.assertTrue(self.G.is_active_trail('d', 'i', observed='l'))
self.assertFalse(self.G.is_active_trail('g', 's', observed='i'))
def test_is_active_trail(self):
self.assertFalse(self.G.is_active_trail('d', 's'))
self.assertTrue(self.G.is_active_trail('s', 'l'))
self.assertTrue(self.G.is_active_trail('d', 's', observed='g'))
self.assertFalse(self.G.is_active_trail('s', 'l', observed='g'))
def test_is_active_trail_args(self):
self.assertFalse(self.G.is_active_trail('s', 'l', 'i'))
self.assertFalse(self.G.is_active_trail('s', 'l', 'g'))
self.assertTrue(self.G.is_active_trail('d', 's', 'l'))
self.assertFalse(self.G.is_active_trail('d', 's', ['i', 'l']))
def test_get_cpds(self):
cpd_d = TabularCPD('d', 2, np.random.rand(2, 1))
cpd_i = TabularCPD('i', 2, np.random.rand(2, 1))
cpd_g = TabularCPD('g', 2, np.random.rand(2, 4), ['d', 'i'], [2, 2])
cpd_l = TabularCPD('l', 2, np.random.rand(2, 2), ['g'], 2)
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)
self.assertEqual(self.G.get_cpds('d').variable, 'd')
def test_get_cpds1(self):
self.model = BayesianModel([('A', 'AB')])
cpd_a = TabularCPD('A', 2, np.random.rand(2, 1))
cpd_ab = TabularCPD('AB', 2, np.random.rand(2, 2), evidence=['A'],
evidence_card=[2])
self.model.add_cpds(cpd_a, cpd_ab)
self.assertEqual(self.model.get_cpds('A').variable, 'A')
self.assertEqual(self.model.get_cpds('AB').variable, 'AB')
def test_add_single_cpd(self):
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_s)
self.assertListEqual(self.G.get_cpds(), [cpd_s])
def test_add_multiple_cpds(self):
cpd_d = TabularCPD('d', 2, np.random.rand(2, 1))
cpd_i = TabularCPD('i', 2, np.random.rand(2, 1))
cpd_g = TabularCPD('g', 2, np.random.rand(2, 4), ['d', 'i'], [2, 2])
cpd_l = TabularCPD('l', 2, np.random.rand(2, 2), ['g'], 2)
cpd_s = TabularCPD('s', 2, np.random.rand(2, 2), ['i'], 2)
self.G.add_cpds(cpd_d, cpd_i, cpd_g, cpd_l, cpd_s)
self.assertEqual(self.G.get_cpds('d'), cpd_d)
self.assertEqual(self.G.get_cpds('i'), cpd_i)
self.assertEqual(self.G.get_cpds('g'), cpd_g)
self.assertEqual(self.G.get_cpds('l'), cpd_l)
self.assertEqual(self.G.get_cpds('s'), cpd_s)
def test_check_model(self):
cpd_g = TabularCPD('g', 2,
np.array([[0.2, 0.3, 0.4, 0.6],
[0.8, 0.7, 0.6, 0.4]]),
['d', 'i'], [2, 2])
cpd_s = TabularCPD('s', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
['i'], 2)
cpd_l = TabularCPD('l', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
['g'], 2)
self.G.add_cpds(cpd_g, cpd_s, cpd_l)
self.assertTrue(self.G.check_model())
def test_check_model1(self):
cpd_g = TabularCPD('g', 2,
np.array([[0.2, 0.3],
[0.8, 0.7]]),
#.........这里部分代码省略.........
示例2: configure
# 需要导入模块: from pgmpy.models import BayesianModel [as 别名]
# 或者: from pgmpy.models.BayesianModel import check_model [as 别名]
#.........这里部分代码省略.........
for k in range(len(modelDict[j]['pickleData']['original'])):
print modelDict[j]['pickleData']['original'][k]
if modelDict[j]['pickleData']['original'][k] not in modelDict[selfName]['labels']:
modelDict[j]['pickleData']['original'][k] = 'unknown'
for j in modelList:
if modelDict[j]['pickleData']['original'] != modelDict[selfName]['actualLabels']:
failFlag = True
print 'original classifications of', j, 'are not identical to those of', selfName
if failFlag:
return False
# Update netStructureString to reflect changes in the modelList names
strSections = netStructureString.split("'")
for k in range(len(strSections)):
if len(strSections[k]) > 2 and ',' not in strSections[k]:
strSections[k] = strSections[k].split(' ')[0]
netStructureString = "'".join(strSections)
netStructure = ast.literal_eval(netStructureString)
# ---------------------------------------------------------------------------------------------------------------
# iterate through actual labels
# for each actual label, iterate through models
# for each model find classification label of this model for current actual label
# get the index of the current classification and add it to its CPD
# also calculate which item in the joint CPD needs to be incremented
for j in range(len(modelDict[selfName]['actualLabels'])):
currActualLabel = modelDict[selfName]['actualLabels'][j]
row = modelDict[selfName]['labels'].index(currActualLabel)
colVar = np.zeros([len(modelList)])
for k in range(len(modelList)):
cmod = modelList[k]
if k != 0:
pmod = modelList[k-1]
colVar *= len(modelDict[pmod]['labels'])
colVar[k] = modelDict[cmod]['labels'].index(
modelDict[cmod]['pickleData']['results'][j])
modelDict[cmod]['CPD'][0, colVar[k]] += 1
col = sum(colVar)
modelDict[selfName]['CPD'][row, col] += 1
# take all CPD's and normalise the matrices
evidenceCard = copy.deepcopy(modelList)
for j in modelDict:
if j == selfName:
# this is a joint CPD matrix
# normalise columns to have sum = 1
modelDict[j]['CPD'] = normalize(modelDict[j]['CPD'], axis=0, norm='l1')
else:
# normalise sum of matrix = 1
modelDict[j]['CPD'] /= np.sum(modelDict[j]['CPD'])
evidenceCard[evidenceCard.index(j)] = len(modelDict[j]['labels'])
print modelDict[j]['CPD']
model = BayesianModel(netStructure)
# create TabularCPD data structure to nest calculated CPD
for j in modelDict:
if j == selfName:
modelDict[j]['cpdObject'] = TabularCPD(variable=j, variable_card=len(modelDict[j]['labels']),
values=modelDict[j]['CPD'],
evidence=modelList,
evidence_card=evidenceCard)
else:
modelDict[j]['cpdObject'] = TabularCPD(variable=j,
variable_card=len(modelDict[j]['labels']),
values=modelDict[j]['CPD'])
# Associating the CPDs with the network
for j in modelDict:
model.add_cpds(modelDict[j]['cpdObject'])
# check_model checks for the network structure and CPDs and verifies that the CPDs are correctly
# defined and sum to 1.
if not model.check_model():
print 'Model check returned unsuccessful'
return False
infer = VariableElimination(model)
confMatrix = np.zeros(len(modelDict[selfName]['labels']))
# iterate over all original data and perform classifications to calculate if accuracy with PGN has increased
for j in range(len(modelDict[selfName]['actualLabels'])):
currEvidenceDict = dict()
for k in modelList:
currEvidenceDict[k] = modelDict[k]['labels'].index(modelDict[k]['pickleData']['results'][j])
q = infer.query([selfName], currEvidenceDict)
inferenceClass = modelDict[selfName]['labels'][np.argmax(q[selfName].values)]
actualClass = modelDict[selfName]['actualLabels'][j]
confMatrix[modelDict[selfName].index(actualClass), modelDict[selfName].index(inferenceClass)] += 1
print "%Accuracy with PGN"
dCalc = SAMTesting.calculateData(modelDict[selfName]['actualLabels'], confMatrix)
return True