Binary encoding is a type of categorical encoding, transforms categorical data into a binary representation.(0 and 1). This is suitable for machine learning. This technique is useful when you deal with binary feature or when you a want to reduce the number of columns. Simple example Say you have a gender feature which contains two classes like male and female. In binary encoding male will be represented by 1 and female will be represented by 1.(vice versa).

Tasks involved:

  1. Identify the Categorical column where you want to encode the classes into binary code
  2. Define the mapping: Assign 0 to one category(female) and assign 1 to another category Male
  3. Replace Male and Female with 1 and 0
  4. Convert into numerical type the categories using dtype as int

Binary encoding

  • Helps you to convert the categorical features into numerical for easier visualization and analysis
  • Machine learning model supports only numerical values
  • Binary encoding removes the Dimensionality Curse and capture the relevant info.

Enables you to explore the relationship between the target variable and the features with ease after the conversion to numerical form

Load libraries

Read data

Gender has two classes Male and female. Since ML accepts only numerical value for further learning and analysis let us encode the above using Binary encoder

Map

Female is mapped to 1 and male is mapped 0

Using LabelEncoder encode the other two features card and type

Now the dataset is ready for further analysis

Separate Target variable and other features

Statesmodels.api Ordinary Least Square Method

Summary

Install category_encoders  using pip install category_encoders. Now import BinaryEncoders

BinaryEncoder Method

gender feature is removed and two columns gender_0 (Male) and gender_1 (female)  are created using binary.