How to Perform Data Cleaning for Machine Learning with Python

Last Updated on June 30, 2020

Data cleaning is a critically important step in any machine learning project.

In tabular data, there are many different statistical analysis and data visualization techniques you can use to explore your data in order to identify data cleaning operations you may want to perform.

Before jumping to the sophisticated methods, there are some very basic data cleaning operations that you probably should perform on every single machine learning project. These are so basic that they are often overlooked by seasoned machine learning practitioners, yet are so critical that if skipped, models may break or report overly optimistic performance results.

In this tutorial, you will discover basic data cleaning you should always perform on your dataset.

After completing this tutorial, you will know:

  • How to identify and remove column variables that only have a single value.
  • How to identify and consider column variables with very few unique values.
  • How to identify and remove rows that contain duplicate observations.

Kick-start your project with my new book Data Preparation for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

  • Updated Apr/2020: Added a section on Datasets and the VarianceThreshold.
  • Updated May/2020: Added quotes and book references.
Basic Data Cleaning You Must Perform in Machine Learning

Basic Data Cleaning You Must Perform in Machine Learning
Photo by Allen McGregor, some rights reserved.

Tutorial Overview

This tutorial is divided into seven parts; they are:

  1. Messy Datasets
  2. Identify Columns That Contain a Single Value
  3. Delete Columns That Contain a Single Value
  4. Consider Columns That Have Very Few Values
  5. Remove Columns That Have A Low Variance
  6. Identify Rows that Contain Duplicate Data
  7. Delete Rows that Contain Duplicate Data

Messy Datasets

Data cleaning refers to identifying and correcting errors in the dataset that may negatively impact a predictive model.

Data cleaning is used to refer to all kinds of tasks and activities to detect and repair errors in the data.

— Page xiii, Data Cleaning, 2019.

Although critically important, data cleaning is not exciting, not does it involve fancy techniques. Just a good knowledge of the dataset.

Cleaning up your data is not the most glamourous of tasks, but it’s an essential part of data wrangling. […] Knowing how to properly clean and assemble your data will set you miles apart from others in your field.

— Page 149, Data Wrangling with Python, 2016.

There are many types of errors that exist in a dataset, although some of the simplest errors include columns that don’t contain much information and duplicated rows.

Before we dive into identifying and correcting messy data, let’s define some messy datasets.

We will use two datasets as the basis for this tutorial, the oil spill dataset and the iris flowers dataset.

Want to Get Started With Data Preparation?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Download Your FREE Mini-Course

Oil Spill Dataset

The so-called “oil spill” dataset is a standard machine learning dataset.

The task involves predicting whether the patch contains an oil spill or not, e.g. from the illegal or accidental dumping of oil in the ocean, given a vector that describes the contents of a patch of a satellite image.

There are 937 cases. Each case is comprised of 48 numerical computer vision derived features, a patch number, and a class label.

The normal case is no oil spill assigned the class label of 0, whereas an oil spill is indicated by a class label of 1. There are 896 cases for no oil spill and 41 cases of an oil spill.

You can access the entire dataset here:

Review the contents of the file.

The first few lines of the file should look as follows:

We can see that the first column contains integers for the patch number. We can also see that the computer vision derived features are real-valued with differing scales such as thousands in the second column and fractions in other columns.

This dataset contains columns with very few unique values that provides a good basis for data cleaning.

Iris Flowers Dataset

The so-called “iris flowers” dataset is another standard machine learning dataset.

The dataset involves predicting the flower species given measurements of iris flowers in centimeters.

It is a multi-class classification problem. The number of observations for each class is balanced. There are 150 observations with 4 input variables and 1 output variable.

You can access the entire dataset here:

Review the contents of the file.

The first few lines of the file should look as follows:

We can see that all four input variables are numeric and that the target class variable is a string representing the iris flower species.

This dataset contains duplicate rows that provides a good basis for data cleaning.

Identify Columns That Contain a Single Value

Columns that have a single observation or value are probably useless for modeling.

