This project performs comprehensive Exploratory Data Analysis (EDA) on a medical insurance dataset to understand the factors influencing insurance costs. The analysis includes data cleaning, statistical summaries, visualizations, correlation analysis, and feature selection using chi-square tests for categorical variables.
The dataset used is insurance.csv, which contains information about medical insurance costs and related factors. It includes the following columns:
- age: Age of the primary beneficiary
- sex: Gender of the insurance contractor (male/female)
- bmi: Body mass index, providing an understanding of body weight relative to height
- children: Number of children covered by health insurance
- smoker: Smoking status (yes/no)
- region: Residential area in the US (northeast, northwest, southeast, southwest)
- expenses: Individual medical costs billed by health insurance
The dataset contains 1,338 records with no missing values.
.
├── insurance.csv # Raw dataset
├── l.ipynb # Jupyter notebook with complete EDA
└── README.md # Project documentation
- Python 3.x
- pandas: Data manipulation and analysis
- numpy: Numerical computations
- matplotlib: Basic plotting
- seaborn: Statistical data visualization
- scipy: Statistical functions (chi-square test)
- Ensure Python 3.x is installed on your system
- Install required packages:
pip install pandas numpy matplotlib seaborn scipy
- Clone or download this repository
- Open the Jupyter notebook
l.ipynbin Jupyter Lab or Jupyter Notebook
- Load the dataset using pandas
- Display first few rows and dataset shape
- Check data types and missing values
- Generate descriptive statistics for numerical columns
- Check for null values (none found in this dataset)
- Histograms and box plots for numerical variables
- Count plots for categorical variables
- Correlation heatmaps
- Scatter plots showing relationships between variables
- Label encoding for binary categorical variables (sex, smoker)
- One-hot encoding for multi-category variables (region)
- BMI categorization into weight classes
- Correlation analysis for numerical features
- Chi-square test for categorical features to determine statistical significance
- Binning of target variable (expenses) for categorical analysis
- Age: Range from 18 to 64 years, mean ~39 years
- BMI: Range from 15.96 to 53.13, mean ~30.66
- Children: 0 to 5 children, mean ~1.09
- Expenses: Range from $1,122 to $63,770, mean ~$13,270
Strong positive correlations observed between:
- Expenses and age
- Expenses and BMI
- Expenses and smoking status
The analysis evaluates the relationship between categorical features and binned expense categories:
| Feature | Chi-square Statistic | P-value | Decision |
|---|---|---|---|
| smoker | High value | < 0.05 | Keep (significant) |
| sex | Varies | Check p-value | May keep/drop |
| region_* | Varies | Check p-value | May keep/drop |
| bmi_category_* | Varies | Check p-value | May keep/drop |
- Open
l.ipynbin Jupyter Notebook or Jupyter Lab - Run cells sequentially to reproduce the analysis
- View visualizations and statistical outputs
- Modify parameters as needed for further exploration
import pandas as pd
# Load dataset
df = pd.read_csv('insurance.csv')
# View first 5 rows
print(df.head())
# Check dataset shape
print(f"Dataset shape: {df.shape}")# Descriptive statistics
print(df.describe())
# Check for missing values
print(df.isnull().sum())import matplotlib.pyplot as plt
import seaborn as sns
# Distribution of expenses
plt.figure(figsize=(10, 6))
sns.histplot(df['expenses'], bins=30, kde=True)
plt.title('Distribution of Insurance Expenses')
plt.xlabel('Expenses ($)')
plt.ylabel('Frequency')
plt.show()# Correlation matrix
correlation_matrix = df.corr()
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', center=0)
plt.title('Correlation Matrix')
plt.show()- Histograms: Distribution of age, BMI, children, and expenses
- Box Plots: Outlier detection for numerical variables
- Count Plots: Distribution of categorical variables (sex, smoker, region)
- Scatter Plots: Relationships between expenses and other variables
- Correlation Heatmap: Overall correlation between numerical features
- Pair Plots: Multivariate relationships
- BMI Categories: Classified into Normal weight, Overweight, and Obese
- Expense Binning: Divided into 4 equal frequency bins for categorical analysis
- One-hot Encoding: Region variable converted to dummy variables
- Label Encoding: Sex and smoker variables converted to binary
This EDA serves as a foundation for predictive modeling. Potential next steps include:
- Model Development: Build regression models to predict insurance costs
- Feature Engineering: Create additional derived features
- Model Evaluation: Compare different algorithms (Linear Regression, Random Forest, etc.)
- Hyperparameter Tuning: Optimize model performance
- Deployment: Create a web application for cost prediction
Feel free to fork this repository and contribute improvements to the analysis or add predictive modeling components.
This project is for educational purposes. Please check the dataset source for any licensing restrictions.