Learn Machine Learning and AI with Python Easily-DAY_01
Decision Trees: A Powerful Machine Learning Algorithm:
What is a Decision Tree?
A decision tree is a widely used machine learning algorithm that can be used for both classification and regression tasks. It is a type of supervised learning algorithm that is based on a tree-like structure, where:
Each internal node represents a feature or attribute
Each branch represents a decision
Each leaf node represents the outcome or prediction
How do Decision Trees Work?
Decision tree algorithms work by recursively partitioning the data into subsets based on the feature that provides the most information gain. This process is known as the splitting process.
Advantages of Decision Trees:
One of the key advantages of decision trees is that they are easy to interpret and understand.
Preventing Overfitting:
To prevent overfitting, techniques such as pruning or ensemble methods can be used. Ensemble methods combine multiple decision trees to produce a more robust model. Examples of ensemble methods include:
Classification Boundaries and Decision Trees
Classification Boundaries:
A classification boundary is defined as the point where the probabilities of being in class 1 and class 0 are equal.
This occurs when P(Y=1) = P(Y=0) = 1/2, which is equivalent to when the log-odds are 0.
The equation for the classification boundary can be expressed as x × β = 0, where β is a vector of parameters and x are the feature values.
This equation defines a line or hyperplane that separates the classes.
Limitations of Linear Classification Boundaries:
Linear classification boundaries may not be sufficient to capture complex relationships between features and classes.
Non-linear decision boundaries can be difficult to interpret and may not be easily described by a single equation.
Decision Trees:
Decision trees are a type of machine learning model that can handle complex decision boundaries and are easy to interpret.
Decision trees are based on a flowchart-like approach to decision-making, where each node represents a decision or test.
The tree is traversed from the root node to the leaf nodes, where the final prediction is made.
Properties of Decision Trees:
Decision trees can handle complex decision boundaries that are not easily described by a single equation.
Decision trees are interpretable by humans, with each node representing a simple decision or test.
Decision trees have locally linear decision boundaries, making them easy to understand and visualize.
Example of a Decision Tree
The tree is constructed by recursively partitioning the feature space into regions, where each region is defined by a simple decision or test.
The final prediction is made by traversing the tree from the root node to the leaf node, where the majority class is predicted.
Predicting with a Decision Tree:
To make a prediction using a decision tree, we start at the root node and traverse the tree based on the feature values of the instance.
At each node, we apply the decision or test, and move to the next node until we reach a leaf node.
The prediction is made by taking the majority class of the leaf node.
Splitting Criteria for Decision Trees:
Introduction
When building a decision tree, we need to decide how to split the data at each node.
There are several splitting criteria that can be used, each with its own strengths and weaknesses.
Classification Error:
Classification error measures the degree of purity of a region.
It is defined as the number of errors divided by the total number of points in the region.
The goal is to minimize the classification error at each split.
Classification error measures the degree of purity of a region. It is defined as the number of errors divided by the total number of points in the region. The goal is to minimize the classification error at each split.
Mathematically, it can be represented as:
Classification Error = 1 - (maximum proportion of a class in the region)
For example, if we have two classes, blue circles and orange triangles, and we split the region into two sub-regions, R1 and R2. Let's say R1 has 6 blue circles and no orange triangles, and R2 has 5 blue circles and 8 orange triangles.
The classification error for R1 would be 0, since it's a pure region with only one class. The classification error for R2 would be 1 - (8/13) = 0.38, since the majority class is orange triangles.
Gini Index:
The Gini index is another way of measuring purity.
It is defined as 1 minus the sum of the squared proportions of each class.
The Gini index is a soft way of finding the maximum proportion, and it is differentiable.
For example, if we have two classes, blue circles and orange triangles, and we split the node into two sub-nodes, R1 and R2. Let's say R1 has 6 blue circles and no orange triangles, and R2 has 5 blue circles and 8 orange triangles.
The Gini Index for R1 would be 0, since it's a pure node with only one class. The Gini Index for R2 would be approximately 0.47, calculated as:
Gini Index = 1 - (5/13)^2 - (8/13)^2
Entropy:
Entropy is a measure of the uncertainty or impurity of a region.
It is defined as the negative sum of the proportions of each class times the log of the proportion.
Entropy is a measure of the strength of the signal in a region.
Mathematically, it can be represented as:
Entropy = - ∑ (p * log(p))
where p is the proportion of each class in the region.
For example, if we have two classes, blue circles and orange triangles, and we split the region into two sub-regions, R1 and R2. Let's say R1 has 6 blue circles and no orange triangles, and R2 has 5 blue circles and 8 orange triangles.
The entropy for R1 would be 0, since it's a pure region with only one class. The entropy for R2 would be approximately 1.38, calculated as:
Entropy = - (5/13 log(5/13) + 8/13 log(8/13))
Comparison of Splitting Criteria
Classification error is not differentiable, so it is not commonly used.
Gini index and entropy are both differentiable and can be used to optimize the split.
Entropy penalizes impurity more than Gini index, which penalizes it more than classification error.
Greedy Algorithm:
The optimal decision tree is NP-complete, so we use a greedy algorithm to approximate it.
The algorithm starts with an empty decision tree and recursively splits the data until a stopping condition is met.
At each node, the algorithm chooses the optimal predictor and threshold to split the data.
Here's a general outline of the Greedy Algorithm:
Initialize the solution.
While the problem is not solved: a. Make a locally optimal choice. b. Update the solution.
Return the solution.
Some examples of problems that can be solved using the Greedy Algorithm include:
Fractional Knapsack Problem: Given a set of items, each with a weight and a value, determine the subset of items to include in a knapsack of limited capacity to maximize the total value.
Activity Selection Problem: Given a set of activities, each with a start and finish time, select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
Stopping Conditions:
Stopping conditions are used to determine when to stop splitting the data.
Common stopping conditions include a maximum depth, a minimum number of samples, or a minimum impurity decrease.
Subscribe to my newsletter
Read articles from ABHISHEK SINGH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by