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
import matplotlib.pyplot as plt
from statsmodels.formula.api import logit
# Loading file
car_insurance = pd.read_csv('car_insurance.csv')
# Get columns with missing values
cols_with_NaN = [col for col in car_insurance.columns if car_insurance[col].isnull().sum() > 0]
print(car_insurance[cols_with_NaN].info(), '\n')
print(car_insurance[cols_with_NaN].describe())# fill missing values with average
for col in cols_with_NaN:
car_insurance[col] = car_insurance[col].fillna(car_insurance[col].mean())
# List of featues
features_list = car_insurance.drop(columns=['id', 'outcome']).columns
# Empty list for adding model accuracies
accuracy_list = []
# Get model accuracies and append to list above
for feature in features_list:
formula = "outcome ~ {}".format(feature)
model = logit(formula, data=car_insurance).fit()
model = model.pred_table()
TN, FP, FN, TP = model[0,0], model[0,1], model[1,0], model[1,1]
accuracy = (TN + TP) / (TN + TP + FN + FP)
accuracy_list.append(accuracy)
print(accuracy_list)
best_accuracy = max(accuracy_list)
best_feature = features_list[accuracy_list.index(best_accuracy)]
best_feature_df = pd.DataFrame({'best_feature': [best_feature], 'best_accuracy': [best_accuracy]})
print(f'The single feature with the best predictor is {best_feature} with a model accuracy of {best_accuracy}')