×

Decision Tree Algorithm in Machine Learning

We are very familiar with the word Machine Learning nowadays. Machine Learning technologies are used everywhere. Many IT companies use this type of technologies to improve their product. Decision tree algorithm is one of the important algorithms used in Machine Learning models. It can be used in both classification and regression techniques. In this article, we are going to explore more about decision tree algorithm.

What is Decision tree algorithm?

The basic structure of a tree is made by nodes and branches. In this decision tree there will be root node, leave nodes and branches. It is basically a graphical representation of all the possible solutions available for a given problem depending upon conditions. This is mainly supervised learning. It can be used in both classification and regression. But it is mostly used in classification problems. In the tree, the internal nodes represent the features of data set and the branches of tree represents the decision rules and the leave nodes represent the outputs. We can use Classification and Regression Tree Algorithm in order to build the decision tree.

Example:

Let’s understand the concept of decision tree by a simple and easy example. Suppose one candidate has got an offer letter and previously he also got another one offer letter for job. Now he wants to decide his company to job. Here we can use the decision tree. So, first we start with root node. In this root node we check whether the salary is thirty thousand and above or not. If the branch says yes then we go to next node and if the branch goes for no then we can say that this job is declined. Next node can check the location. In this way you can check the features of data set and classify the input data.

Terminologies used in Decision tree algorithm

  1. Branch/Sub Tree: There exists many sub trees in a decision tree. This sub trees form the main decision tree.
  2. Leaf Node: From the word leaf, we can understand that this is the ending of our decision tree. It mainly represents the final output of the decision tree.
  3. Parent/Child Node: We have previously discussed about sub tree. In this sub tree a node come from another node by branch. This next node is called child and previous node is named parent node.
  4. Pruning: It is a process by which we can minimize the branches of a tree.
  5. Root Node: It is the node of the tree from where the tree begins. It represents the whole data set. It is divided into subtrees.
  6. Splitting: It is the process by which we can divide the node into sub nodes.

Working process of Decision tree algorithm

Now, we are going to understand the working process of decision tree algorithm. In this decision tree algorithm, the input data set is taken. After that the attributes of input data is compared with the existing data set and then the branch is chosen. After that we go to next node. In next node, the same process is followed again. These things repeat until we get leaf node means our output. You can understand the above process by this algorithm mentioned below:

  • Step-1: We will begin the tree by initializing the root node. This root node will represent the whole data set.
  • Step-2: After that we will find the best attribute from the data set. We will use attribute selection measure for this thing.
  • Step-3: In next step we will divide the whole data set into subsets which can possess best possible attributes.
  • Step-4: After that we will create the node which have best attribute in the decision tree.
  • Step-5: We will do this process recursively by dividing the data set into subsets and creating new decision tree nodes. This process will continue till we get the best possible result. When we cannot go further in this recursion we will declare that node as leaf node. This leaf node will be our output result.

Python Implementation of Decision Tree Algorithm

Now, we are going to see how the decision tree algorithm is implemented in Python language. We will use user_data.csv file for this implementation. Follow the steps given below to implement the decision tree algorithm.

  1. Data Pre-processing step.
  2. Fitting a Decision-Tree algorithm to the Training set.
  3. Predicting the test result.
  4. Test accuracy of the result (Creation of Confusion matrix).
  5. Visualizing the training set result.
  6. Visualizing the test set result.

1. Data pre-processing step

Below is the code for its implementation

1.	import numpy as nm  
2.	import matplotlib.pyplot as mtp  
3.	import pandas as pd  
4.	data_set= pd.read_csv('user_data.csv')  
5.	  
6.	x= data_set.iloc[:, [2,3]].values  
7.	y= data_set.iloc[:, 4].values  
8.	  
9.	from sklearn.model_selection import train_test_split  
10.	x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)  
11.	  
12.	from sklearn.preprocessing import StandardScaler    
13.	st_x= StandardScaler()  
14.	x_train= st_x.fit_transform(x_train)    
15.	x_test= st_x.transform(x_test)    

