Bayesian network using using the Jayes library
Code: "neural_net_jayes3.py". Programming language: Python DMelt Version 1. Last modified: 10/27/2015. License: Pro
https://datamelt.org/code/cache/neural_net_jayes3_8114.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


# it shows how to build a tree with probabilities


from bayesnet.jayes import BayesNode,BayesNet
from bayesnet.jayes.inference.jtree import JunctionTreeAlgorithm
from  java.util import *

net = BayesNet()
a = net.createNode("visitor")
a.addOutcomes("smoking", "non-smoking")
a.setProbabilities(0.4, 0.6)


b=net.createNode("condition");
b.addOutcomes("ill", "healty");
b.setParents(Arrays.asList(a));
b.setProbabilities(
  0.8, 0.2, # a == true
  0.3, 0.7, # a == false
);

c = net.createNode("sickness");
c.addOutcomes("Bronchitis", "Cancer", "Dyspnoea");
c.setParents(Arrays.asList(b));
c.setProbabilities(
  0.4, 0.5, 0.2, # b == ill 
  0.3, 0.3, 0.4, # a == healty 
);

inferer=JunctionTreeAlgorithm()
inferer.setNetwork(net)

evidence = HashMap()
evidence.put(a, "smoking");
evidence.put(b, "ill");
inferer.setEvidence(evidence);
beliefsC = inferer.getBeliefs(c);
print "P(c | a=smoking, b=ill)=",beliefsC.tolist()

Ads help maintain this website.