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
# Reading the data
df = pd.read_csv("car_insurance.csv")
# Examining all the types and missing values
print(df.describe())
print(df.info())
print(df.isnull().sum())
# Filling the missing values
for col in df.columns:
if df[col].dtype == "object":
df[col].fillna(df[col].mode()[0], inplace=True)
else:
df[col].fillna(df[col].median(), inplace=True)
# Droping the id column
df.drop(columns = ["id"], inplace = True)
# labellimg the target column
target = "outcome"
# List of features excluding the target
features = [col for col in df.columns if col != target]
# Dictionary to store features and accuracy
feature_scores = {}
# Loop through the features
for feature in features:
# Use the formula notation for statsmodels logit
formula = f"{target} ~ {feature}"
# Fit a Logistic Regression model with each feature
model = logit(formula, data = df).fit(disp =0)
# Make predictions using the model
predictions = (model.predict(df[feature]) > 0.5).astype(int)
# Calculate accuracy
accuracy = (predictions == df[target]).mean()
# Store the feature and its accuracy
feature_scores[feature] = accuracy
# Display the collected scores
print(feature_scores)
# Identify the best performing feature
best_feature = max(feature_scores, key = feature_scores.get)
best_accuracy = feature_scores[best_feature]
# Store in the required DataFrame format
best_feature_df = pd.DataFrame({
"best_feature" : [best_feature],
"best_accuracy" : [best_accuracy]
})
# Display the final result
print(best_feature_df)