2. Fitting a decision tree algorithm to the training set

Now, we will fit the decision tree algorithm in the training set. For this reason, we have to import DecisionTreeClassifier class from sklearn.tree library. Below is the code for its implementation:

1.	From sklearn.tree import DecisionTreeClassifier  
2.	classifier= DecisionTreeClassifier(criterion='entropy', random_state=0)  
3.	classifier.fit(x_train, y_train) 

3. Predicting the test result

Now, we will predict the test result. Below is the code for its implementation: 

1. y_pred= classifier.predict(x_test)  

4. Checking the test accuracy of the result

Now, we will check whether the test result is correct or not. Below is the code for its implementation:

1. from sklearn.metrics import confusion_matrix  
2. cm= confusion_matrix(y_test, y_pred)  

5. Visualization of training set result

Now, we will visualise the result of training set. For this reason, we will use one graph. Below is the code for its implementation:

1.	from matplotlib.colors import ListedColormap  
2.	x_set, y_set = x_train, y_train  
3.	x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step  =0.01),  
4.	nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))  
5.	mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),  
6.	alpha = 0.75, cmap = ListedColormap(('purple','green' )))  
7.	mtp.xlim(x1.min(), x1.max())  
8.	mtp.ylim(x2.min(), x2.max())  
9.	fori, j in enumerate(nm.unique(y_set)):  
10.	mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],  
11.	        c = ListedColormap(('purple', 'green'))(i), label = j)  
12.	mtp.title('Decision Tree Algorithm (Training set)')  
13.	mtp.xlabel('Age')  
14.	mtp.ylabel('Estimated Salary')  
15.	mtp.legend()  
16.	mtp.show()  

6. Visualization of test set result

For visualization of test set result, we just need to implement the same code which we used in the implementation of training set result. Here, the only change is that the training set will be implemented by test set. Below is the code for its implementation:

1.	from matplotlib.colors import ListedColormap  
2.	x_set, y_set = x_test, y_test  
3.	x1, x2 = nm.meshgrid (nm.arange (start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step  = 0.01 ),  
4.	nm.arange ( start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01 ) )  
5.	mtp.contourf ( x1, x2,  classifier.predict ( nm.array ( [ x1.ravel(), x2.ravel() ] ).T ).reshape (x1.shape) ,  
6.	alpha = 0.75, cmap = ListedColormap ( ( 'purple', 'green'  ) ) )  
7.	mtp.xlim ( x1.min(), x1.max() )  
8.	mtp.ylim( x2.min(), x2.max() )  
9.	fori, j in enumerate ( nm.unique( y_set ) ):  
10.	mtp.scatter (x_set [y_set == j, 0], x_set [y_set == j, 1],  
11.	        c = ListedColormap(('purple', 'green'))(i), label = j)  
12.	mtp.title('Decision Tree Algorithm(Test set)')  
13.	mtp.xlabel('Age')  
14.	mtp.ylabel('Estimated Salary')  
15.	mtp.legend()  
16.	mtp.show()  

Advantages of the Decision Tree

  1. This decision tree algorithm can help you solving the problems related to decision making.
  2. In this algorithm, we can check all possible results.
  3. The working process of this algorithm is more or less same as the working process of our brain while decision making. So, human can understand the logic easily.
  4. Here, in this algorithm, we need less cleaning of data.

Disadvantages of the Decision Tree

  1. If we have many class labels then the decision tree may become very complex.
  2. The decision tree algorithm may face over fitting issues. To solve this problem, we can use random forest algorithm.
  3. As we have seen in previous examples and implementations, the decision tree has many layers. So, it can be a little bit complex.

Related Topics

Heart Disease Prediction Using Machine Learning

The world uses machine learning in many different fields. This is also true in the healthcare sector. Machine learning may be crucial in determining if locomotor disorders, heart illnesses, and...

12 minutes read.

Kaggle Machine Learning Project

