Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Decision trees are one of the simpler machine-learning methods. They are a completely transparent method of classifying observations, which, after training, look like a series of if-then statements arranged into a tree. Figure 7-1 shows an example of a decision tree for classifying fruit.
Once you have a decision tree, it’s quite easy to see how it makes all of its decisions. Just follow the path down the tree that answers each question correctly and you’ll eventually arrive at an answer. Tracing back from the node where you ended up gives a rationale for the final classification.
This chapter will look at a way to represent a decision tree, at
code for constructing the tree from real data, and at code for
classifying new observations. The first step is to create a
representation of a tree. Create a new class called decisionnode, which represents each node in
the tree: