1. Classification-Tree Learning
Welcome back! In this video, you'll examine how a classification-tree learns from data.
2. Building Blocks of a Decision-Tree
Let's first start by defining some terms.
A decision-tree is a data-structure consisting of a hierarchy of individual units called nodes.
A node is a point that involves either a question or a prediction.
3. Building Blocks of a Decision-Tree
The root is the node at which the decision-tree starts growing. It has no parent node and involves a question that gives rise to 2 children nodes through two branches.
An internal node is a node that has a parent. It also involves a question that gives rise to 2 children nodes.
Finally, a node that has no children is called a leaf. A leaf has one parent node and involves no questions. It's where a prediction is made.
Recall that when a classification tree is trained on a labeled dataset, the tree learns patterns from the features in such a way to produce the purest leafs.
In other words the tree is trained in such a way so that, in each leaf, one class-label is predominant.
4. Prediction
In the tree diagram shown here, consider the case where an instance traverses the tree to reach the leaf on the left.
In this leaf, there are 257 instances classified as benign and 7 instances classified as malignant. As a result, the tree's prediction for this instance would be: 'benign'.
In order to understand how a classification tree produces the purest leafs possible, let's first define the concept of information gain.
5. Information Gain (IG)
The nodes of a classification tree are grown recursively; in other words, the obtention of an internal node or a leaf depends on the state of its predecessors.
To produce the purest leafs possible, at each node, a tree asks a question involving one feature f and a split-point sp.
But how does it know which feature and which split-point to pick? It does so by maximizing Information gain!
The tree considers that every node contains information and aims at maximizing the Information Gain obtained after each split.
Consider the case where a node with N samples is split into a left-node with Nleft samples and a right-node with Nright samples.
6. Information Gain (IG)
The information gain for such split is given by the formula shown here.
A question that you may have in mind here is: 'What criterion is used to measure the impurity of a node?'
Well, there are different criteria you can use among which are the gini-index and entropy. Now that you know what is Information gain, let's describe how a classification tree learns.
7. Classification-Tree Learning
When an unconstrained tree is trained, the nodes are grown recursively. In other words, a node exists based on the state of its predecessors.
At a non-leaf node, the data is split based on feature f and split-point sp in such a way to maximize information gain.
If the information gain obtained by splitting a node is null, the node is declared a leaf.
Keep in mind that these rules are for unconstrained trees. If you constrain the maximum depth of a tree to 2 for example, all nodes having a depth of 2 will be declared leafs even if the information gain obtained by splitting such nodes is not null.
8. Information Criterion in scikit-learn (Breast Cancer dataset)
Revisiting the 2D breast-cancer dataset from the previous lesson, you can set the information criterion of dt to the gini-index by setting the criterion parameter to 'gini' as shown on the last line here.
9. Information Criterion in scikit-learn
Now fit dt to the training set and predict the test set labels. Then determine dt's test set accuracy which evaluates to about 92%.
10. Let's practice!
Now it's your turn to practice.