Vehicle insurance — Random forest classifier

using the random forest classifier model we will make a model that will predict whether a person will buy insurance for his or her vehicle or not (with source code)

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

if you want to know about the random forest 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 3 columns (gender, vehicle damage, and vehicle age)

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

gender : 1 = male and 0 = female

vehicle age : > 2 = 2 Years , 0 =1–2 Year and 1= < 1 Year

vehicle damage : 1 = yes and 0 = no

and now I have created a list in which I think it will be the deciding factor 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 response 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 random forest classifier because we have to classify our values into 0 or 1 for making that we have to import random forest classifier from sklearn ensemble 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
gender = 1 (male)
age = 44
vehicle age = 2 (>2 Years)
vehicle damage = 1 (yes)
annual premium = 40454.0

and the answer is that the person has bought the insurance


now we will see how to get our model accuracy here our model is 84% accurate which means it has guessed 84 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 random forest 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

NLP Coaching — The Difference That Makes a Bond

Mixture Density Networks: Probabilistic Regression for Uncertainty Estimation

[TensorFlow 2.0]Word-Embeddings in Keras — Part 2

Understanding Generative Adversarial Networks (GANs)

Hierarchical Softmax as output activation function in Neural Network — Part 2

An efficient AutoML ~Magical TPOT lamp in SageMaker Container~

READ/DOWNLOAD%? TensorFlow for Deep Learning: From

Survival Analysis, Part 1: The Weibull model

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

Predicting House Prices Using Regression Techniques(Machine Learning)

Parameter Tuning for Gradient Boosting Machine (GBM) in Python

Predicting age of a crab in mud crab farming using Machine Learning

Machine Learning For Sentiment Analysis