One-hot Encoding in Machine Learning

Outcome School · Beginner ·📐 ML Fundamentals ·1y ago

About this lesson

In this video, we discussed the one-hot encoding technique used in machine learning. We also discussed when to use it and when not to. #machinelearning #datascience Software engineers like you join Outcome School to achieve the outcome that is a high-paying tech job. Join Outcome School and get a high-paying tech job: https://outcomeschool.com Founder of Outcome School: - Amit Shekhar About Amit Shekhar: Founder @ Outcome School • IIT 2010-14 • I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos. Connect with Amit Shekhar: - X: https://x.com/amitiitbhu - LinkedIn: https://www.linkedin.com/in/amit-shekhar-iitbhu - GitHub: https://github.com/amitshekhariitbhu - YouTube: https://www.youtube.com/@amitshekhar Follow Outcome School: - X: https://x.com/outcome_school - LinkedIn: https://www.linkedin.com/company/outcomeschool - YouTube: https://youtube.com/@OutcomeSchool - GitHub: https://github.com/OutcomeSchool

Full Transcript

Hey guys and welcome to outcome school and in this video we are going to discuss an encoding technique which is called one hot encoding which is used in machine learning. So let's start with the learning and for that we need to understand why encoding is needed in the first place. So here we have the data set. So basically what we need to do based on the input that we have employee performance data. So this is the input that we have right which is used to predict if an employee will be promoted or not. So here is the output which is the labeled one right and here is the feature which is the input feature. So these are the input feature that we will feed in the model to train the model. So for that what we need to understand first is that so the performance rating here right. So this column is a text data which is in the string format as you can see here right fair poor good excellent. So we need to observe one thing here is that most machine learning algorithm operate on a numerical data as they can easily perform the mathematical operations like matrix multiplications like distance calculations right. So all those things can be easily performed if we have the data in the numerical format. So for that only what we need to do we need to convert this data into the numerical format. But here we have the data in the text data. So we need to convert that. So conversion of the text data into the numerical data is called encoding in this case. So encoding converts the text into the numerical representation. So that is why we need the encoding so that we can simply operate on those numbers. And if you closely observe here, so the column which is the performance rating has some order in terms of relationship, right? And based on the column that we have performance rating, we can put the category in this order. Excellent is greater than very good, very good is greater than good, good is greater than fair, and fair is greater than poor. Like that we can simply put. So because we can order the data in this way we have a relationship here. So because of that we can simply assign some numbers to those category. As you can see here the performance rating can be encoded as integers 1 to 5 and we can preserve the order simply like this 1 2 5 is greater than four four greater than three 2 and 1 so on so right. So like this we can do here because we have some ordering in the data right because of that only we can put the numbers like this. So ultimately we will be able to convert our feature performance rating from text into the numerical representation. So after conversion our input data set the feature input will look like this as you can see. So here we can simply convert the textual into the numerical representation as you can see. So this is the final input that we will feed in the machine learning model as you can see right. So till now we have understood why we need to do the encoding in the first place. So basically it will help the model in doing the mathematical operations easily. So for that matter we need to do the encoding in the first place and before jumping into the one not encoding we first need to understand the ordinal versus nominal data. So let's try to understand here. So here we have the ordinal data and here we have the nominal data. And if you observe this you can see clearly that there is some relation between these category poor, fair, good, very good and excellent. So excellent is greater than good. Good is greater than poor. Like that we can say and because of that we can actually give the numbering 1 2 3 4 5. But here we don't have as such. We don't have anything as such here. So we cannot say that documentary is greater than comedy or action is greater than drama. We cannot say like that. Right? So the data in which we have some order some relationship there we will say this is the ordinal data and when we don't have any relationship as such then we will say nominal data. So for the ordinal data we have already seen we can simply do the normal encoding that we have seen 1 2 3 4 5 like that. But for now we need to understand how we can do the encoding for the nominal data because we don't have any order any relation as such in that. So now let's try to see one more example where we have the nominal data not the ordinal data. Let's try to see that here we have customer preferences data used to predict if a customer will subscribe or not. So let's say some website some video is there right? Some video platform on that the customer will subscribe or not like that right? So here we have the age and then we have the monthly watch hours and then we have the favorite category right similar to that right comedy drama and so on so and then the output which is labeled one which is the subscribe yes or no like that right so here if you again closely observe the favorite category is the text format. So again we need to do some encoding but we cannot do the similar encoding that we have learned as of now. So the favorite category here is a nominal data. It is not the ordinal data. And because of that we don't have any relation here. So we can't do 1 2 3 4 5 like that if we do like that. Let's try to understand what will happen. So there is no meaningful order between the data that we have. So for example, we assign the number 1 2 3 4 5 like this. Right? So assigning integers value to them can be misleading and we will see why as the categories are unrelated. So the machine learning models may assume drama is greater than the comedy which can lead to incorrect prediction. So this is the major issue here. So if we have the nominal data we cannot assign the numbers as we have learned in case of the ordinal data. So that encoding is not going to work here at all. So for that only now we will introduce the one hot encoding technique which is the center of this video and this is the right time to discuss the one hot encoding. So one hot encoding is a technique used in machine learning once again to convert the text data into the numerical format but here we do the things differently that we will try to understand now. And as you can see that we have the nominal data and for that what we will do we will do the encoding differently and for that only we are learning the one hot encoding. So what we do here we will create the columns and the number of columns will be equal to the number of types in that category that we have right. So for each category that we have comedy drama and so on so right we have five category here. So we will create five columns. We will create five columns in our case like this. 1 2 3 4 5. Ultimately also in the data set also we will have five columns. Now instead of one we will have five columns like this. So what we do? So for example our value is comedy right? So for this what we will do? We will do the one hot encoding and this will create the data like this. So for the comedy we will fill one here and for the other columns we will fill simply zero right like this. So basically what we do we create columns for each category and place one in the column corresponding to the value it had and zero elsewhere. So for the comedy category it will come out like this. So for other category we can see here. So for example for this we have the drama category right. So zero in the drama column we have one otherwise we have zero. Similarly in the place where we have the value is equal to documentary in that we will have the zero zero in all the category and in the documentary we will have one as you can see right. So we create n number of columns where n is equal to number of types in that category and simply we have converted the text data into the numerical representations and that is what we call it as a one hot encoding. So it is called one hot as exactly one element is one others are zero. So that is why we call it as a one hot encoding. So what we have learned today is that if we have the ordinal data it means we have some relations ordering. So in that case we can simply go with the basic encoding 1 2 3 4 5 as we have seen in the previous example. But if in case if we have the nominal data in it means we don't have any relationship as such no ordering as such. So in that case we can use the one hot encoding in the first place but it has also some limitations that we will try to understand now. So one hot encoding is not recommended for the features with too many values. Too many values will create highdimensional sparse matrix which will consume more memory and the processing power. So for example we have the country one column right in that we can have 200 different different countries as you can see right 200 different different countries. So if we do one hot encoding here we will create 200 columns and in 199 columns we will feed zero and in only one column we are going to feed one. So it means that we are creating highdimensional sparse matrix and that is going to consume more memory and the processing power obviously. So that is why it is not recommended to use the one hot encoding where we have the features with too many values. So for this we will use some different encoding and that we will cover in the future videos. So this was all about the one hot encoding technique that we use in the machine learning. So that's it for now. I will see you in the next video.

Original Description

In this video, we discussed the one-hot encoding technique used in machine learning. We also discussed when to use it and when not to. #machinelearning #datascience Software engineers like you join Outcome School to achieve the outcome that is a high-paying tech job. Join Outcome School and get a high-paying tech job: https://outcomeschool.com Founder of Outcome School: - Amit Shekhar About Amit Shekhar: Founder @ Outcome School • IIT 2010-14 • I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos. Connect with Amit Shekhar: - X: https://x.com/amitiitbhu - LinkedIn: https://www.linkedin.com/in/amit-shekhar-iitbhu - GitHub: https://github.com/amitshekhariitbhu - YouTube: https://www.youtube.com/@amitshekhar Follow Outcome School: - X: https://x.com/outcome_school - LinkedIn: https://www.linkedin.com/company/outcomeschool - YouTube: https://youtube.com/@OutcomeSchool - GitHub: https://github.com/OutcomeSchool
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
ML Anomaly Detection in Loki Logs: Per-Entity Isolation Forest
Learn to implement ML anomaly detection in Loki logs using Per-Entity Isolation Forest for more accurate and efficient log analysis
Dev.to · Oleksandr Kuryzhev
📰
The First Python Program That Finally Clicked
Learn to build a simple Python program that takes user input and uses variables to produce a meaningful output, a great starting point for beginners
Medium · Programming
📰
The Dirty Secret Behind Robotics Vision: Why Data (Not Algorithms) Is the Real Bottleneck — And Why…
Robots fail due to bad data, not bad code, and Vision-Language Models (VLMs) aim to address this data crisis
Medium · Data Science
📰
Machine Learning Concepts Explained #5: Training, Validation, and Testing Datasets
Learn how to split data into training, validation, and testing datasets to build reliable machine learning models
Medium · Machine Learning
Up next
Dropout in Deep Learning
AnuTech-CH
Watch →