Travel insurance — Decision tree classifier

using the decision tree classifier model we will make a model that will predict whether a person will buy travel insurance or not (with source code)

As in the previous article, I have given you an introduction to the decision tree model now in this article, I will tell you how to make a decision tree classifier model with some lines of codes.

if you want to know about the decision tree model click on this link-

so let’s start

the first step is we need to download the dataset and then apply the dataset to the model. you can download or copy data from the URL —

Importing the libraries

now we will import pandas as given below if your system has not installed these libraries you can download them by pip command.

Data preparation

now we will read the data by pandas and store it in a variable named data so we don’t need to call it again and again, by the head command we can see the first 5 elements of the data if you wish to see more you can enter the number inside the bracket.

now as some data columns are strings(words) so first we have to convert them into integers and we will do it by encoding techniques by using a label encoder

so we have to encode 4 columns (Employment Type, Graduate Or Not, Frequent Flyer, and Ever Travelled Abroad)

as you can see in our new dataset the values are changed to

Employment Type : Government Sector = 0 and Private Sector/Self Employed = 1

Graduate or not : Yes = 1 and no = 1

Frequent Flyer : Yes = 1 and no = 1

Ever travelled abroad : Yes = 1 and no = 1

and now I have created a list in which I think it will be the deciding factor(Age, Employment type, Graduate or not, Annual income, Family members, Chronic diseases, Frequent flyer, Ever travelled abroad) for a person to buy insurance or not and assign it to variable features now I will pass features in the dataset and store it as x and the Travel insurance whether a person has bought or not as y

Making the model

Splitting the model

first, we have to import the test train split from sklearn model selection then we will split our model dataset into a train and test dataset


here we will be making a decision tree classifier because we have to classify our values into 0 or 1 for making that we have to import the decision tree classifier from the sklearn tree and then fit our train data into the model to train the model

and finally, our model is ready now we are all set to predict from our model


nowhere I have made a variable named prediction which will predict the values of our test data

we also can predict entered values simply by adding the values to the list manually

here I have entered
Age = 31
Employment type = 0 (Government Sector)
Graduate or not = 1 (yes)
Annual income = 400000
Family members = 6
Chronic diseases = 1 (yes)
Frequent flyer = 0 (no)
Ever travelled abroad = 0 (no)

and the answer is that the person has not bought the insurance


now we will see how to get our model accuracy here our model is 81% accurate which means it has guessed 81 values correct out of 100 which is a good accuracy

source code

you can go check the link for full code


in the article I have given you information and codes on how to make a decision tree model with source code I would be making more exciting models for you so stay connected




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Machine Learning Infrastructure ~Terraforming SageMaker Part 2~

Personalized Store Feed with Vector Embeddings

Executing Machine Learning Codes on Docker Container

We are pulling centos image from docker hub .

365 Days of Online Abuse

Online machine learning (Part 3): Exponentiated Gradient

Practical Machine Learning with C++ and GRT

Delivery time Prediction System

Twitter Sentiment Analysis-(Revised)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aviral Bhardwaj

Aviral Bhardwaj


More from Medium

Iris classification — Support Vector Machine

Predict using naïve bayes with python

On the Trees of Machine Learning

Machine Learning and Healthcare — Detection of Parkinson’s Disease