Decision Tree Classifier Basics
Lesson Introduction
Welcome! Today, we are going to learn about the Decision Tree Classifier. It's one of the basic tools in machine learning that helps us make decisions like a flowchart. Imagine deciding whether to wear a coat. If it's cold, you wear it; if not, you don't. This is similar to how a Decision Tree works in predicting outcomes based on given data.
By the end of this lesson, you will know:
- How to train a
Decision Tree Classifierto make predictions. - The concept and learning process of a decision tree.
- General parameters of a decision tree.
Let's start by looking at each of these steps one by one.
Loading and Splitting a Dataset
In machine learning, data is very important. We will use the wine dataset from Scikit-Learn. As a reminder, this dataset has measurements of different wines, and our goal is to predict the class of wine.
Here's a quick reminder on how to load and split this dataset:
Concept of a Decision Tree
A Decision Tree is a type of supervised learning model used for classification and regression tasks. It is a flowchart-like structure where:
- Root node represents one feature of the data.
- Internal nodes represent features (or attributes) of the data.
- Branches represent the decision rules.
- Leaf nodes represent the outcome.
Here is the example:
Imagine a simple decision tree for classifying whether an animal is a mammal.
-
Root Node: Start with a feature, such as whether the animal has fur.
- If yes, go to the next node.
- If no, the animal is not a mammal.
-
First Decision Node: If the animal has fur, check if it gives birth.
- If yes, the animal is a mammal.
- If no, the animal is not a mammal.
This decision-making process can be visualized as follows:

The Training Algorithm
A decision tree is trained through a process called recursive partitioning, which involves the following steps:
- Select the Best Feature: At each node, the algorithm evaluates all available features to determine which one best splits the data. This is typically done by calculating a metric such as information gain, Gini impurity, or entropy. The feature that provides the best split (i.e., maximizes information gain or minimizes impurity) is selected for that node.
- Split the Data: Once the best feature is identified, the dataset is split into subsets based on that feature's unique values or ranges. For instance, if the chosen feature is "has fur" with possible values "yes" or "no," the data is split into two subsets: one subset where "has fur" is "yes" and another where it is "no." This creates branches in the tree, leading to further splits and decision nodes.
- Repeat: This process is repeated recursively for each subset, creating new nodes, until a stopping criterion is met (such as maximum depth or minimum number of samples per node).
- Assign Outputs: Leaf nodes are assigned an output value (class label for classification tasks).
