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)

Aviral Bhardwaj
4 min readNov 26, 2021

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 URLhttps://raw.githubusercontent.com/aviralb13/codes/main/datas/vehicle%20insursnce.csv

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

Model

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

Predicting

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

Accuracy

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

Conclusion

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

--

--

Aviral Bhardwaj

One of the youngest writer and mentor on AI-ML & Technology.