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
| 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
# Load the dataset
df = pd.read_csv('car_insurance.csv', index_col='id')
# Define the columns with nominal categorical data
nominal_cat_cols = ['gender', 'vehicle_ownership', 'married', 'postal_code', 'vehicle_type']
# Define the columns with ordinal categorical data and their respective order
ordinal_cat_cols = {
'age': [0, 1, 2, 3],
'driving_experience' : ['0-9y', '10-19y', '20-29y', '30y+'],
'education': ['none', 'high school', 'university'],
'income': ['poverty', 'working class', 'middle class', 'upper class'],
'vehicle_year': ['before 2015', 'after 2015']
}
# Define the columns with integer data
int_cols = ['children', 'speeding_violations', 'duis', 'past_accidents']
# Iterate through each column in the dataframe and set the appropriate data type
for col in df.columns:
if col in nominal_cat_cols:
# Convert nominal categorical columns to 'category' data type
df[col] = df[col].astype('category')
elif col in int_cols:
# Convert integer columns to 'int8' data type for memory efficiency
df[col] = df[col].astype('int8')
elif col in ordinal_cat_cols:
# Convert ordinal categorical columns to 'category' data type with specified order
category = pd.CategoricalDtype(ordinal_cat_cols[col], ordered=True)
df[col] = df[col].astype(category)df.head()df.info()# Fill missing values in 'credit_score' with the mean of the column
credit_score_mean = df['credit_score'].mean()
df['credit_score'].fillna(credit_score_mean, inplace=True)
print(f"Filled missing 'credit_score' values with the mean: {credit_score_mean}")
# Fill missing values in 'annual_mileage' with the mean of the column
annual_mileage_mean = df['annual_mileage'].mean()
df['annual_mileage'].fillna(annual_mileage_mean, inplace=True)
print(f"Filled missing 'annual_mileage' values with the mean: {annual_mileage_mean}")# Dictionary to store the accuracy of each feature
features = dict()
# Iterate over each column except the last one (assumed to be the target variable)
# postal_code column raises ConvergenceWarning
for col in df.columns[:-1]:
# Fit a logistic regression model using the current column as the predictor
mdl = logit(f'outcome ~ {col}', data=df).fit(disp=False)
# Generate the confusion matrix
conf_matrix = mdl.pred_table()
# Manually calculate the accuracy from the confusion matrix
accuracy = np.trace(conf_matrix) / conf_matrix.sum()
# Store the accuracy in the dictionary with the column name as the key
features[col] = accuracy
features# Identify the feature with the highest accuracy
best_feature = max(features, key=features.get)
best_accuracy = features[best_feature]
# Create a DataFrame to store the best feature and its accuracy
best_feature_df = pd.DataFrame({
"best_feature": best_feature,
"best_accuracy": best_accuracy
}, index=[0])
# Display the DataFrame
best_feature_df