Skip to content

SwayamAg/Medical-Insurance-Premium-Prediction

Repository files navigation

πŸ₯ Medical Insurance Premium Prediction App

This repository contains a comprehensive Medical Insurance Premium Prediction system with both a Streamlit web application and a FastAPI backend. The project leverages multiple Machine Learning algorithms to predict insurance premiums based on user demographics and health factors.

🌐 Live Application

πŸš€ Try the App Now

Experience the Medical Insurance Premium Predictor in action!


🌟 Features

Frontend (Streamlit App)

  • Modern UI/UX: Beautiful, responsive interface with dark theme and medical styling
  • Real-Time Predictions: Instant premium predictions with API integration
  • Interactive Visualizations: Model performance comparisons and insights
  • User-Friendly Input: Intuitive form with validation and helpful tooltips

Backend (FastAPI)

  • RESTful API: Production-ready API deployed on Railway
  • Model Serving: Loads trained ML model for predictions
  • Input Validation: Pydantic models for data validation
  • Health Checks: API status monitoring endpoints

Machine Learning

  • Multiple Algorithms: Random Forest, Gradient Boosting, XGBoost comparison
  • Model Performance: Comprehensive evaluation metrics (RΒ² scores, RMSE)
  • Trained Model: Pre-trained model included (Insurance_ML.pkl)
  • Dataset: Complete insurance dataset (insurance.csv)

πŸ“ Repository Structure

Medical_Insurance_premium_ML/
β”œβ”€β”€ App.py                                    # Streamlit frontend application
β”œβ”€β”€ FAPI_MED.py                              # FastAPI backend server
β”œβ”€β”€ Insurance_ML.pkl                         # Trained machine learning model
β”œβ”€β”€ insurance.csv                            # Complete dataset (1,340 records)
β”œβ”€β”€ Medical_Insurance_Premium_Predicting_Model.ipynb  # Jupyter notebook with ML pipeline
β”œβ”€β”€ Medical Insurance Price Prediction using Machine Learning.py  # Standalone ML script
β”œβ”€β”€ requirements.txt                         # Python dependencies
β”œβ”€β”€ Procfile                                # Deployment configuration for Railway
β”œβ”€β”€ README.md                               # This file
β”œβ”€β”€ .gitignore                              # Git ignore patterns
β”œβ”€β”€ Model_Performance.png                   # Model comparison visualization
β”œβ”€β”€ Preview_1.png                           # App interface preview
└── Preview_2.png                           # Additional app preview

🧩 Technologies Used

Frontend & Web

  • Streamlit - Interactive web application
  • HTML/CSS - Custom styling and theming

Backend & API

  • FastAPI - Modern, fast web framework
  • Uvicorn - ASGI server
  • Pydantic - Data validation

Machine Learning

  • Scikit-Learn - ML algorithms and preprocessing
  • XGBoost - Gradient boosting
  • Pandas - Data manipulation
  • NumPy - Numerical computing

Deployment

  • Railway - Cloud deployment platform
  • Pickle - Model serialization

🎯 Quick Start

Option 1: Use the Deployed Streamlit App (Recommended)

🌐 Live Application: https://medical-premium-predictor.streamlit.app/

Simply visit the link above to use the application directly in your browser!

Option 2: Run Locally

  1. Clone the Repository:

    git clone https://github.com/SwayamAg/Medical-Insurance-Premium-Prediction.git
    cd Medical-Insurance-Premium-Prediction
  2. Install Dependencies:

    pip install -r requirements.txt
  3. Run the Streamlit App:

    streamlit run App.py
  4. Access the App: Open your browser to http://localhost:8501

Option 2: Use the FastAPI Backend

  1. Install Dependencies:

    pip install -r requirements.txt
  2. Run the FastAPI Server:

    python FAPI_MED.py
  3. Access the API:

    • API Documentation: http://localhost:8080/docs
    • Health Check: http://localhost:8080/
    • Prediction Endpoint: http://localhost:8080/predict

