Simplifying Machine Learning model development with ColumnTransformer and Pipeline

This article explains the usage of ColumnTransformer and Pipeline classes of Scikit-Learn to simplify the process of developing and deploying a machine learning model.

KSV Muralidhar
Towards Data Science

--

Image by Chris Ried on Unsplash

Introduction to ColumnTransformer

ColumnTransformer enables us to transform a specified set of columns. It helps us to apply multiple transforms to multiple columns with a single fit() or fit_transform() statement. For example, we can mean impute the first column and one hot encode the second column of a data frame with a single fit() or fit_transform() statement. ColumnTransformer class can be imported from ‘sklearn.compose’ module as shown below.

Let’s dive deep into ColumnTransformer by looking at an example. Consider the data frame below where we have to one hot encode ‘col1’ and ordinal encode ‘col2’.

Image by author

Conventionally, we create an instance of OneHotEncoder class and fit it to the data frame and transform it, as shown below.

Image by author

The above step has one hot encoded both the columns of the data frame, which is not what we wanted to do. This problem can be solved by using ColumnTransformer. Let’s create a data frame and transform its columns using ColumnTransformer.

Image by author
Image by author

As seen above, we could perform multiple transforms on multiple columns with a single fit_transform() statement. This operation would have been complex without ColumnTransformer. In the above example, we created an instance of ColumnTransformer class and passed an argument named ‘transformers’ which is a list/tuple of transforms we want to perform.

Each transform in a list/tuple of transforms has three parts:

  1. Name of the transform, which can be any name, but each transform in the list of transforms must have a unique name.
  2. The transform itself.
  3. A list of columns to which you want to transform. It should be a list even if you have to apply it to a single column.

Let’s work with another example. Consider the data frame below where we have to one hot encode ‘col1’ and keep the ‘col2’ as it is.

Image by author
Image by author

Where is ‘col2’ in the output?

A ColumnTransformer returns only the columns which it has transformed. In the previous example, we have transformed both the columns, hence, both of them were returned. But in the current example, we’ve transformed only a single column (‘col1’) so only the transformed ‘col1’ is returned. To avoid this, we need to pass an argument ‘remainder=passthrough’, as shown below, the default value of this argument is ‘drop’ which drops the other columns.

Image by author
Image by author

Limitations of ColumnTransformer

There are a few limitations of ColumnTransformer which are discussed below.

  1. ColumnTransformer outputs an array even if we input a DataFrame object which makes it difficult to keep a track of the columns.
  2. In a ColumnTransformer, we cannot apply multiple transforms to a single column as shown below.

Consider the data frame below, where we have to mode impute and one hot encode ‘col1’ and median impute ‘col2’. Here we are trying to apply multiple transforms to a single column (‘col1’).

Image by author
Image by author

We got an error saying ‘Input contains NaN’. Why is ‘NaN’ present even after imputing the ‘col1’ in the first step of the above ColumnTransformer?

It is because the ColumnTransformer takes the columns directly from the input data frame/array in each step. Output of one step is not an input to the next step. So, the second step of one hot encoding took the ‘col1’ from the input data frame, not from step 1. Let’s look at another example. In the data frame below, we’ll try to ordinal encode and min-max scale ‘col1’ and median impute ‘col2’.

Image by author
Image by author

Again there is an error saying ‘could not convert string to float: ‘a’’ even after adding an ordinal encoder in step1. To tackle this problem, let’s know something about Pipeline.

Introduction to Pipeline

Pipeline is a sequence of operations, where output of one step becomes the input to its subsequent step. Pipeline class can be imported from sklearn.pipeline module as shown below.

In the example below, we’ll first median impute the columns of the data frame and then min-max scale them using Pipeline.

Image by author
Image by author

As seen above, we could perform multiple transforms with a single fit() or fit_transform() statement. Unlike ColumnTransformer, Pipelines follow a sequential process, where output of a previous step becomes an input to the next step. Similar to ColumnTransformer, Pipeline takes an input of steps, which can be a classifier, transform, etc. Each step in Pipeline has two parts:

  1. Name of step.
  2. The operation itself.

Without using a Pipeline, the above operation would have been performed using the conventional process as shown below.

Image by author
Image by author

The conventional process took two fit_transform() statements to transform the columns, whereas, Pipeline took it down to one.

Limitations of Pipeline

  1. Similar to ColumnTransformer, Pipeline also outputs an array.
  2. As in ColumnTransformer, we cannot specify a column which we want to transform.

Using ColumnTransformer in conjunction with Pipeline

As seen in one of the examples previously, we couldn’t apply multiple transforms to a single column using ColumnTransformer. We got an error while trying it. Let’s discuss the example once again. Consider a data frame below, where we have to mode impute and one hot encode ‘col1’ and median impute ‘col2’. Here we are trying to apply two transforms to a single column (‘col1’).

Image by author
Image by author

What happened above?

As discussed above, in the pipeline (‘col1_pipe’), the output of the first step (‘mode_col1’) became an input to the second step (‘one_hot_encode’). Then we passed the pipeline as an input to ColumnTransformer (‘col_transform’), where these sequence of steps are applied to ‘col1’ and median transform is applied to ‘col2’.

Thus, using ColumnTransformer in conjunction with Pipeline simplifies both the model development and deployment process and also reduces the size of the code.

Know more about my work at https://ksvmuralidhar.in/

--

--

Data Science | ML | DL | NLP | CV | Web scraping | Kaggler | Python | SQL | Excel VBA | Tableau | About Me: https://ksvmuralidhar.in/