Dive into the heart of data science with a project that combines healthcare insights and predictive analytics. As a Data Scientist at a top Health Insurance company, you have the opportunity to predict customer healthcare costs using the power of machine learning. Your insights will help tailor services and guide customers in planning their healthcare expenses more effectively.
Dataset Summary
Meet your primary tool: the insurance.csv
dataset. Packed with information on health insurance customers, this dataset is your key to unlocking patterns in healthcare costs. Here's what you need to know about the data you'll be working with:
insurance.csv
Column | Data Type | Description |
---|---|---|
age | int | Age of the primary beneficiary. |
sex | object | Gender of the insurance contractor (male or female). |
bmi | float | Body mass index, a key indicator of body fat based on height and weight. |
children | int | Number of dependents covered by the insurance plan. |
smoker | object | Indicates whether the beneficiary smokes (yes or no). |
region | object | The beneficiary's residential area in the US, divided into four regions. |
charges | float | Individual medical costs billed by health insurance. |
A bit of data cleaning is key to ensure the dataset is ready for modeling. Once your model is built using the insurance.csv
dataset, the next step is to apply it to the validation_dataset.csv
. This new dataset, similar to your training data minus the charges
column, tests your model's accuracy and real-world utility by predicting costs for new customers.
Let's Get Started!
This project is your playground for applying data science in a meaningful way, offering insights that have real-world applications. Ready to explore the data and uncover insights that could revolutionize healthcare planning? Let's begin this exciting journey!
# Import required libraries
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
# Loading the insurance dataset
insurance_data_path = 'insurance.csv'
insurance = pd.read_csv(insurance_data_path)
insurance.head()
# Check for missing values and data types
insurance.info()
# Check for duplicates
insurance[insurance.duplicated()]
# Drop duplicates
insurance.drop_duplicates(inplace=True)
# Confirm duplicates have been dropped successfully
assert any(insurance.duplicated()) == False
# Check missing values
insurance.isna().sum()
# Drop missing values
insurance.dropna(inplace=True)
insurance.isna().sum()
# Convert column data types to appropriate ones
insurance['age'] = insurance['age'].astype(int)
insurance['children'] = insurance['children'].astype(int)
insurance['charges'] = insurance['charges'].str.strip('$').astype('float')
insurance['charges'] = insurance['charges'].fillna(0)
insurance['charges'].isna().sum()
# Check unique values in sex column
insurance['sex'].unique()
# Substitute values with appropriate ones
insurance['sex'] = insurance['sex'].replace('woman', 'female')
insurance['sex'] = insurance['sex'].replace('F', 'female')
insurance['sex'] = insurance['sex'].replace('man', 'male')
insurance['sex'] = insurance['sex'].replace('M', 'male')
# Confirm conversion has taken place
assert insurance['sex'].nunique() == 2
# Inspect smoker column
insurance['smoker'].unique()
# Inspect region column
insurance['region'].unique()
# Correct inconsistent values
insurance['region'] = insurance['region'].str.lower()
insurance['region'].unique()
def encode_cols(df):
cols = [col for col in df.columns if df[col].dtype == 'object']
dummies = pd.get_dummies(df, columns=cols, drop_first=True)
df = pd.concat([df, dummies], axis=1)
df.drop(columns=cols, inplace=True)
return df