Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’³ Insurance Charges Prediction

A Machine Learning regression project that predicts estimated insurance charges based on customer information.

The project compares multiple regression algorithms and uses the best-performing model to build an interactive Streamlit web application.

πŸš€ Live Demo

πŸ”— Live Application:
https://ayyan-insurance-charges-prediction.streamlit.app

πŸ“Œ Project Overview

This project demonstrates an end-to-end Machine Learning workflow for insurance charges prediction.

The following customer information is used for prediction:

  • Age
  • Sex
  • BMI
  • Number of Children
  • Smoking Status
  • Region

Multiple regression algorithms were trained and evaluated to determine the best model for the prediction task.

The final model was integrated into a Streamlit frontend and deployed online.

πŸ€– Models Compared

  • Linear Regression
  • Decision Tree Regressor
  • K-Nearest Neighbors Regressor
  • Random Forest Regressor

πŸ“Š Model Performance

Model RΒ² Score Adjusted RΒ² MAE RMSE
Linear Regression 0.802517 0.792410 4338.923817 6024.004673
Decision Tree 0.797372 0.787001 2743.403351 6101.977336
KNN 0.561290 0.538837 5532.465446 8978.616749
Random Forest 0.882175 0.876145 2588.687357 4653.061926

πŸ† Best Model

The Random Forest Regressor achieved the best overall performance.

Random Forest Performance

  • RΒ² Score: 0.882175
  • Adjusted RΒ²: 0.876145
  • MAE: 2588.687357
  • RMSE: 4653.061926

Random Forest achieved the highest RΒ² and Adjusted RΒ² scores while also achieving the lowest MAE and RMSE.

Therefore, Random Forest was selected as the final model for deployment.

🧠 Machine Learning Workflow

Dataset
   ↓
Data Cleaning
   ↓
Exploratory Data Analysis
   ↓
Feature Engineering
   ↓
Categorical Encoding
   ↓
Train / Test Split
   ↓
Feature Scaling
   ↓
Model Training
   ↓
Model Evaluation
   ↓
Model Comparison
   ↓
Best Model Selection
   ↓
Model Serialization
   ↓
Streamlit Frontend
   ↓
Deployment

πŸ“‚ Project Structure

Insurance_charges_Prediction/
β”‚
β”œβ”€β”€ pickles/
β”‚   β”œβ”€β”€ model.pkl
β”‚   β”œβ”€β”€ scalar.pkl
β”‚   β”œβ”€β”€ columns.pkl
β”‚   └── columns_to_scale.pkl
β”‚
β”œβ”€β”€ app.py
β”œβ”€β”€ insurance.ipynb
β”œβ”€β”€ insurance.csv
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
└── .gitattributes

πŸ–₯️ Web Application

The Streamlit application provides an interactive interface for estimating insurance charges.

Users can enter:

  • Age
  • Sex
  • BMI
  • Number of Children
  • Smoking Status
  • Region

After submitting the information, the trained Random Forest model generates an estimated insurance charge.

Features

  • Modern dark-themed interface
  • Responsive design
  • Mobile-friendly layout
  • Interactive input fields
  • Random Forest regression prediction
  • Estimated insurance charges
  • Premium prediction result dialog
  • Deployed Streamlit application

πŸ“ˆ Evaluation Metrics

RΒ² Score

RΒ² measures how much of the variation in the target variable is explained by the model.

Higher values indicate better performance.

Adjusted RΒ²

Adjusted RΒ² accounts for the number of predictors used by the model.

Higher values indicate better performance.

Mean Absolute Error

MAE measures the average absolute difference between actual and predicted values.

Lower values indicate better performance.

Root Mean Squared Error

RMSE measures the square root of the average squared prediction error and gives greater importance to larger errors.

Lower values indicate better performance.

πŸ”¬ Model Comparison

The model comparison shows that:

  • Linear Regression provides a strong baseline.
  • Decision Tree achieves relatively low MAE but has a higher RMSE than Random Forest.
  • KNN performs significantly worse on this dataset.
  • Random Forest provides the strongest overall performance.

The Random Forest model achieves an RΒ² score of approximately 0.88, meaning it explains a large portion of the variation in insurance charges.

🌐 Deployment

The application is deployed using Streamlit Community Cloud.

πŸ”— Live Application:
https://ayyan-insurance-charges-prediction.streamlit.app

The trained model and preprocessing artifacts are included with the project so the Streamlit application can load them during deployment.

The large model.pkl file is managed using Git Large File Storage (Git LFS).

βš™οΈ Installation

1. Clone the Repository

git clone https://github.com/code-with-ayyan/Insurance_charges_Prediction.git

2. Move into the Project Directory

cd Insurance_charges_Prediction

3. Create a Virtual Environment

python -m venv venv

4. Activate the Virtual Environment

Linux:

source venv/bin/activate

Windows:

venv\Scripts\activate

5. Install Dependencies

pip install -r requirements.txt

6. Run the Streamlit Application

streamlit run app.py

πŸ“¦ Requirements

  • Python
  • NumPy
  • Pandas
  • Scikit-learn
  • Joblib
  • Streamlit
  • Matplotlib
  • Seaborn

πŸ’Ύ Model Serialization

The trained model and preprocessing components are saved using Joblib.

pickles/
β”œβ”€β”€ model.pkl
β”œβ”€β”€ scalar.pkl
β”œβ”€β”€ columns.pkl
└── columns_to_scale.pkl
  • model.pkl β€” trained Random Forest regression model
  • scalar.pkl β€” fitted feature scaler
  • columns.pkl β€” model feature columns
  • columns_to_scale.pkl β€” columns requiring scaling before prediction

πŸ“± Responsive Application

The frontend is designed to work across:

  • Desktop
  • Laptop
  • Tablet
  • Mobile

The interface automatically adapts to smaller screen sizes for a better user experience.

⚠️ Disclaimer

The predicted value is a machine-learning estimate and should not be considered an official insurance quote.

Actual insurance charges may vary depending on the insurance provider, policy, coverage, location, customer profile, and other factors.

This project is intended for educational and demonstration purposes.

πŸ“š Learning Outcomes

This project helped in understanding:

  • Regression problems
  • Feature preprocessing
  • Categorical encoding
  • Feature scaling
  • Train-test splitting
  • Regression model comparison
  • Evaluation metrics
  • Random Forest Regression
  • Model serialization
  • Streamlit application development
  • Machine Learning deployment

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

Ayyan Ahmed

Machine Learning & AI Student

Building and understanding Machine Learning algorithms from fundamentals to deployment.

About

End-to-end Machine Learning regression project for insurance charges prediction, featuring EDA, feature engineering, preprocessing, model comparison, evaluation, Random Forest optimization, and Streamlit deployment.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages