• Aucun résultat trouvé

Coding Up the Network

Dans le document Machine Learning (Page 107-117)

You have down on paper (sort of) what you are looking to do. The next step is to create some code. You’re going to work through a complete project from start to fi nish. I’m using Eclipse for this project, but you can easily substitute your own integrated development environment (IDE).

Creating the Project

First, create a clean project with which to work. Click File ➪ New ➪ Java Project.

Call this project BayesNetDemo, as shown in Figure 4-5.

Figure 4-5: Creating a new project

Adding the JavaBayes Library

You need the JavaBayes library to complete the demo, so if you haven’t done so already, get the precompiled jar fi le from the Github repository at https://

github.com/jasebell/JavaBayesAPI.

Create a new directory on your fi lesystem and via the command line type the following (see Figure 4-6):

git clone https://github.com/jasebell/JavaBayesAPI

Back in your IDE, you need to add the jar fi le to your build properties so your code can see the library. In Eclipse you do this by clicking the name of the project in the project chooser window. Right-click and you see the Properties option.

Click Java Build Path and then click Add External JARs, as shown in Figure 4-7.

Find the fi le location of the jar fi le and click Open.

Figure 4-6: Cloning the Git repository

Figure 4-7: Project properties

Creating the Base Graph

With the library added, you can start to put some code in place. Create a new Java class called BayesNetExample.java. In Eclipse, select File ➪ New ➪ Class, as shown in Figure 4-8. Make sure you have a main method to run the code as well.

Figure 4-8: Creating a new class

Create the constructor and add an InferenceGraph class. This is your graph network; you’ll use this class to do the belief calculations at the end.

import javabayes.Helpers.BayesNetHelper;

import javabayes.InferenceGraphs.InferenceGraph;

import javabayes.InferenceGraphs.InferenceGraphNode;

public class BayesNetExample { public BayesNetExample() {

InferenceGraph inferenceGraph = new InferenceGraph();

}

public static void main(String[] args) {

BayesNetExample bne = new BayesNetExample();

} }

I’ve added the line in the main method to ensure that the program runs.

Adding the Nodes

You have four nodes: the age of the patient, the patient’s smoking status, the duration of the condition, and the surgical outcome. What’s required code-wise is the creation of each node connected to the graph. First, here is a Java code representation of the age node:

InferenceGraphNode age = BayesNetHelper.createNode(inferenceGraph,"under 55", "<55", ">55");

The InferenceGraphNode assigns the node to the graph class you created; you also give it a name "under55" and then the names of the two outcomes, "<55" and

">55". These are the true/false states of the node. You’ll assign values to these shortly. To create the nodes you’re using a helper class called BayesnNetHelper, which has a createNode() method that does the work for you.

One node down, three to go; here is the Java code to represent the other three nodes:

Right now the nodes don’t know each other; there are no edges set to connect the nodes together. That’s covered in the next section.

Connecting the Nodes

Now that you have nodes, you can connect them. Remember from the yard example earlier in this chapter that certain nodes were parents of others. For this CSM model, the age node is the parent of the smoker node, which in turn is the parent of the surgical outcome node. Also, the duration node is the parent of the surgical outcome node.

In the code, you connect the nodes (called arcs in the syntax of the Java code, but still graph edges to me) with the create_arc() function. It takes the following syntax.

graph.create_arc(parent_node, child_node);

The following are your newly created InferenceGraphNodes; you can now create the arcs that connect them.

inferenceGraph.create_arc(age, smoker);

inferenceGraph.create_arc(smoker, surgical);

inferenceGraph.create_arc(duration, surgical);

If you were to draw the graph, you’d have something along the lines of what’s

Figure 4-9: The CSM graph

With the nodes connected, it’s time to turn your attention to the probabilities.

Assigning Probabilities

The BayesNetHelper class enables you to set the probability values while taking away the complexity. Start with the conditional probabilities. The smoker node is conditional with the age of the patient. In pseudocode, it would look like this:

if age < 55 then smoker(smokes) = 0.4, smoker(doesnotsmoke) = 0.6 if age > 55 then smoker(smokes) = 0.8, smoker(doesnotsmoke) = 0.2 In the Java class, you add the values with the helper class:

BayesNetHelper.setProbabilityValues(smoker, "<55", 0.4, 0.6);

BayesNetHelper.setProbabilityValues(smoker, ">55", 0.8, 0.2);

Next you set the probability values for the surgical outcome node; this one has two conditional probabilities connected to it, so you have to look at all the possible conditions and assign the probabilities accordingly:

if smokes and duration < 2Y then surgical(positive) = 0.1, surgical(negative) = 0.9 if smokes and duration > 2Y then surgical(positive) = 0.01, surgical(negative) = 0.99 if does not smoke and duration < 2Y then surgical(positive) = 0.8, surgical(negative) = 0.2

In the code, you again add the probabilities with the helper class. Notice this time you use parent and the secondary parent conditions.