These columns or predictors are referred to zero-variance predictors as if we measured the variance (average value from the mean), it would be zero.

When a predictor contains a single value, we call this a zero-variance predictor because there truly is no variation displayed by the predictor.

— Page 96, Feature Engineering and Selection, 2019.

Here, a single value means that each row for that column has the same value. For example, the column X1 has the value 1.0 for all rows in the dataset:

Columns that have a single value for all rows do not contain any information for modeling.

Depending on the choice of data preparation and modeling algorithms, variables with a single value can also cause errors or unexpected results.

You can detect rows that have this property using the unique() NumPy function that will report the number of unique values in each column.

The example below loads the oil-spill classification dataset that contains 50 variables and summarizes the number of unique values for each column.

Running the example loads the dataset directly from the URL and prints the number of unique values for each column.

We can see that column index 22 only has a single value and should be removed.

A simpler approach is to use the nunique() Pandas function that does the hard work for you.

Below is the same example using the Pandas function.

Running the example, we get the same result, the column index, and the number of unique values for each column.

Delete Columns That Contain a Single Value

Variables or columns that have a single value should probably be removed from your dataset.

… simply remove the zero-variance predictors.

— Page 96, Feature Engineering and Selection, 2019.

Columns are relatively easy to remove from a NumPy array or Pandas DataFrame.

One approach is to record all columns that have a single unique value, then delete them from the Pandas DataFrame by calling the drop() function.

The complete example is listed below.

Running the example first loads the dataset and reports the number of rows and columns.

The number of unique values for each column is calculated, and those columns that have a single unique value are identified. In this case, column index 22.

The identified columns are then removed from the DataFrame, and the number of rows and columns in the DataFrame are reported to confirm the change.

Consider Columns That Have Very Few Values

In the previous section, we saw that some columns in the example dataset had very few unique values.

For example, there were columns that only had 2, 4, and 9 unique values. This might make sense for ordinal or categorical variables. In this case, the dataset only contains numerical variables. As such, only having 2, 4, or 9 unique numerical values in a column might be surprising.

We can refer to these columns or predictors as near-zero variance predictors, as their variance is not zero, but a very small number close to zero.

… near-zero variance predictors or have the potential to have near zero variance during the resampling process. These are predictors that have few unique values (such as two values for binary dummy variables) and occur infrequently in the data.

— Pages 96-97, Feature Engineering and Selection, 2019.

These columns may or may not contribute to the skill of a model. We can’t assume that they are useless to modeling.

Although near-zero variance predictors likely contain little valuable predictive information, we may not desire to filter these out.

— Page 97, Feature Engineering and Selection, 2019.

Depending on the choice of data preparation and modeling algorithms, variables with very few numerical values can also cause errors or unexpected results. For example, I have seen them cause errors when using power transforms for data preparation and when fitting linear models that assume a “sensible” data probability distribution.

To help highlight columns of this type, you can calculate the number of unique values for each variable as a percentage of the total number of rows in the dataset.

Let’s do this manually using NumPy. The complete example is listed below.

Running the example reports the column index and the number of unique values for each column, followed by the percentage of unique values out of all rows in the dataset.

Here, we can see that some columns have a very low percentage of unique values, such as below 1 percent.

We can update the example to only summarize those variables that have unique values that are less than 1 percent of the number of rows.

Running the example, we can see that 11 of the 50 variables have numerical variables that have unique values that are less than 1 percent of the number of rows.

This does not mean that these rows and columns should be deleted, but they require further attention.

For example:

  • Perhaps the unique values can be encoded as ordinal values?
  • Perhaps the unique values can be encoded as categorical values?
  • Perhaps compare model skill with each variable removed from the dataset?

For example, if we wanted to delete all 11 columns with unique values less than 1 percent of rows; the example below demonstrates this.

Running the example first loads the dataset and reports the number of rows and columns.

The number of unique values for each column is calculated, and those columns that have a number of unique values less than 1 percent of the rows are identified. In this case, 11 columns.

