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!car_insurance = pd.read_csv("car_insurance.csv")My Observation
car_insurance.head(50)car_insurance.groupby('outcome')[['driving_experience', 'past_accidents']].value_counts()import seaborn as sns
sns.countplot(x='driving_experience', hue='outcome', data = car_insurance)car_insurance.info()visualizing the categorical and numeric important variables against outcome variable
import matplotlib.pyplot as plt
def plot_categorical(feature):
plt.figure(figsize=(10, 6))
sns.barplot(x=feature, y='outcome', data = car_insurance, ci=None)
plt.title(f'Average Claim Rate by {feature.capitalize()}')
plt.ylabel('Claim Rate')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
def plot_numerical(feature):
plt.figure(figsize=(10, 6))
sns.boxplot(x='outcome', y=feature, data = car_insurance)
plt.title(f'Distribution of {feature.capitalize()} by Claim Status')
plt.xlabel('Claim Status (0: No Claim, 1: Claim)')
plt.tight_layout()
plt.show()
features = ['duis', 'past_accidents', 'speeding_violations', 'gender',
'driving_experience', 'age', 'vehicle_type', 'annual_mileage', 'credit_score']
for feature in features:
if car_insurance[feature].dtype in ['int64', 'float64']:
plot_numerical(feature)
else:
plot_categorical(feature)yeah so I can see here after all visualization 'age' or 'driving experience'(which are likely correlated) to perform logistic regration on our dataset to predict 'outcome'.
Now focus on the project instruction
first task: Identify the single feature of the data that is the best predictor of whether a customer will put in a claim (the "outcome" column), excluding the "id" column.
Reading in and exploring the dataset
car_insurance.describe()