BayesNetHelper.setProbabilityValues(surgical, "smokes", "<2Y",

The last two nodes are called leaf nodes because they don’t have any parents connected to them. All you have to do with these is add the true/

false values via the helper. You set one for the duration node and another for the age node.

BayesNetHelper.setProbabilityValues(duration, 0.9, 0.1);

BayesNetHelper.setProbabilityValues(age, 0.8, 0.2);

With nodes created, edges defi ned, and probabilities set, you can now start getting some results from the code. Make sure you have saved your work before running it.

Testing the Belief Network

To get the percentage belief of the network, you have to use the helper class again. It has a method called getBelief() and it takes the graph and the node for which you want to get the belief value. First off, look at the predicted prob-ability of a surgery’s success when you have no other conditions assigned to it:

double belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being positive: " + belief);

You will see the output in the console window:

The probability of surgery being positive: 0.44823999999999997

The model’s predicted probability of a surgery’s being successful is 44.8 percent; remember this calculation is made without any other conditions.

When you know more information about the patient, you’ll run the program again and see how the model’s predicted probability for a successful surgery changes for the patient. Remember that the model is based on the predicted probabilities based on the expert’s recommendation. There’s no assurance that the surgery is going to be successful; only the model’s predicted result.

The more times the surgery is performed should validate the model’s accu-racy in the long term.

Testing the Belief Network with a Condition

You now have some information about the patient so you can add this infor-mation to the program and see how the chances for a successful surgery look.

The age of the patient is less than 55. You can set the observation within the age node itself and then run the belief calculation again.

age.set_observation_value("<55");

belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being positive and patient is younger than 55 : " + belief);

Run the program again and the model’s prediction of the patient’s chances improves.

The probability of surgery being positive and patient is younger than 55 : 0.5032

If the patient smokes, it would have an impact on the result from the model.

Adding another observation to the node you now have the following to compute:

smoker.set_observation_value("smokes");

belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being positive for a smoker, younger than 55: " + belief);

Adding this last datum changes things drastically for the patient! While being younger than 55 is a good indicator for successful surgery, the fact he or she smokes causes the predicted probability from the network to decrease drastically.

The probability of surgery being positive for a smoker, younger than 55:

0.09100000000000001

The experts know that because of the location of the issue—the neck—surgery will affect the throat area, so there’s a high probability the surgery will fail;

many patients who desire surgery are refused on that basis alone.

Lastly add the duration the patient has had the symptoms. In this patient’s case it’s been more than two years.

duration.set_observation_value(">2Y");

belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being positive for a smoker, younger than 55 with symptoms over 2 years: " + belief);

Once again this decreases the chance of successful surgery.

The probability of surgery being positive for a smoker, younger than 55 with symptoms for over 2 years: 0.01

You’ve created a Bayesian Network to calculate the probability of successful surgery of CSM. With the conditional parameters, you see that the probability varies enormously, especially when the age and smoking factors are taken into account.

The following is the full code listing for the Bayes Network class created in this chapter. public BayesNetExample() {

InferenceGraph inferenceGraph = new InferenceGraph();

InferenceGraphNode age = BayesNetHelper.createNode(inferenceGraph,"

under55", "<55", ">55");

InferenceGraphNode smoker = BayesNetHelper.

createNode(inferenceGraph, "smoker", "smokes", "doesnotsmoke");

InferenceGraphNode duration = BayesNetHelper.

createNode(inferenceGraph, "duration", "<2Y", ">2Y");

InferenceGraphNode surgical = BayesNetHelper.

createNode(inferenceGraph, "surgicalOutcome", "positive", "negative");

inferenceGraph.create_arc(age, smoker);

inferenceGraph.create_arc(smoker, surgical);

inferenceGraph.create_arc(duration, surgical);

BayesNetHelper.setProbabilityValues(smoker, "<55", 0.4, 0.6);

BayesNetHelper.setProbabilityValues(smoker, ">55", 0.8, 0.2);

BayesNetHelper.setProbabilityValues(surgical, "smokes", "<2Y", 0.1, 0.9);

BayesNetHelper.setProbabilityValues(surgical, "smokes", ">2Y", 0.01, 0.99);

BayesNetHelper.setProbabilityValues(surgical, "doesnotsmoke",

"<2Y",0.8 , 0.2);

BayesNetHelper.setProbabilityValues(surgical, "doesnotsmoke",

">2Y", 0.58, 0.42);

BayesNetHelper.setProbabilityValues(duration, 0.9, 0.1);

BayesNetHelper.setProbabilityValues(age, 0.8, 0.2);

double belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probabilty of surgery being postive: " + belief);

age.set_observation_value("<55");

belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being postive and patient is younger than 55 : " + belief);

smoker.set_observation_value("smokes");

belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being postive for a smoker, younger than 55: " + belief);

duration.set_observation_value(">2Y");

belief = BayesNetHelper.getBelief(inferenceGraph, surgical);

System.out.println("The probability of surgery being postive for a smoker, younger than 55 with symptoms over 2 years: " + belief);

}

public static void main(String[] args) {

BayesNetExample bne = new BayesNetExample();

} }

N O T E The probabilities in the walkthrough are just base values used for demon-stration; they are not connected to any study or informed by any expert. They are merely there to illustrate how Bayesian Networks can be formulated.

Summar y

This chapter covered a lot of ground, including simple graph theory, probability, and Bayes’ Theorem. The Bayesian Network examples show that it’s straightfor-ward to create a network, create the nodes and connect them, and then assign probabilities and conditional probabilities. After you apply existing observations, your overall results change due to the nature of the graph.

Coding these things can be diffi cult and require some proper planning on paper. Wherever you can, try to enlist a domain expert to help with the initial values of the probabilities, because this will make your fi nal prediction output more accurat e.

There’s something about gathering knowledge about the human brain that makes people tick. Many people think that if we can mimic how the brain works, we’ll be able to make better decisions.

In this chapter, you look at how artifi cial neural networks work and how they are applied in the machine learning arena.

Dans le document Machine Learning (Page 107-117)

Documents relatifs