The identified columns are then removed from the DataFrame, and the number of rows and columns in the DataFrame are reported to confirm the change.

Remove Columns That Have A Low Variance

Another approach to the problem of removing columns with few unique values is to consider the variance of the column.

Recall that the variance is a statistic calculated on a variable as the average squared difference of values on the sample from the mean.

The variance can be used as a filter for identifying columns to removed from the dataset. A column that has a single value has a variance of 0.0, and a column that has very few unique values will have a small variance value.

The VarianceThreshold class from the scikit-learn library supports this as a type of feature selection. An instance of the class can be created specify the “threshold” argument, which defaults to 0.0 to remove columns with a single value.

It can then be fit and applied to a dataset by calling the fit_transform() function to create a transformed version of the dataset where the columns that have a variance lower than the threshold have been removed automatically.

We can demonstrate this on the oil spill dataset as follows:

Running the example first loads the dataset, then applies the transform to remove all columns with a variance of 0.0.

The shape of the dataset is reported before and after the transform, and we can see that the single column where all values are the same has been removed.

We can expand this example and see what happens when we use different thresholds.

We can define a sequence of thresholds from 0.0 to 0.5 with a step size of 0.05, e.g. 0.0, 0.05, 0.1, etc.

We can then report the number of features in the transformed dataset for each given threshold.

Finally, we can plot the results.

Tying this together, the complete example of comparing variance threshold to the number of selected features is listed below.

Running the example first loads the data and confirms that the raw dataset has 49 columns.

Next, the VarianceThreshold is applied to the raw dataset with values from 0.0 to 0.5 and the number of remaining features after the transform is applied are reported.

We can see that the number of features in the dataset quickly drops from 49 in the unchanged data down to 35 with a threshold of 0.15. It later drops to 31 (18 columns deleted) with a threshold of 0.5.

A line plot is then created showing the relationship between the threshold and the number of features in the transformed dataset.

We can see that even with a small threshold between 0.15 and 0.4, that a large number of features (14) are removed immediately.

Line Plot of Variance Threshold (X) Versus Number of Selected Features (Y)

Line Plot of Variance Threshold (X) Versus Number of Selected Features (Y)

Identify Rows That Contain Duplicate Data

Rows that have identical data are probably useless, if not dangerously misleading during model evaluation.

Here, a duplicate row is a row where each value in each column for that row appears in identically the same order (same column values) in another row.

… if you have used raw data that may have duplicate entries, removing duplicate data will be an important step in ensuring your data can be accurately used.

— Page 173, Data Wrangling with Python, 2016.

From a probabilistic perspective, you can think of duplicate data as adjusting the priors for a class label or data distribution. This may help an algorithm like Naive Bayes if you wish to purposefully bias the priors. Typically, this is not the case and machine learning algorithms will perform better by identifying and removing rows with duplicate data.

From an algorithm evaluation perspective, duplicate rows will result in misleading performance. For example, if you are using a train/test split or k-fold cross-validation, then it is possible for a duplicate row or rows to appear in both train and test datasets and any evaluation of the model on these rows will be (or should be) correct. This will result in an optimistically biased estimate of performance on unseen data.

Data deduplication, also known as duplicate detection, record linkage, record matching, or entity resolution, refers to the process of identifying tuples in one or more relations that refer to the same real-world entity.

— Page 47, Data Cleaning, 2019.

If you think this is not the case for your dataset or chosen model, design a controlled experiment to test it. This could be achieved by evaluating model skill with the raw dataset and the dataset with duplicates removed and comparing performance. Another experiment might involve augmenting the dataset with different numbers of randomly selected duplicate examples.

The pandas function duplicated() will report whether a given row is duplicated or not. All rows are marked as either False to indicate that it is not a duplicate or True to indicate that it is a duplicate. If there are duplicates, the first occurrence of the row is marked False (by default), as we might expect.

The example below checks for duplicates.

Running the example first loads the dataset, then calculates row duplicates.

First, the presence of any duplicate rows is reported, and in this case, we can see that there are duplicates (True).

