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
from statsmodels.formula.api import logit
# Start coding!
# Initalize df
df = pd.read_csv('car_insurance.csv')
df.info()
# Fill NAs, we could use correlation to single out more specific features, but for safety measure we use all columns and will say which is the best based on accuracy
df['annual_mileage'].fillna(df['annual_mileage'].mean(), inplace= True)
df['credit_score'].fillna(df['credit_score'].mean(), inplace= True)
# Now that we have our features we can use a for loop to create a model for each and fit them accordingly
cols = df.drop(['id','outcome'], axis = 1)
models = []
# Initiate for loop
for col in cols:
#Create model
model = logit(f'outcome ~{col}', data = df).fit()
#Append
models.append(model)
# Empty list to store accuracies
accuracies = []
# Loop through models
for feature in range(0, len(models)):
# Compute the confusion matrix
conf_matrix = models[feature].pred_table()
# True negatives
tn = conf_matrix[0,0]
# True positives
tp = conf_matrix[1,1]
# False negatives
fn = conf_matrix[1,0]
# False positives
fp = conf_matrix[0,1]
# Compute accuracy
acc = (tn + tp) / (tn + fn + fp + tp)
accuracies.append(acc)
models
# Make a list as the indexes are the same for cols and accuracies so they sould be matched when displayed in a dataframe
list_of_cols = cols.columns.to_list()
# The same for accuracy
outcome = pd.DataFrame({'best_feature':list_of_cols,
'best_accuracy':accuracies})
best_feature_df = outcome[outcome['best_accuracy'] == max(outcome['best_accuracy'])]
best_feature_df