Categorical Variables Dummy Coding

Converting categorical variables into numerical dummy coded variable is generally a requirement in machine learning libraries such as Scikit as they mostly work on numpy arrays.

In this blog, let’s look at how we can convert bunch of categorical variables into numerical dummy coded variables using four different methods-

  1. Scikit learn preprocessing LabelEncoder
  2.  Pandas getdummies
  3. Looping
  4. Mapping

We will work with a dataset from IBM Watson blog as this has plenty of categorical variables. You can find the data here.  In this data, we are trying to build a model to predict “churn”, which has two levels “Yes” and “No”.

We will convert the dependent variable using Scikit LabelEncoder and the independent categorical variables using Pandas getdummies. Please note that LabelEncoder will not necessarily create additional columns, whereas the getdummies will create additional columns in the data. We will see that in the below example-


Here are few other ways to dummy coding-


Here is an excellent Kaggle Kernel for detailed feature engineering.


3 thoughts on “Categorical Variables Dummy Coding

  1. Pingback: Learn Python Step by Step | RP's Blog on data science

  2. Pingback: Logistic Regression using Scikit Python | RP's Blog on data science

  3. Pingback: Decision Tree using Python Scikit | RP's Blog on data science

This site uses Akismet to reduce spam. Learn how your comment data is processed.