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!
# Re-run this cell
# Import required libraries
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
# Loading the insurance dataset
insurance_data_path = 'insurance.csv'
insurance = pd.read_csv(insurance_data_path)
insurance.head()from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipelinescaler = StandardScaler()
insurance_model = LinearRegression()Clean the data.
We will drop missing values for this model.
We will also make sure only appropriate entries appear in 'sex', 'region', and 'smoker'
insurance.dtypes#copy our dataframe into a new one which will be cleaned
df_insurance_prepped = insurance.copy()
df_insurance_prepped['sex'] = df_insurance_prepped['sex'].str.lower()
df_insurance_prepped['sex'] = df_insurance_prepped['sex'].replace({'f':'female','m':'male','man':'male','woman':'female'})
df_insurance_prepped['region'] = df_insurance_prepped['region'].str.lower()
df_insurance_prepped['smoker'] = df_insurance_prepped['smoker'].str.lower()
df_insurance_prepped['smoker'] = df_insurance_prepped['smoker'].fillna(df_insurance_prepped['smoker'].mode()[0])
df_insurance_prepped['children'] = df_insurance_prepped['children'].fillna(df_insurance_prepped['children'].mode()[0]).astype('int')
df_insurance_prepped['charges'] = df_insurance_prepped['charges'].str.replace("$",'').astype('float')
df_insurance_prepped['bmi'] = df_insurance_prepped['bmi'].fillna(df_insurance_prepped['bmi'].mean())
df_insurance_prepped.dropna(subset=['age','sex','region','charges'], inplace=True)
Ensure we now have the data in their correct types
This can all be done in a function but will be done line by line here.
df_insurance_prepped.dtypesdf_insurance_prepped.isna().sum()Scale numeric data
df_insurance_prepped['age'] = scaler.fit_transform(df_insurance_prepped['age'].values.reshape(-1,1))
df_insurance_prepped['bmi'] = scaler.fit_transform(df_insurance_prepped['bmi'].values.reshape(-1,1))
df_insurance_prepped['children'] = scaler.fit_transform(df_insurance_prepped['children'].values.reshape(-1,1))
Turn categorical values into their own columns.