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):
|
1 - Reading in and exploring the dataset
# Import required modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.formula.api import logit
# Load dataset
df = pd.read_csv('car_insurance.csv')
# Display the first few rows of the dataset
print(df.head())
# Checking for missing values
print(df.isnull().sum())
# Checking data types
print(df.dtypes)
# Summary statistics
print(df.describe())
# Visualizing distributions of features
df.hist(bins=20, figsize=(20, 15))
plt.show()
2 - Filling missing values
# Checking for missing values
print(df.isnull().sum())
# Calculate the mean of credit_score and annual_mileage columns
mean_credit_score = df['credit_score'].mean()
mean_annual_mileage = df['annual_mileage'].mean()
# Fill missing values with the mean value of each column
df['credit_score'].fillna(mean_credit_score, inplace=True)
df['annual_mileage'].fillna(mean_annual_mileage, inplace=True)
# Checking for missing values
print(df.isnull().sum())
3 - Preparing for modeling
models = []
features = df.drop(columns=['outcome', 'id']).columns
4 - Building and storing the models
# Loop through features
for feature in features:
# Creating a logistic regression model to each features using .logit
formula = f"outcome ~ {feature}"
model = logit(formula=formula, data=df).fit()
models.append(model)
5 - Measuring performance
from sklearn.metrics import confusion_matrix
accuracies = []
# Looping through the index of the models list
for i in range(len(models)):
# Getting predictions and creating a confusion matrix
pred_table = models[i].pred_table()
# Extracting true negatives, true positives, false negatives and false positives
tn = pred_table[0, 0]
fp = pred_table[0, 1]
fn = pred_table[1, 0]
tp = pred_table[1, 1]
# Calculating accuracy
accuracy = (tn + tp) / (tn + fp + fn + tp)
# Storing the model's accuracy
accuracies.append(accuracy)
# Printing the model's accuracy
print(f"Accuracy for model {i}: {accuracy}")
6 - Finding the best performing model
# Finding the index of the model with the highest accuracy
best_index = accuracies.index(max(accuracies))
best_feature = features[best_index]
best_accuracy = accuracies[best_index]
# Creating a DataFrame to display the best performing model
best_feature_df = pd.DataFrame({
"best_feature": [best_feature],
"best_accuracy": [best_accuracy]
})
print("Best performing model:")
print(best_feature_df)