Skip to content

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!

(Source: https://www.accenture.com/_acnmedia/pdf-84/accenture-machine-leaning-insurance.pdf)

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

ColumnDescription
idUnique client identifier
ageClient's age:
  • 0: 16-25
  • 1: 26-39
  • 2: 40-64
  • 3: 65+
genderClient's gender:
  • 0: Female
  • 1: Male
driving_experienceYears the client has been driving:
  • 0: 0-9
  • 1: 10-19
  • 2: 20-29
  • 3: 30+
educationClient's level of education:
  • 0: No education
  • 1: High school
  • 2: University
incomeClient's income level:
  • 0: Poverty
  • 1: Working class
  • 2: Middle class
  • 3: Upper class
credit_scoreClient's credit score (between zero and one)
vehicle_ownershipClient's vehicle ownership status:
  • 0: Does not own their vehilce (paying off finance)
  • 1: Owns their vehicle
vehcile_yearYear of vehicle registration:
  • 0: Before 2015
  • 1: 2015 or later
marriedClient's marital status:
  • 0: Not married
  • 1: Married
childrenClient's number of children
postal_codeClient's postal code
annual_mileageNumber of miles driven by the client each year
vehicle_typeType of car:
  • 0: Sedan
  • 1: Sports car
speeding_violationsTotal number of speeding violations received by the client
duisNumber of times the client has been caught driving under the influence of alcohol
past_accidentsTotal number of previous accidents the client has been involved in
outcomeWhether the client made a claim on their car insurance (response variable):
  • 0: No claim
  • 1: Made a claim
# Import required modules
import pandas as pd
import numpy as np
from statsmodels.formula.api import logit
import seaborn as sns
import matplotlib.pyplot as plt


# Start coding!
# read the csv file
df = pd.read_csv('car_insurance.csv') 
df.head()
df.info()
Hidden output
df.isna().sum()
Hidden output
# How is the distribution of the credit score? is it normal?
plt.figure(figsize=(3,3))
sns.histplot(df['credit_score'], bins=50)
plt.show
# determine the distribution of annual_mileage
plt.figure(figsize=(3,3))
sns.histplot(df['annual_mileage'])
plt.show()
# calculate the median credit score
median_credit = df['credit_score'].median()
median_mileage = df['annual_mileage'].mean()
# fill the null credit score with median value
df1 = df.fillna({'credit_score':median_credit,
                 'annual_mileage': median_mileage})
obj_features = [col for col in df1.columns if df1[col].dtype == 'object']
obj_features
# create a dectionary of mapping that convert values in the object-features to numbers
encoding_dict = {}
for col in obj_features:
    unique_values = df1[col].unique()
    encoding_dict[col] = {value: index for index, value in enumerate(unique_values)}
encoding_dict
# apply map and convert the values in the dataframe
for col in obj_features:
    df1[col] = df1[col].map(encoding_dict[col])
df1.head()
# define the dependent and independent variable
num_features = df1.drop(columns=['id','outcome']).columns
target = df1['outcome']
models = []
for col in num_features:
    formula = f"target ~ {col}" 
    model = logit(formula, data = df1).fit()
    models.append(model)