Skip to content

Decision Tree in Machine Learning Narender Kumar Spark By {Examples}

  • by

Decision trees are a fundamental tool in machine learning and data analysis. They are widely used for classification, regression, and decision-making tasks. Decision trees provide an intuitive and transparent way to model complex relationships between variables and make predictions based on availability. In this blog, we will provide an in-depth introduction to decision trees.

Whether you are new to the field of machine learning or an experienced data scientist, this blog will provide valuable insights into decision trees and how they can be used to make informed decisions. So, let’s dive in and explore the world of decision trees!

1. What is Decision Tree?

A decision tree is a machine-learning algorithm that is widely used in data mining and classification. It is a tree-like model that displays all possible solutions to a decision based on certain conditions in a graphical format.

The decision tree algorithm works by dividing the data into subsets based on the values of different attributes and selecting the attribute that provides the best split. This is done by calculating a metric called the information gain or gain ratio, which measures how much the decision tree reduces the uncertainty or entropy in the data. The algorithm then creates a tree structure that displays the different decisions and their possible consequences.

At each node in the tree, a decision is made based on a feature or attribute of the data, and the path to the next node is determined by the outcome of that decision. This process continues until a final decision is reached, which is represented by a leaf node in the tree.

2. Why use Decision Trees?

Decision trees are a popular machine learning algorithm used for both classification and regression tasks. Here are some reasons why decision trees are often used:

Intuitive and easy to interpret: Decision trees provide a simple way to model complex relationships and make predictions based on available data, making them an ideal choice for decision-making applications. They can also be visualized graphically, which makes them easy to interpret and explain to others.

Versatile: Decision trees can be used for both classification and regression tasks, which makes them a valuable tool in machine learning. They can be applied to a wide range of problems, including those in healthcare, finance, and marketing.

Non-parametric: Decision trees are a non-parametric method, meaning they do not make assumptions about data distribution, which makes them suitable for use with a wide range of data types and distributions. This is in contrast to parametric methods, which assume a specific data distribution.

Robust: Decision trees are robust to outliers and noise in the data, and can handle missing values without the need for imputation. This means they can still produce accurate predictions even when data is not perfectly clean.

Scalable: Decision trees can easily be scaled up to handle large datasets and complex problems. There are also variations of decision trees, such as random forests and boosting, which can further improve their performance and scalability.

3. Decision Tree Terminologies

Here are some commonly used decision tree terminologies:

Root Node: The topmost node of a decision tree, which represents the initial input data and from which all other nodes are derived.

Internal Node: A node in a decision tree that represents a decision based on a feature or attribute.

Leaf Node: A node in a decision tree that represents a prediction or classification.

Branch: A line connecting nodes in a decision tree that represents a decision rule or path.

Split: The process of dividing a node into sub-nodes based on a feature or attribute.

Pruning: The process of reducing the size of a decision tree by removing branches or sub-trees that do not improve the model’s performance.

Entropy: A measure of impurity or randomness in a dataset that is used to calculate the information gain of each split in a decision tree.

Information Gain: A measure of the reduction in entropy achieved by a particular split in a decision tree.

Gini Impurity: A measure of the probability of misclassifying a randomly chosen element from a dataset, which is used to calculate the information gain of each split in a decision tree.

CART: Classification and Regression Tree, a popular decision tree algorithm that can be used for both classification and regression tasks.

4. How does the Decision Tree algorithm Work?

Detailed explanation of how the Decision Tree algorithm works:

Data Preparation: The first step in building a decision tree model is to prepare the data. This includes dividing the data into training and testing sets, and preprocessing the data by converting categorical variables into numerical values, scaling the data, etc.

Building the Tree: Once the data is prepared, we can build the decision tree by recursively splitting the data into subsets based on the values of the features. At each split, we choose the feature that provides the highest information gain or Gini impurity reduction. This means that we choose the feature that separates the data into the most distinct classes or groups.

Pruning the Tree: After the decision tree is built, we can prune it to improve its performance and reduce overfitting. This involves removing branches or sub-trees that do not significantly improve the model’s accuracy.

