Cleaning PredictHealth's Customer Database

Introduction And Lesson Overview

Welcome back! In the last lesson, you learned how to build a complete insurance cost prediction model using both numerical and categorical features. You also practiced encoding categorical variables and building a modeling pipeline. Now, you are ready to take the next step: preparing your data for real-world challenges.

In practice, data is rarely perfect. Customer databases often contain missing values, outliers, duplicates, and inconsistencies. If these issues are not addressed, your models may produce unreliable results or even fail to run. That is why data cleaning is a critical step in any data science project.

In this lesson, you will learn how to clean PredictHealth's customer database so it is ready for modeling. You will inspect the data for problems, handle missing values, remove duplicate records, detect and treat outliers, and normalize numerical features. By the end, you will have a clean dataset that is ready for building robust predictive models. This lesson will build directly on your previous work, but with a focus on making your data as reliable as possible.

Inspecting The Messy Dataset

Before you can clean your data, you need to know what problems exist. In real-world datasets, it is common to find missing values and outliers. Missing values are simply empty cells in your data, while outliers are values that are unusually high or low compared to the rest of the data.

Let's look at an example. Suppose you have a copy of the insurance data, but it has been made "messy" for demonstration purposes. This messy dataset has missing values added to all columns (about 6% missing rate) and some extreme outliers in the charges and bmi columns.

Here is how you can check for missing values in the dataset:

Python
print("Checking for missing values:")
print(messy_data.isnull().sum())

The output might look like this:

plaintext
Checking for missing values:
age         78
sex         82
bmi         85
children    79
smoker      84
region      77
charges     86
dtype: int64

This tells you how many missing values are in each column. Even a few missing values can cause problems for your model, especially if they are in important columns. Notice that in this messy dataset, all columns have missing values, which is common in real-world scenarios.

Outliers are another issue. The messy dataset contains some extreme outliers: insurance charges ranging from $100,000 to $200,000, and BMI values between 50 and 70. These values are far outside the normal range and can distort your model's understanding of the data, leading to poor predictions.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal