Skip to content

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

ColumnDescription
idUnique client identifier
ageClient's age:
  • 0: 16-25
  • 1: 26-39
  • 2: 40-64
  • 3: 65+
genderClient's gender:
  • 0: Female
  • 1: Male
driving_experienceYears the client has been driving:
  • 0: 0-9
  • 1: 10-19
  • 2: 20-29
  • 3: 30+
educationClient's level of education:
  • 0: No education
  • 1: High school
  • 2: University
incomeClient's income level:
  • 0: Poverty
  • 1: Working class
  • 2: Middle class
  • 3: Upper class
credit_scoreClient's credit score (between zero and one)
vehicle_ownershipClient's vehicle ownership status:
  • 0: Does not own their vehilce (paying off finance)
  • 1: Owns their vehicle
vehcile_yearYear of vehicle registration:
  • 0: Before 2015
  • 1: 2015 or later
marriedClient's marital status:
  • 0: Not married
  • 1: Married
childrenClient's number of children
postal_codeClient's postal code
annual_mileageNumber of miles driven by the client each year
vehicle_typeType of car:
  • 0: Sedan
  • 1: Sports car
speeding_violationsTotal number of speeding violations received by the client
duisNumber of times the client has been caught driving under the influence of alcohol
past_accidentsTotal number of previous accidents the client has been involved in
outcomeWhether the client made a claim on their car insurance (response variable):
  • 0: No claim
  • 1: Made a claim
# Import necessary libraries
import pandas as pd
import numpy as np
from statsmodels.formula.api import logit

# Load the dataset
cars = pd.read_csv("car_insurance.csv")

# Fill missing values in 'credit_score' and 'annual_mileage' with their respective means
# 'inplace=True' modifies the DataFrame directly
cars["credit_score"].fillna(cars["credit_score"].mean(), inplace=True)
cars["annual_mileage"].fillna(cars["annual_mileage"].mean(), inplace=True)

# Initialize variables to store the best performing feature and its accuracy
# 'best_accuracy' is set to -1 to ensure any valid accuracy (0 to 1) will be greater
best_accuracy = -1
best_feature = None

# Identify feature columns by dropping 'id' and 'outcome'
features = cars.drop(columns=["id", "outcome"]).columns

print("Calculating models and accuracies for each feature...")

# Iterate through each feature column to build and evaluate individual logistic regression models
for col in features:
    # Build and train a logistic regression model
    # 'outcome' is the dependent variable, 'col' is the independent variable
    # 'data=cars' specifies the dataset
    # 'disp=False' suppresses the optimization summary output for cleaner console display
    model = logit(f"outcome ~ {col}", data=cars).fit(disp=False)

    # Calculate the confusion matrix from the model's predictions
    # This matrix summarizes correct and incorrect predictions
    # conf_matrix[0,0] = True Negatives (TN)
    # conf_matrix[1,1] = True Positives (TP)
    # conf_matrix[1,0] = False Negatives (FN)
    # conf_matrix[0,1] = False Positives (FP)
    conf_matrix = model.pred_table()
    tn = conf_matrix[0,0]
    tp = conf_matrix[1,1]
    fn = conf_matrix[1,0]
    fp = conf_matrix[0,1]

    # Calculate the accuracy of the current model
    # Accuracy = (Correct Predictions) / (Total Predictions)
    current_accuracy = (tn + tp) / (tn + fn + fp + tp)

    # Compare the current model's accuracy with the best accuracy found so far
    # If the current model is more accurate, update 'best_accuracy' and 'best_feature'
    if current_accuracy > best_accuracy:
        best_accuracy = current_accuracy
        best_feature = col
    
    # Print the accuracy for the current feature to show progress
    print(f"  Feature: {col}, Accuracy: {current_accuracy:.4f}")

# Create a Pandas DataFrame to display the single best feature and its accuracy
# 'index=[0]' explicitly sets the row index to 0 for this single-row DataFrame
best_feature_df = pd.DataFrame({
    "best_feature": best_feature,
    "best_accuracy": best_accuracy
}, index=[0])

# Print the final result
print("\n--- Final Result ---")
print(best_feature_df)