Making Predictions: Finally, we can use the decision tree to make predictions on new data. We do this by starting at the root node and following the path down the tree based on the values of the features in the new data. When we reach a leaf node, we output the predicted value or class.

5. Attribute Selection Measures in the Decision Tree

5.1 Information gain

Information gain is defined as the difference between the entropy of the parent node and the weighted average of the entropies of the child nodes. It measures the amount of information gained by splitting the data based on a particular attribute.

#Equation:
IG(T,A)=H(T)−H(T∣A)

where T is the target variable, A is the attribute being considered, H(T) is the entropy of the target variable, and H(T|A) is the conditional entropy of the target variable given the attribute.

5.2 Gini index

Gini index is based on the concept of impurity and measures the probability of misclassifying a randomly chosen element in the dataset. It is defined as the sum of the squared probabilities of each class minus one.

#Equation:
Gini Index= 1- ∑jPj2

where T is the target variable, k is the number of classes, and p is the proportion of instances belonging to class i.

6. Pruning in Decision Tree

Pruning is a technique used to reduce the complexity of decision trees by removing branches that are unlikely to improve the accuracy of the tree on unseen data. Pruning can help prevent overfitting, where the tree becomes too complex and fits the training data too closely, leading to poor generalization to new data.

There are two main types of pruning:

Pre-pruning: This involves setting a limit on the depth of the tree, the minimum number of instances required in a leaf, or the minimum information gain required for a split. Pre-pruning is often used when the dataset is large or noisy, and when building a large tree would be computationally expensive or lead to overfitting.

Post-pruning: This involves building the full decision tree and then removing branches that do not improve the accuracy of the tree on a validation set. Post-pruning is often used when the dataset is small or clean, and when building a large tree is not computationally expensive.

7. Python Implementation of Decision Tree

Data Set Link: https://github.com/Narenderbeniwal/Spark-By-Example

# Import necessary libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt

# Load the dataset
data = pd.read_csv(“diabetes.csv”)

# Split the dataset into features and target variable
X = data.drop(‘Outcome’, axis=1)
y = data[‘Outcome’]

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)

# Fit a decision tree classifier on the training set
classifier = DecisionTreeClassifier(max_depth=3)
classifier.fit(X_train, y_train)

# Predict the target variable for testing set
y_pred = classifier.predict(X_test)

# Evaluate the accuracy of the model
accuracy = metrics.accuracy_score(y_test, y_pred)
print(“Accuracy:”, accuracy)

# Create a confusion matrix to visualize the performance of the model
confusion_matrix = pd.crosstab(y_test, y_pred, rownames=[‘Actual’], colnames=[‘Predicted’])
print(confusion_matrix)

# Visualize the decision tree
plt.figure(figsize=(20,10))
plot_tree(classifier, filled=True, feature_names=X.columns, class_names=[‘0′,’1’])
plt.show()

In this example, we load the diabetes dataset from a CSV file, split it into features (X) and target variable (y), and then split the data into training and testing sets. We then fit a decision tree classifier on the training set, predict the target variable for the testing set, and evaluate the accuracy of the model using the accuracy score from the metrics module. Finally, we create a confusion matrix to visualize the performance of the model, and plot the decision tree using the plot_tree function from the tree module and matplotlib.pyplot.

# Output:
Accuracy: 0.7662337662337663

Predicted 0 1
Actual
0 138 14
1 42 37

This shows the confusion matrix, which displays the number of true positives, false positives, true negatives, and false negatives, along with the accuracy score.

And here is an example visualization of the decision tree:

Example visualization of the decision tree

8. Conclusion

In conclusion, decision trees are a powerful and widely used machine learning algorithm for both classification and regression tasks. They are intuitive, easy to interpret, and can handle a wide range of data types and distributions without making assumptions about data distribution. Decision trees are also robust to outliers and noise in the data, and can handle missing values without the need for imputation.

 Decision trees are a fundamental tool in machine learning and data analysis. They are widely used for classification, regression, and decision-making tasks. Decision trees provide an intuitive and transparent way to model complex relationships between variables and make predictions based on availability. In this blog, we will provide an in-depth introduction to decision trees. Whether  Read More Machine Learning 

Leave a Reply

Your email address will not be published. Required fields are marked *