ONE HOT ENCODING
This topic comes under the DATA PRE-PROCESSING step which is done while creating the machine learning model.
If in any case, you don’t know about DATA PRE-PROCESSING step. No problem i will share my story about.
HERE IS THE LINK: - DATA PRE-PROCESSING.
So, Let’s get started.
WHAT TOU WILL LEARN: -
- WHY WE USE ONE HOT ENCODING?
- WHAT IS ONE HOT ENCODING?
Let’s get started.
Let’s say we want to predict the price of the house in the near future. So we are going to create a machine learning model.
We know that the machine needs the dataset as an input.
In the above simple dataset, you can see the independent variable and dependent variable.
INDEPENDENT VARIABLE: - TOWN, AREA, FLOOR COUNT.
DEPENDENT VARIABLE: - PRICE.
You can also see that the town column having categorical features(values) in text. We know that the machine learning model can do computation only on Numerical representation.
That means we have to “encode” this text.
ENCODING: - converting a data from one form to another with help certain techniques or algorithms.
Some of the encoding techniques in machine learning are: -
- One Hot Encoding.
- Label Encoding.
- Ordinal Encoding.
In our dataset, we are going to use the One Hot Encoding. You may ask why we are not using the other Encoding methods.
Label Encoding: - It converts the categorical features (values) into numbers and arrange them in ascending or descending order.
You can see that the column (Town). After the Encoding, the previous values present in there is assigned to the numbers.
- PICADILLY = 1
- MAYFAIR = 2
- OXFORD CITY = 3.
So, these are the values assigned to it.This make the machine learning model learn from it.
Get VIJAY RAGUL’s stories in your inbox
Join Medium for free to get updates from this writer.
If it does it’s work perfectly. Then why we are not using this Encoding technique. This is because the model will think that the town is ordered wise arranged and it make assumption that the Picadilly (1) is lesser than Mayfair (2) and oxford city (3) is greater than Mayfair (2). So, this type of info is not needed for our model. Of course, there is need of using Label Encoder in other machine learning model.
OKAY, LET’S TALK ABOUT OUR HERO (ONE HOT ENCODER).
In here we work differently, so that the machine learning model can learn the right information.
ONE HOT ENCODING:
What we do in here is we create an extra column (dummy variable). Which denote whether the feature (value) belong to certain row by using binary digits.
Don’t worry the pictorial representation can help you.
The image consists of a ONE HOT ENCODED dataset. You can see that the extra new columns are added in here and you can see that the number of new columns is equal to the number of categorical features in the TOWN column. This gives the model the perfect info of what “city” the “row” belongs to.
But it needs some tunning before the dataset is given as an input to the machine learning model.
WHY WE NEED TO TUNE?
- DUMMY VARIABLE TRAP?
This is a trap that take place when we create dummy variable.
WAIT A MINIUTE.DID I TOLD YOU WHAT IS DUMMY VARIABLE?
Dummy variable is nothing but the extra columns that we created when we ONE HOT ENCODE.
We can clearly see that if we know that if (1) is present in the piccadilly column then we don’t have to see the other columns and vice versa. We know that the row is piccadilly.
To understand about this, we have dive into the mathematical world predictive model.
This is a separate topic. It will take some time so i made a story about it seperatly
So, I made my rule of only to have “(n-1)” number of DUMMY VARIABLE IN MY DATASET.
THERE ARE TWO KEY CHANGES TAKEN HERE.
1.In the above dataset, you can see that the column “PICADILLY” is removed. This is because when we see the value of MAYFAIR (0) and OXFORD CITY (0) we can conclude that the row belongs to the PICADIILY.
2.We removed the “TOWN” column because the DUMMY VARIABLE itself gives the information which city it is.
SO, THAT’S ALL ABOUT ONE ENCODING. I BELIEVE I DID MY BEST IN SIMPLIFYING THIS.
WHAT YOU LEARNED:
- WHAT IS ONE HOT ENCODING
- WHAT LABEL ENCODING DO
- THE RULE TO FOLLOW TO ESCAPE FROM THE DUMMY VARIABLE TRAP.
THE CODE TO IMPLEMENT THIS ONE HOT ENCODING IN THE DATASET AND THE EXLANATION WILL BE CLEAR IN THE NEXT STORY…
I BELIVE YOU FOUND VALUE IN THIS STORY.
THANK YOU FOR SPENDING YOUR TIME WITH ME…