Then all duplicate rows are reported. In this case, we can see that three duplicate rows that were identified are printed.

Delete Rows That Contain Duplicate Data

Rows of duplicate data should probably be deleted from your dataset prior to modeling.

If your dataset simply has duplicate rows, there is no need to worry about preserving the data; it is already a part of the finished dataset and you can merely remove or drop these rows from your cleaned data.

— Page 186, Data Wrangling with Python, 2016.

There are many ways to achieve this, although Pandas provides the drop_duplicates() function that achieves exactly this.

The example below demonstrates deleting duplicate rows from a dataset.

Running the example first loads the dataset and reports the number of rows and columns.

Next, the rows of duplicated data are identified and removed from the DataFrame. Then the shape of the DataFrame is reported to confirm the change.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Tutorials

Books

APIs

Summary

In this tutorial, you discovered basic data cleaning you should always perform on your dataset.

Specifically, you learned:

  • How to identify and remove column variables that only have a single value.
  • How to identify and consider column variables with very few unique values.
  • How to identify and remove rows that contain duplicate observations.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Get a Handle on Modern Data Preparation!

Data Preparation for Machine Learning

Prepare Your Machine Learning Data in Minutes

...with just a few lines of python code

Discover how in my new Ebook:
Data Preparation for Machine Learning

It provides self-study tutorials with full working code on:
Feature Selection, RFE, Data Cleaning, Data Transforms, Scaling, Dimensionality Reduction, and much more...

Bring Modern Data Preparation Techniques to
Your Machine Learning Projects


See What's Inside

