Insurance companies invest a lot of time and money into optimizing their pricing and accurately estimating the likelihood that customers will make a claim. In many countries insurance it is a legal requirement to have car insurance in order to drive a vehicle on public roads, so the market is very large!
Knowing all of this, On the Road car insurance have requested your services in building a model to predict whether a customer will make a claim on their insurance during the policy period. As they have very little expertise and infrastructure for deploying and monitoring machine learning models, they've asked you to identify the single feature that results in the best performing model, as measured by accuracy, so they can start with a simple model in production.
They have supplied you with their customer data as a csv file called car_insurance.csv
, along with a table detailing the column names and descriptions below.
The dataset
Column | Description |
---|---|
id | Unique client identifier |
age | Client's age:
|
gender | Client's gender:
|
driving_experience | Years the client has been driving:
|
education | Client's level of education:
|
income | Client's income level:
|
credit_score | Client's credit score (between zero and one) |
vehicle_ownership | Client's vehicle ownership status:
|
vehcile_year | Year of vehicle registration:
|
married | Client's marital status:
|
children | Client's number of children |
postal_code | Client's postal code |
annual_mileage | Number of miles driven by the client each year |
vehicle_type | Type of car:
|
speeding_violations | Total number of speeding violations received by the client |
duis | Number of times the client has been caught driving under the influence of alcohol |
past_accidents | Total number of previous accidents the client has been involved in |
outcome | Whether the client made a claim on their car insurance (response variable):
|
# Import required modules
import pandas as pd
import numpy as np
import seaborn as sns
from statsmodels.formula.api import logit
from scipy.stats import normaltest
import matplotlib.pyplot as plt
# Start coding!
df=pd.read_csv("car_insurance.csv")
display(df.info(),df.head(), df.describe())
#Labeling data
df["driving_experience"] = np.where(df["driving_experience"]=='0-9y', 0,
np.where(df["driving_experience"]=='10-19y', 1,
np.where(df["driving_experience"]=='20-29y', 2,
3)))
df["education"] = np.where(df["education"]=='none', 0,
np.where(df["education"]=='high school', 1,
2))
df["income"] = np.where(df["income"]=='poverty', 0,
np.where(df["income"]=='working class', 1,
np.where(df["income"]=='middle class', 2,
3)))
df["vehicle_year"] = np.where(df["vehicle_year"]=='before 2015', 0,1)
df["vehicle_type"] = np.where(df["vehicle_type"]=='sedan', 0,1)
df.head()
#missing value
sns.histplot(df["credit_score"])
plt.show()
sns.histplot(df["annual_mileage"])
plt.show()
#data is normally distributed from the plot, input all the NaN value with mean
df["credit_score"].fillna(df["credit_score"].mean(),inplace=True)
df["annual_mileage"].fillna(df["annual_mileage"].mean(),inplace=True)
display(df.info(),df.head(), df.describe())
#create a list of feature
features = list(df.columns.drop(['id','outcome']))
features
#create a dataframe to compare actual outcome and predicted outcome
df_outcomes = pd.DataFrame({"actual_outcome":df["outcome"]})
#create a list of calculated accuracy
accuracy_list = []
#looping all the column (feature), calculate the accuracy and append the score to accuracy_list
for col in list(features):
mdl_col_vs_outcome_logit = logit(f"outcome ~ {col}", data=df).fit()
predicted_response = np.round(mdl_col_vs_outcome_logit.predict())
df_outcomes["predicted_outcome"] = predicted_response
accuracy = len(df_outcomes[df_outcomes["actual_outcome"]==df_outcomes["predicted_outcome"]])/len(df_outcomes)
accuracy_list.append(accuracy)
#store all the features and accuracy into a dataframe
best_feature_df =pd.DataFrame({"best_feature":features,"best_accuracy":accuracy_list})
best_feature_df
#select the top 1 feature (best accuracy)
best_feature_df = best_feature_df.sort_values("best_accuracy",ascending=False).head(1)
best_feature_df