This project leverages supervised learning techniques to analyze a medical cost dataset. It aims to both predict continuous medical charges and classify costs into distinct categories.
The core of this project involves:
- Data Loading and Exploration: Initial steps include loading the dataset and performing exploratory data analysis (EDA) with various visualizations to understand the data's characteristics.
- Model Building: Developing and evaluating machine learning models for two distinct tasks: regression and classification.
- Linear Regression (for predicting 'charges')
Purpose: To predict continuous medical charges based on patient attributes like age, BMI, smoker status, and more.
Implementation Details:
A sklearn.pipeline.Pipeline is constructed to streamline the data processing and model training. This pipeline includes:
- Preprocessing:
sklearn.preprocessing.OneHotEncoderfor nominal categorical features (sex, smoker, region).sklearn.preprocessing.StandardScalerfor numerical features (age, BMI, children) to standardize their scale.sklearn.impute.SimpleImputerto handle any missing values in the dataset.
- Model: A
sklearn.linear_model.LinearRegressionmodel trained on the preprocessed data.
Evaluation:
The model's performance is assessed using Mean Squared Error (MSE) and R-squared (R²) on both the training and test datasets.
- Classification Models (for predicting 'charges_group')
Purpose: To categorize medical costs into three distinct groups (low, medium, and high) based on the same patient features.
Implementation Details:
- A new categorical feature,
charges_group, is created by binning the continuous 'charges' into three defined cost categories. - Similar preprocessing steps to the Linear Regression pipeline are applied to the data.
- The following classification algorithms are trained and evaluated:
sklearn.tree.DecisionTreeClassifiersklearn.ensemble.RandomForestClassifiersklearn.linear_model.LogisticRegression
Evaluation: Model performance is evaluated using standard classification metrics, including:
AccuracyPrecisionRecallF1-scoreConfusion matricesare also used to visualize the models' predictive performance for each category.
Sequence diagram illustrating the data flow and model training process:
