Docsity

Introduction To Decision Trees, Study notes of Machine Learning

Toggle sidebar
bg1
Decision trees are among the simplest simple machine learning algorithms, and they can be employed
for both classification and regression. After viewing, you'll be able to construct a decision tree classifier
from scratch.
The following is how the article is organised:
An Overview of Decision Trees
The Science of Decision Trees
Crash Course Recursion
Complete Implementation
Conclusion
An Overview of Decision Trees
Decision trees are a non-parametric model that can be used for regression and classification. The from-
scratch implementation will take some time to grasp, but the algorithm's logic is straightforward.
Decision trees are made up of only two components: nodes and branches. In a moment, we'll go over
the various types of nodes. If you decide to follow along, the term recursion should not feel foreign to
you because the algorithm is based on it.
There are various types of nodes:
The root node is the node at the top of the tree. It has a feature that best divides the data (a single
feature that alone classifies the target variable most accurately)
Decision nodes are nodes that evaluate variables. There are arrows pointing to and away from these
nodes.
Leaf nodes are the final nodes where the prediction is made.
Depending on the size of the dataset (both in rows and columns), there are probably thousands to
millions of different ways to arrange the nodes and their conditions.
So, how do we locate the root node?
pf3
pf4

Partial preview of the text

Download Introduction To Decision Trees and more Study notes Machine Learning in PDF only on Docsity!

Decision trees are among the simplest simple machine learning algorithms, and they can be employed for both classification and regression. After viewing, you'll be able to construct a decision tree classifier from scratch. The following is how the article is organised: An Overview of Decision Trees The Science of Decision Trees Crash Course Recursion Complete Implementation Conclusion An Overview of Decision Trees Decision trees are a non-parametric model that can be used for regression and classification. The from- scratch implementation will take some time to grasp, but the algorithm's logic is straightforward. Decision trees are made up of only two components: nodes and branches. In a moment, we'll go over the various types of nodes. If you decide to follow along, the term recursion should not feel foreign to you because the algorithm is based on it. There are various types of nodes: The root node is the node at the top of the tree. It has a feature that best divides the data (a single feature that alone classifies the target variable most accurately) Decision nodes are nodes that evaluate variables. There are arrows pointing to and away from these nodes. Leaf nodes are the final nodes where the prediction is made. Depending on the size of the dataset (both in rows and columns), there are probably thousands to millions of different ways to arrange the nodes and their conditions. So, how do we locate the root node?

How to Find the Root Node In a nutshell, we must examine how each input feature independently classifies the target variable. If none of the features is completely correct in the classification, we can consider them impure. We can use the Entropy metric to determine which of the impure features is the most pure. We'll go over the formula and calculations later, but keep in mind that the entropy value ranges from 0 (best) to

  1. (Worst). The variable with the least entropy is then selected as the root node. The training procedure To start training the decision tree classifier, we must first identify the root node. That aspect has already been addressed. The information gain metric is then calculated for each and every split. Simply put, it is the average of all entropy values based on a specific split. We'll go over the formula and calculations later, but keep in mind that the greater the gain, the better the decision split. The algorithm then goes through all of the input features and their unique values, calculates information gain for each combination, and saves the best split feature and threshold for each node. The tree is constructed recursively in this manner. Because the recursion process could go on indefinitely, we'll need to specify some exit conditions manually. The most common ones are maximum depth and minimum samples at the node. Both will be addressed after implementation. The Science of Decision Trees Decision trees are a coding challenge rather than a mathematical one. For the learning part, you'll only need to use two formulas: entropy and information gain. Let us begin with entropy. As previously stated, it measures the purity of a split at the node level. Its value ranges from 0 to 1 (pure) (impure). Here is the entropy formula:

E=−∑i= 1 Npilog 2 pi

The _build(X, y, depth) function builds a decision tree recursively until the stopping criteria is met (hyper parameters in the constructor) The fit(X, y) function invokes the _build () function, which returns the constructed tree to the function Object () { [native code] }. The _predict(x) function searches the tree for a single instance to classify. Predict(X) calls the _predict () function on each instance in matrix X. There's no denying it's a lot. Take your time understanding each line of the code below. It's well- documented, so the following comments should help: You are not expected to comprehend every line of code in a single sitting. Give it some time, go through the code line by line, and try to figure out why things work. It's not that difficult once you understand the algorithm's basic intuition. Conclusion: Naturally, it will take you at least a couple of readings to fully comprehend the subject. Please feel free to look into additional resources as they will help you understand more. You now understand how to build the Decision tree classifier algorithm from the ground up. Is this to say you should abandon the de facto standard machine learning libraries? No way, no how. Let me explain. You now understand how to build the Decision tree classifier algorithm from the ground up. Is this to say you should abandon the de facto standard machine learning libraries? No way, no how. Let me explain. You should not write something from scratch just because you can. Nonetheless, knowing every detail of how algorithms work is a valuable skill that can set you apart from every other fit and predict data scientist. Thank you for reading.