Option 3: Production API

The FastAPI backend is deployed on Railway and available at:

  • API Base URL: https://medical-insurance-premium-prediction-production.up.railway.app
  • API Documentation: https://medical-insurance-premium-prediction-production.up.railway.app/docs

πŸ” How It Works

Data Input

The system accepts the following parameters:

  • Age: Customer's age (18-64)
  • Sex: Gender (male/female)
  • BMI: Body Mass Index (10-50)
  • Children: Number of dependents (0-5)
  • Smoker: Smoking status (yes/no)
  • Region: Geographic region (northwest/northeast/southeast/southwest)

Prediction Process

  1. User inputs demographic and health data
  2. Data is validated and preprocessed
  3. Categorical variables are encoded
  4. Trained ML model makes prediction
  5. Premium amount is returned to user

Model Performance

The system includes multiple ML models with performance comparisons:

  • Random Forest: High accuracy and interpretability
  • Gradient Boosting: Excellent predictive power
  • XGBoost: State-of-the-art performance

πŸ“Š Dataset Information

The insurance.csv dataset contains 1,340 records with the following features:

  • age: Age of the insured person
  • sex: Gender of the insured person
  • bmi: Body Mass Index
  • children: Number of children/dependents
  • smoker: Smoking status
  • region: Geographic region
  • charges: Insurance premium (target variable)

πŸš€ Deployment

Frontend Deployment

The Streamlit app can be deployed on:

  • Streamlit Cloud
  • Heroku
  • Railway
  • AWS/GCP/Azure

Backend Deployment

The FastAPI backend is currently deployed on Railway:

  • Production URL: https://medical-insurance-premium-prediction-production.up.railway.app
  • Auto-scaling: Handles traffic automatically
  • SSL Certificate: HTTPS enabled

Deployment Configuration

The project includes a Procfile for deployment on Railway and other platforms:

web: uvicorn FAPI_MED:app --host 0.0.0.0 --port $PORT

This Procfile tells the deployment platform to:

  • Run the FastAPI application using uvicorn
  • Use FAPI_MED.py as the main application file
  • Bind to all network interfaces (0.0.0.0)
  • Use the $PORT environment variable for the port number

πŸ“Έ Screenshots

Streamlit Interface

App Interface

Streamlit Interface (Prediction)

App Interface

Model Performance

Model Performance


πŸ”§ Development

Local Development Setup

  1. Create a virtual environment:

    python -m venv .MED
    source .MED/bin/activate  # On Windows: .MED\Scripts\activate
  2. Install development dependencies:

    pip install -r requirements.txt
  3. Run tests and development server:

    # Frontend
    streamlit run App.py
    
    # Backend
    python FAPI_MED.py

Model Training

To retrain the model:

  1. Open Medical_Insurance_Premium_Predicting_Model.ipynb
  2. Run all cells to train new models
  3. Save the best model as Insurance_ML.pkl

πŸ“ API Documentation

Endpoints

GET / - Health Check

Returns API status

{
  "message": "Medical Insurance Premium Predictor API is running."
}

POST /predict - Make Prediction

Input:

{
  "age": 25,
  "sex": "male",
  "bmi": 28.5,
  "children": 0,
  "smoker": "no",
  "region": "northwest"
}

Output:

{
  "predicted_premium": 2840.50
}

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


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

Swayam Agrawal


πŸ™ Acknowledgments

  • Dataset source and contributors
  • Streamlit and FastAPI communities
  • Machine Learning community for algorithms and techniques

⭐ Star this repository if you find it helpful!

About

A comprehensive Medical Insurance Premium Prediction system featuring a Streamlit web app and FastAPI backend. Uses machine learning algorithms (Random Forest, XGBoost, Gradient Boosting) to predict insurance premiums based on demographics and health factors. Includes 1,340-record dataset, trained models, and production deployment on Railway.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors