Skip to content

Repository files navigation

πŸ₯ Insurance Premium Prediction API & Serving System

An end-to-end Machine Learning application that predicts insurance premium categories based on customer demographics, lifestyle factors, health indicators, and financial information.

The project follows a complete MLOps-inspired workflow:

  • Data Analysis & Feature Engineering
  • Random Forest Classification Model
  • FastAPI REST API Development
  • Pydantic Request Validation
  • Interactive Streamlit Frontend
  • Cloud Deployment on Render

πŸš€ Live Demo

API Documentation

https://insurance-premium-prediction-api-serving.onrender.com

Streamlit Application

https://insurance-premium-prediction-api-serving-98v3.onrender.com/

Getting Started

  1. Open the API documentation link first.
  2. Wait for the API to respond successfully.
  3. Open the Streamlit application.
  4. Enter the required details and generate a prediction.

Note

The backend is hosted on Render and may enter a sleep state after periods of inactivity. The first request may take a little longer while the server wakes up. Once initialized, the application should respond normally.

🐳 Docker Image

The project is also available as a Docker image on Docker Hub.

Docker Hub Repository

https://hub.docker.com/r/omikalix/insurance-premium-app

Pull Image

docker pull omikalix/insurance-premium-app

Run Container

docker run -p 8000:8000 -p 8501:8501 omikalix/insurance-premium-app

Access Application

Note: The services listen on 0.0.0.0 inside the container but should be accessed through 127.0.0.1 or localhost from your browser.


πŸ“Έ Application Screenshots

πŸ–₯️ Interactive Streamlit Dashboard

Insurance Premium Prediction UI

πŸš€ FastAPI Documentation & Testing

FastAPI Swagger Documentation


πŸ“Œ Project Overview

Insurance companies determine premium categories based on multiple factors such as:

  • Age
  • BMI
  • Smoking Habits
  • Income
  • Occupation
  • City Tier
  • Lifestyle Risk

This project leverages Machine Learning to automate premium category prediction.

The model classifies customers into:

Category Meaning
🟒 Low Lower insurance risk
🟑 Medium Moderate insurance risk
πŸ”΄ High Higher insurance risk

πŸ—οΈ System Architecture

User
  β”‚
  β–Ό
Streamlit UI
  β”‚
  β–Ό
FastAPI REST API
  β”‚
  β–Ό
Random Forest Model (.pkl)
  β”‚
  β–Ό
Prediction Response

πŸ“‚ Project Structure

serving-ml-models/
β”‚
β”œβ”€β”€ images/
β”‚   β”œβ”€β”€ low.png
β”‚   β”œβ”€β”€ medium.png
β”‚   β”œβ”€β”€ high.png
β”‚   └── background_image.png
β”‚
β”œβ”€β”€ model.ipynb
β”œβ”€β”€ insurance_premium_model.pkl
β”œβ”€β”€ insurance_premium_api.py
β”œβ”€β”€ UI.py
β”œβ”€β”€ Indian_Insurance_Data.csv
β”‚
β”œβ”€β”€ requirements.txt
└── README.md

πŸ“Š Dataset Features

Original Features

Feature Description
age Customer age
weight Weight in kg
height Height in meters
income_lpa Annual income
smoker Smoking status
city Customer city
occupation Occupation

βš™οΈ Feature Engineering

The following custom features were created:

BMI

BMI = weight / (height ** 2)

Income Per Age

income_lpa / age

Weight Height Ratio

weight / height

Income Γ— BMI

income_lpa * bmi

Smoker Income

smoker * income_lpa

Health Risk Index

(bmi * 0.4) + (age * 0.3) + (smoker * 20)

City Tier Classification

Tier 1
Tier 2
Tier 3

πŸ€– Machine Learning Model

Algorithm

RandomForestClassifier()

Data Preprocessing

  • One Hot Encoding
  • Feature Engineering
  • Pipeline Architecture
  • Column Transformer

Train Test Split

80% Training
20% Testing

πŸ“ˆ Model Performance

Classification Report

Class Precision Recall F1
High 0.91 0.90 0.91
Low 0.91 0.93 0.92
Medium 0.88 0.87 0.87

Overall Metrics

Accuracy        : 89.63%
Macro F1 Score  : 0.90

Cross Validation

Fold 1 : 89.25%
Fold 2 : 89.63%
Fold 3 : 87.25%
Fold 4 : 90.75%
Fold 5 : 91.75%

Mean CV Accuracy : 89.73%

πŸ”₯ Feature Importance

Top contributing features:

1. Health Risk Index
2. BMI
3. Age
4. Weight Height Ratio
5. Smoker Income
6. Income (LPA)
7. Income Γ— BMI
8. Income Per Age

Key Insight:

Insurance premium category is primarily influenced by:

  • Health Risk
  • BMI
  • Age
  • Smoking Status
  • Income

while City Tier and Occupation contribute less to prediction.


⚑ FastAPI Backend

The backend API is developed using:

  • FastAPI
  • Pydantic
  • Uvicorn

Features

  • Request Validation
  • JSON Response
  • Interactive Swagger Documentation
  • Fast Prediction Serving

API Endpoint

POST /predict

Sample Request

{
  "age": 30,
  "weight": 70,
  "height": 1.75,
  "income_lpa": 12,
  "smoker": 0,
  "city": "Pune",
  "occupation": "Software Engineer"
}

Sample Response

{
  "predicted_insurance_premium_category": "Low"
}

🎨 Streamlit Frontend

The project includes an interactive Streamlit application featuring:

  • Modern Insurance Dashboard
  • Dynamic Background Design
  • Interactive Form Inputs
  • Real-Time Predictions
  • Personalized Recommendations
  • Category-Based Visual Cards

Premium Category Visualization

🟒 Low Premium

  • Low Risk
  • High Savings
  • Healthy Lifestyle

🟑 Medium Premium

  • Balanced Risk
  • Balanced Coverage

πŸ”΄ High Premium

  • Higher Risk
  • Enhanced Protection

πŸ’‘ Personalized Recommendation Engine

The application generates dynamic recommendations based on:

  • BMI
  • Age
  • Income
  • Occupation
  • Smoking Status
  • Predicted Premium Category

Examples:

🚭 Quit smoking to reduce risk.

πŸƒ Maintain a healthy BMI.

🩺 Schedule annual checkups.

πŸ’° Build an emergency healthcare fund.

πŸ› οΈ Installation

Clone Repository

git clone https://github.com/yourusername/insurance-premium-prediction.git

Move into Project

cd insurance-premium-prediction

Install Dependencies

pip install -r requirements.txt

▢️ Run FastAPI Server

uvicorn insurance_premium_api:app --reload

API Docs

http://127.0.0.1:8000/docs

▢️ Run Streamlit UI

streamlit run UI.py

☁️ Deployment

This project is fully deployed on Render.

Services

βœ… FastAPI Backend

βœ… Streamlit Frontend

Deployment Stack

  • Render
  • FastAPI
  • Streamlit
  • Uvicorn
  • Random Forest Model

πŸ‘¨β€πŸ’» Author

OMKAR SAWANT

Building intelligent machine learning systems with scalable deployment architectures.

Dataset: Health Insurance Dataset by Parth Patel
Source: https://www.kaggle.com/datasets/patelparth3399/indian-insurance-premium-prediction-dataset

License: MIT

Special thanks to the author for making the dataset publicly available.

About

An end-to-end Machine Learning application that predicts insurance premium categories based on customer demographics, lifestyle factors, health indicators, and financial information.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages