Build This Project · Portfolio Guide
Insurance Claims Analytics — Complete Project Guide
Many claims-analytics tutorials online share one bug: they pass a text column such as claim_type straight into a scikit-learn model, and the script stops with ValueError: could not convert string to float. Others train on data where only a few percent of claims are fraudulent, report 95% accuracy and call it a success. This project avoids both problems. You will compute the KPIs a claims team actually watches, build a fraud-flag model correctly, and present results that hold up in an interview.
The business questions
Write these at the top of your README. Every chart and model in the project should answer one of them.
- How many claims do we receive, of what type, and how large are they?
- How long do claims take to settle, and where are the delays?
- Which claims should a human investigator look at first?
The data
Real insurer data is private. Two practical options:
- A public practice file. Kaggle hosts several auto-insurance claims datasets with a fraud label (for example, files with a
fraud_reportedcolumn). Read the dataset page to check its size, columns and licence. Avoid using card-transaction sets such as IEEE-CIS for this project; they are about online payments, not insurance claims. - Synthetic data you generate. This makes the project reproducible, and you control the patterns, which helps you check whether your model finds them. The code below does this.
import numpy as np
import pandas as pd
rng = np.random.default_rng(7)
n = 2000
df = pd.DataFrame({
"claim_type": rng.choice(["motor", "health", "property"], n, p=[0.5, 0.4, 0.1]),
"policy_tenure_months": rng.integers(1, 120, n),
"num_prev_claims": rng.poisson(0.6, n),
"claim_amount": rng.gamma(2.0, 25000, n).round(-2),
"days_to_settle": rng.integers(3, 60, n),
})
# synthetic rule for who is fraudulent, plus noise
risk = (df.policy_tenure_months < 6) * 1.5 + (df.num_prev_claims >= 2) * 1.0 \
+ (df.claim_amount > 100000) * 1.0
df["fraud_flag"] = (rng.random(n) < 0.02 + 0.12 * risk).astype(int)
kpi = df.groupby("claim_type").agg(
claims=("claim_amount", "size"),
avg_amount=("claim_amount", "mean"),
median_days=("days_to_settle", "median"),
fraud_rate=("fraud_flag", "mean"),
).round({"avg_amount": 0, "fraud_rate": 3})
print(kpi)
print("overall fraud rate:", round(df.fraud_flag.mean(), 3))
df.to_csv("claims.csv", index=False)Output (pandas 3.1, NumPy 2.5):
claims avg_amount median_days fraud_rate
claim_type
health 794 50830.0 29.0 0.050
motor 1013 49983.0 31.0 0.042
property 193 45370.0 31.0 0.036
overall fraud rate: 0.045Only 4.5% of claims are labelled fraud. Keep that number in mind; it decides how you judge the model later. Because the data is seeded, you will get the same table if you run the same code with the same library versions.
KPIs for the claims dashboard
| KPI | Definition | What a claims manager does with it |
|---|---|---|
| Claim count and mix | Number of claims by type, month and region | Plans staffing and spots unusual spikes |
| Average and median claim amount (severity) | Total claimed or paid / number of claims | Tracks cost trends; the median resists a few huge claims |
| Settlement turnaround time | Days from claim intimation to payment, shown as median and 90th percentile | Finds slow stages such as document collection or survey |
| Rejection or repudiation rate | Claims rejected / claims decided | Checks consistency across branches and claim types |
| Loss ratio | Claims incurred / premium earned for the same period | The headline profitability measure for a product line |
| Investigation hit rate | Investigated claims confirmed as fraud / claims investigated | Shows whether fraud flags are worth the investigators' time |
Loss ratio needs premium data, which most practice files lack. If yours does not have it, say so rather than inventing a figure.
The fraud-flag model, done correctly
Three fixes compared with the usual tutorial version: one-hot encode the text column, stratify the split so both sets have the same fraud share, and give the rare class more weight. days_to_settle is dropped because it is only known after the claim is settled, so using it would leak future information into the model.
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
df = pd.read_csv("claims.csv")
X = pd.get_dummies(df.drop(columns=["fraud_flag", "days_to_settle"]),
columns=["claim_type"])
y = df["fraud_flag"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=42)
model = RandomForestClassifier(n_estimators=200, min_samples_leaf=5,
class_weight="balanced", random_state=42)
model.fit(X_train, y_train)
proba = model.predict_proba(X_test)[:, 1]
pred = (proba >= 0.5).astype(int)
print(classification_report(y_test, pred, digits=2))
imp = pd.Series(model.feature_importances_, index=X.columns)
print(imp.sort_values(ascending=False).round(3))Output (scikit-learn 1.9.1):
precision recall f1-score support
0 0.98 0.87 0.92 478
1 0.17 0.59 0.26 22
accuracy 0.85 500
macro avg 0.57 0.73 0.59 500
weighted avg 0.94 0.85 0.89 500
claim_amount 0.450
policy_tenure_months 0.335
num_prev_claims 0.192
claim_type_motor 0.012
claim_type_health 0.008
claim_type_property 0.004
dtype: float64Reading the result honestly
- Accuracy (0.85) is the wrong headline. A model that never flags fraud would score about 0.96 on this test set, because 478 of 500 claims are genuine.
- Recall 0.59 means the model caught 13 of the 22 fraudulent claims.
- Precision 0.17 means most flagged claims are genuine. That is normal for rare-event problems; a flag only sends a claim for review, it does not reject it.
- Feature importance recovers the three signals we built into the data (amount, tenure, previous claims) and correctly gives claim type almost no weight. Importance shows what the model uses, not what causes fraud.
The 0.5 threshold is arbitrary. The business should choose it based on how many claims investigators can review. Add this after the model code:
from sklearn.metrics import precision_score, recall_score
for t in (0.3, 0.5, 0.7):
p = (proba >= t).astype(int)
print(f"threshold {t}: flagged={p.sum():3d} "
f"precision={precision_score(y_test, p, zero_division=0):.2f} "
f"recall={recall_score(y_test, p):.2f}")threshold 0.3: flagged=125 precision=0.15 recall=0.86
threshold 0.5: flagged= 77 precision=0.17 recall=0.59
threshold 0.7: flagged= 24 precision=0.25 recall=0.27If the team can review about 25 claims per 500, a threshold near 0.7 fits that capacity but misses most fraud. If catching fraud matters more, 0.3 catches 86% at the cost of 125 reviews. Presenting this trade-off as a table is more valuable in an interview than any single accuracy score.
Dashboard and write-up
- Page 1: KPI cards (claims, median amount, median settlement days, rejection rate), a monthly trend and a breakdown by claim type.
- Page 2: a ranked list of flagged claims with their score and the top reasons, filterable by type and branch.
- README: the business questions, data source and its limits, the KPI definitions, the threshold table and two or three recommendations.
For context on how insurers use these systems, read how insurance companies use analytics and AI. For other domain projects that round out a portfolio, see the healthcare analytics dashboard and the HR attrition project. If you are new to scikit-learn, start with this beginner's guide.
FAQs
Why not use accuracy for fraud detection?
Because fraud is rare. When 96% of claims are genuine, a model that predicts "genuine" every time is 96% accurate and useless. Use precision, recall and the number of claims flagged.
Is synthetic data acceptable in a portfolio?
Yes, if you say so clearly and explain how you generated it. It is better than a public file you cannot explain.
Which is better here, Random Forest or XGBoost?
Try both with the same split and compare recall at a fixed review capacity. On small tabular data the difference is often small; your handling of imbalance and thresholds matters more.