These scripts are intended for supporting Data Cleanse – the process of detecting and correcting (or removing) corrupt or inaccurate records from a record set, table, or database and refers to identifying incomplete, incorrect, inaccurate or irrelevant parts of the data and then replacing, modifying, or deleting the dirty or coarse data. Data cleansing may be performed interactively with data wrangling tools, or as batch processing through scripting.

The steps and techniques for data cleaning will vary from dataset to dataset. As a result, it’s impossible for a single guide to cover everything you might run into. However, this guide provides a reliable starting framework that can be used every time. We cover common steps such as fixing structural errors, handling missing data, and filtering observations.

Better data beats fancier algorithms.

In other words… garbage in gets you garbage out. If you have a properly cleaned dataset, even simple analysis can learn impressive insights from the data!

Remove Duplicate observations
Duplicate observations most frequently arise during data collection, such as when you:

  1. Combine datasets from multiple places
  2. Scrape data
  3. Receive data from clients/other departments

Removing Irrelevant observations

Irrelevant observations are those that don’t actually fit the specific problem that you’re trying to solve.

For example, if you were building a model for Single-Family homes only, you wouldn’t want observations for Apartments in there.
This is also a great time to review your charts from Exploratory Analysis. You can look at the distribution charts for categorical features to see if there are any classes that shouldn’t be there.
Checking for irrelevant observations before engineering features can save you many headaches down the road.

Fix Structural Errors

Structural errors are those that arise during measurement, data transfer, or other types of “poor housekeeping.”

For instance, you can check for typos or inconsistent capitalisation. This is mostly a concern for categorical features, and you can look at your bar plots to check.

Here’s an example:

Structural errors before
As you can see:

‘composition’ is the same as ‘Composition’
‘asphalt’ should be ‘Asphalt’
‘shake-shingle’ should be ‘Shake Shingle’
‘asphalt,shake-shingle’ could probably just be ‘Shake Shingle’ as well
After we replace the typos and inconsistent capitalisation, the class distribution becomes much cleaner:

Structural errors after
Finally, check for mislabeled classes, i.e. separate classes that should really be the same.

e.g. If ’N/A’ and ’Not Applicable’ appear as two separate classes, you should combine them.
e.g. ’IT’ and ’information_technology’ should be a single class.

Filter Unwanted Outliers

Outliers can cause problems with certain types of models. For example, linear regression models are less robust to outliers than decision tree models. In general, if you have a legitimate reason to remove an outlier, it will help your model’s performance. However, outliers are innocent until proven guilty. You should never remove an outlier just because it’s a “big number.” That big number could be very informative for your model. We can’t stress this enough: you must have a good reason for removing an outlier, such as suspicious measurements that are unlikely to be real data.

Handle Missing Data (Data Fix)

Missing data is a deceptively tricky issue in applied machine learning. First, just to be clear, you cannot simply ignore missing values in your dataset. You must handle them in some way for the very practical reason that most algorithms do not accept missing values.

“Common sense” is not sensible here
Unfortunately, from our experience, the 2 most commonly recommended ways of dealing with missing data actually suck.

They are:

  1. Dropping observations that have missing values
  2. Imputing the missing values based on other observations
  3. Dropping missing values is sub-optimal because when you drop observations, you drop information.

The fact that the value was missing may be informative in itself.
Plus, in the real world, you often need to make predictions on new data even if some of the features are missing!
Imputing missing values is sub-optimal because the value was originally missing but you filled it in, which always leads to a loss in information, no matter how sophisticated your imputation method is.

Again, “missingness” is almost always informative in itself, and you should tell your algorithm if a value was missing.
Even if you build a model to impute your values, you’re not adding any real information. You’re just reinforcing the patterns already provided by other features.

  1. Missing data puzzle pieces
  2. Missing data is like missing a puzzle piece. If you drop it, that’s like pretending the puzzle slot isn’t there. If you impute it, that’s like trying to squeeze in a piece from somewhere else in the puzzle.

In short, you should always tell your algorithm that a value was missing because missingness is informative.

Missing categorical data

The best way to handle missing data for categorical features is to simply label them as ’Missing’!

  1. You’re essentially adding a new class for the feature.
  2. This tells the algorithm that the value was missing.
  3. This also gets around the technical requirement for no missing values.

Missing numeric data

For missing numeric data, you should flag and fill the values.

  1. Flag the observation with an indicator variable of missingness.
  2. Then, fill the original missing value with 0 just to meet the technical requirement of no missing values.

By using this technique of flagging and filling, you are essentially allowing the algorithm to estimate the optimal constant for missingness, instead of just filling it in with the mean.

Data Science Article on Data Cleaning

Revision: 3
Last modified: Jun 27, 2019

Feedback

Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.

Post your comment on this topic.

Post Comment