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
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
# Loading the insurance dataset
insurance_data_path = 'insurance.csv'
insurance = pd.read_csv(insurance_data_path)
print(insurance.head())
print("\n")
print(insurance.info())
print("\n")
print(insurance.isnull().sum())
print("\n")
for col in insurance.columns:
print(f"{col}: {insurance[col].unique()}")# Implement model creation and training here
# Use as many cells as you need
# Data cleaning
insurance = insurance[insurance['age'] > 0]
insurance['sex'] = insurance['sex'].replace({'M': "male",
'man': 'male',
'F': 'female',
'woman': 'female'})
insurance['charges'] = insurance['charges'].replace({'\$': ''}, regex=True).astype(float)
insurance = insurance[insurance['age'] > 0]
insurance.loc[insurance['children'] < 0.0, 'children'] = 0.0
insurance['region'] = insurance['region'].str.lower()
insurance = insurance.dropna()# Model development
X = insurance.drop("charges", axis=1)
y = insurance["charges"]
X_categorical = pd.get_dummies(X[['sex', 'smoker', 'region']], drop_first=True)
X_processed = pd.concat([X[['age', 'bmi', 'children']], X_categorical], axis=1)
scaler = StandardScaler()
linreg = LinearRegression()
X_scaled = scaler.fit_transform(X_processed)
steps = [("scaler", scaler), ("linear_regression", linreg)]
pipeline = Pipeline(steps)
pipeline.fit(X_scaled, y)
mse_scores = -cross_val_score(pipeline, X_scaled, y, cv=5, scoring='neg_mean_squared_error')
r2_scores = cross_val_score(pipeline, X_scaled, y, cv=5, scoring='r2')
mean_mse = np.mean(mse_scores)
r2_score = np.mean(r2_scores)
print(f"Mean MSE Score: {mean_mse}\nMean R-squared Score: {r2_score}")# Predictions
validation_data = pd.read_csv("validation_dataset.csv")
validation_transform = pd.get_dummies(validation_data, columns=['sex', 'smoker', 'region'], drop_first=True)
prediction = pipeline.predict(validation_transform)
validation_data['predicted_charges'] = prediction
validation_data.loc[validation_data['predicted_charges'] < 1000, 'predicted_charges'] = 1000
validation_data.head()