* is just one line: learner.initializeWithPriors(network, NofVirtualSamples);
*/
public static void learnWithPriors(String networkFile, String dataFile, int NofVirtualSamples) {
XmlBifReader bifReader = new XmlBifReader();
BeliefNetwork network;
try {
// load network and show the marginals
network = bifReader.loadFromFile(networkFile);
if (network == null) {
System.out.println("Problems with loading the network");
return;
}
Set<BNNode> nodes = network.vertexSet();
System.out.println("CPTs of original network\n");
for (BNNode node : nodes) {
System.out.println(
node.getName() + ":\n" + node.getFunction().toString()
+ "\n");
}
// run inference
JTInference inference = new JTInference();
inference.initialize(network, new JoinTreeCompiler());
inference.run();
// output inference results
System.out.println("Marginals of original network\n");
for (BNNode node : nodes) {
System.out.println(
node.getName() + ":\n" + node.getMarginal().toString()
+ "\n");
}
// learning starts here
// create a new learner
BayesianLearner learner = new BayesianLearner();
// you need to initialize the link between the learner and network
// this initialization will clear existing
// probability tables from the network and set up uniform Dirichlet priors
learner.initializeWithPriors(network, NofVirtualSamples);
System.out.println("CPTs Before Learning \n");
for (BNNode node : nodes) {
System.out.println(
node.getName() + ":\n" + node.getFunction().toString()
+ "\n");
}
// create a new data source for the learner
GraphDataFile graphData = new GraphDataFile();
// now, populate the data source, in this case from file
graphData.readArff(dataFile);
// you need to connect it too, which will help
// the instance IGraphData to understand how to
// format dta so that they fit the network
graphData.connect(network);
// finally, learn!
learner.learnFromTable(graphData);
inference = new JTInference();
inference.initialize(network, new JoinTreeCompiler());
inference.run();
// now, show the probabilities again
// nodes = network.vertexSet();
System.out.println("CPTs after learning\n");
nodes = network.vertexSet();
for (BNNode node : nodes) {
System.out.println(
node.getName() + ":\n" + node.getFunction().toString()
+ "\n");
}