What is Kaggle? Data scientists and machine learning enthusiasts connect online at Kaggle. Users of Kaggle can work together, access and share datasets, use notebooks with GPU integration, and compete with...

6 minutes read.

Random Forest Algorithm for Machine Learning

Introduction to Random Forest Random forest is an ensemble-based supervised learning model. The concept of random forest is used in both classifications as well as in the regression problems. Basically, in...

7 minutes read.

Machine Learning IDE

IDE (Integrated Development Environment) is a software that is used for the development of software. It usually compiled up of common development tools such as source code editor, compiler, and...

6 minutes read.

Machine Learning Projects for the Final Year Students

We live in a technologically advanced age where machines and various technologies are all around us. Machine learning is a method for teaching computers to think and learn. In the...

7 minutes read.

Data Preprocessing in Machine Learning

Before starting a machine learning project, data is an essential thing needed before starting a project. The data used in ML projects is in CSV (Comma Separated Value) format. It...

9 minutes read.

Diabetes Prediction using Machine Learning

Diabetes Mellitus (shortly known as Diabetes) is one of the fastest-growing diseases. Nowadays, many people are affected with diabetes for many reasons, irrespective of age. Recently, many people who belong...

6 minutes read.

Hierarchical Clustering Algorithm

Introduction to Hierarchical Clustering The other unsupervised learning-based algorithm used to assemble unlabeled samples based on some similarity is the Hierarchical Clustering. There are two types of hierarchical clustering algorithm: 1. Agglomerative Hierarchical Clustering...

7 minutes read.

Chi-Square Test in Machine Learning

A statistical technique called the chi-square test is used to compare actual outcomes to predictions. This test aims to determine if a discrepancy between actual and projected data is caused...

6 minutes read.

Feature Selection in Machine Learning

Feature selection Feature selection is the methodology of selecting some particular dataset instead of all the datasets, which is relevant to reducing the noise of machine learning models. In the machine...

3 minutes read.

Association Rule Learning Algorithm

Introduction to Association Rule Learning Association rule learning extracts alliances among the datapoints in a huge dataset. It incorporates the concept of data mining, which helps in finding useful commercial associations or regularities between the...

3 minutes read.

Top 10 Books on Machine Learning

If you are looking to explore some new domains in engineering field then you may like ML or Machine Learning. The popularity of Machine Learning is increasing gradually day by...

5 minutes read.

KNN algorithm in Machine Learning

Machine Learning is one of the most used modern technologies in our world. Machine Learning helps human a lot to do their jobs at ease. Today almost every big tech...

7 minutes read.

Python Anaconda setup

Python programming language is used in this tutorial to get hands-on machine learning. A compatible IDE (Integrated Development Environment) is needed to be installed on the computer system before using...

3 minutes read.

PCA in Machine Learning

Machine Learning is one of the most used modern technologies in our world. Machine Learning helps human a lot to do their jobs at ease. PCA is widely used Machine...

3 minutes read.

Perceptron in Machine Learning

Machine Learning is becoming a very important part of the technology industry day by day. Perceptron is one of the key things of Machine Learning. Mr. Frank Rosenblatt invented this...

3 minutes read.

Bias and Variances in Machine Learning

Machine Learning is an important part in many industries. Machine Learning mainly works on predicting things depending on given input. Machine Learning models are trained over a sample data set....

4 minutes read.

Feature Extraction in Machine Learning

With the advancement of technology in the databases, everyone can store a vast amount of data with hundreds of thousands of features these days. Features contain information about the dependent...

4 minutes read.

Basics Vectors in Linear Algebra in ML

First, to learn Machine Learning sincerely, we must know about vectors in Linear Algebra. The principle of Linear Algebra is very much important here. Linear Algebra is the study of...

3 minutes read.

Applications of Machine Learning

If you have connection with technical world then you have must heard about Machine Learning. It is one of the modern technologies. Machine Learning is the future of our tech...

6 minutes read.