33 Responses to How to Perform Data Cleaning for Machine Learning with Python

  1. Basavaraj March 20, 2020 at 12:36 pm #

    nice work

  2. MF March 20, 2020 at 4:33 pm #

    Hi Jason,

    May I know the reasons why delete columns that have unique values?
    Please enlighten me. Thanks.

    • Jason Brownlee March 21, 2020 at 8:18 am #

      We delete columns that do not have many unique values because they do not or are likely to not contribute to the target variable.

  3. paolo March 20, 2020 at 7:06 pm #

    Thanks. I am using R and I am considering to learn Python. Do you know if there is a tutorial about this same topic (how to clean datasets) for R?
    Is it worth to combine Python and R?

    • Jason Brownlee March 21, 2020 at 8:19 am #

      I don’t know off hand, sorry.

      I recommend sticking to one platform.

  4. Amina Sani Adamu March 21, 2020 at 2:56 am #

    I really enjoyed the Tutorial.

  5. Derek March 21, 2020 at 11:05 pm #

    Great article sometimes we forget the basics when building ML. Thanks Jason

  6. Mwango March 22, 2020 at 3:35 am #

    Thanks for the tutorial. It’s giving us more options for cleaning.

  7. Brent Kamenka April 19, 2020 at 5:14 pm #

    Good thinking in the tutorial. I like the approach of identification of non – unique data. I work in industrial computing and we tend to have a lot of constant “1 / 0 ” entries for when valves are open and shut, but for a time period, removing a constant value would help identify errors.

    Thanks for the thoughts!

  8. Ismaello April 30, 2020 at 4:09 pm #

    Nice tutorial, thanks. You have a few articles about clean, evaluate features,.. Do you have a book with everything?

  9. Grzegorz Kępisty May 22, 2020 at 4:41 pm #

    Does a process of removing/filling Nan instances belong to data cleaning as well? I guess it shall be performed even before the steps covered by your article.
    Regards!

  10. Priya June 20, 2020 at 8:12 pm #

    Nice tutorial thank you. I am new here and want to ask that do you have any tutorial for the training of ML model by aggregating the data for more than one location?

  11. takwa July 1, 2020 at 7:13 pm #

    Hi,

    I’m working with an image Dataset (my model takes as input two images (an image and its ground truth)). I want to do a data Cleaning for my database. There is no tutorial about how to proceed (all tutorials work with a CSV dataset). Can you please guide me on how to do so. Think you very much for your time.

  12. Anthony The Koala July 11, 2020 at 4:42 pm #

    Dear Dr Jason,
    Thank you for the article.

    I need clarification on the how unique and nunique operate. Do these functions operate row-wise or column-wise.
    Steps needed to replicate.

    I need clarification on the counts. Given that there are 50 features and 937 rows, from the above code, for example what does, 238, 297, 927, 933, 179 mean.
    Does that mean there are 238 counts, or 238 is the unique number?
    Or does that mean the unique number in each of the 50 columns are 238, 297, 927,933, 179.
    Or does that mean for example that in the first column there are 238 unique values?

    Reference
    Example 1, at https://numpy.org/doc/stable/reference/generated/numpy.unique.html

    Thank you,
    Anthony of Sydney

    • Anthony The Koala July 11, 2020 at 5:56 pm #

      Dear Dr Jason,
      I used a smaller dataset and understood it now. Summary appears after background program.
      I understand this. I will ask you after the summary of the count/tally of each unique number.

      Summary:
      unique and DataFrame’s nunique work in the same way.
      Each column produces a count of the unique occurrence of a number by invocation of unique() and df.nunique().
      If unique and df.unique were invoked without the () then we print out the whole matrix.

      Question:
      How do we tally and/or have a frequency of each number in the column.

      Thank you,
      Anthony of Sydney

      • Anthony The Koala July 11, 2020 at 6:06 pm #

        Dear Dr Jason,
        A slight correction.

        unique(stuff) – produces a list of the numbers. It does not produce a count.

        To find out a unique number of numbers from using the unique operator.

        Excuse my pun, but it is not the unique way of determining the number of unique numbers in each column.

        My question remains:
        How do we tally and/or have a frequency of each number in the column?

        Thank you,
        Anthony of Sydney

        • Jason Brownlee July 12, 2020 at 5:48 am #

          You must specify the axis along which to count unique values, perhaps check the documentation for the function.

    • Jason Brownlee July 12, 2020 at 5:44 am #

      Unique values are column wise.

      • Anthony The Koala July 12, 2020 at 3:54 pm #

        Dear Dr Jason,
        I wasn’t clear again.
        Before I proceed with what I intended is that:
        * numpy’s unique and DataFrame’s nunique operate column-wise as I have discovered above.

        My original question was about counting tallies for each feature=column.
        What I want to share is the tally/frequency of unique numbers in a matrix.
        It uses the numpy’s unique function BUT with the parameter numpy.unique(stuff,return_counts = True)

        Here is the code to find a tally of frequency/count of unique numbers in the overall matrix

        Now let’s get a frequency/tally of unique numbers for each feature = column

        Maybe the presentation of the counts/frequencies tally for each feature=column could be tidied up.

        The method above for the whole of matrix or for each column=feature is not unique. The implementation was inspired by code at https://kite.com/python/answers/how-to-count-frequency-of-unique-values-in-a-numpy-array-in-python

        You can get a similar result with counts/frequencies tally for each feature=column

        Source for inspiration: section titled Class collections.Counter at https://docs.python.org/2/library/collections.html

        Conclusions:
        This is NOT about the unique/nunique techniques for determining the counts of features.

        Rather I have presented two techniques for finding the counts/tallies of each column using the unique with parameter return_counts=True while the other uses the collections’ Counter.

        Thank you,
        Anthony of Sydney

  13. Boris July 17, 2020 at 12:26 am #

    Thanks for the article, Jason! I really enjoyed the Tutorial.

  14. Swati Gupta August 23, 2020 at 3:34 am #

    Dear Dr. Brownlee,

    Thank you for nice article. I have a doubt that can we use VarianceThreshold() to remove object data types as well? If not, are there alternative methods of doing it?

    Or do you recommend this step to do after data transform?

    Thanks,
    Swati

    • Jason Brownlee August 23, 2020 at 6:28 am #

      You’re welcome.

      What do you mean object types? E.g. strings? categories?

      Yes, I believe it will work for those data types – at least in principle. Try it and see.

Leave a Reply