Difference between Label Encoding and One Hot Encoding

Data Preprocessing is a really essential step in each Machine studying mannequin creation as a result of the unbiased and dependent options needs to be as linearly aligned as doable i.e. the unbiased options needs to be separated as such that correct affiliation might be made with the goal function such that the mannequin accuracy will get elevated. By Data Preprocessing we imply scaling the information, altering the explicit values to numerical ones, normalizing the information, and many others. Today we will probably be discussing encoding the explicit variables into numeric ones utilizing encoding strategies and can study the variations between the completely different encodings. The programming language that we’re taking right here for reference is Python. Different encoding strategies which might be current for preprocessing the information are One Hot Encoding and Label Encoding. Let us perceive these two, one after the other and attempt to study the distinction between the 2:

Label Encoding

This is an information preprocessing method the place we attempt to convert the explicit column knowledge kind to numerical (from string to numeric). This is finished as a result of our machine studying mannequin doesn’t perceive string characters and due to this fact there needs to be a provision to encode them in a machine-understandable format. This is achieved with the Label Encoding technique. In the Label Encoding technique, the classes current underneath the explicit options are transformed in a way that’s related to hierarchical separation. This signifies that if we’ve got categorical options the place the explicit variables are linked with one another by way of hierarchy then we must always encode these options utilizing Label Encoding. If Label Encoding is carried out on non-hierarchical options then the accuracy of the mannequin will get badly affected and therefore it’s not a good selection for non-hierarchical options.


import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
df['Country']= label_encoder.fit_transform(df[‘Country'])
dummy 1

One Hot Encoding

This can also be an encoding method within the discipline of Machine Learning the place we attempt to convert the explicit string variables to numeric ones. The manner it converts these options to numeric could be very fascinating. It creates dummy variables within the knowledge which corresponds to the explicit variables. This signifies that every categorical function is assigned a dummy column. The dummy columns are nothing however One Hot Vector within the n-dimensional house. This kind of encoding method is greatest fitted to non-hierarchical options the place there is no such thing as a hyperlink of 1 variable with others. We can say that it’s reverse to Label Encoder in the way in which it really works. But, there’s a downside of One Hot Encoding which can also be referred to as Dummy Variable Trap. This signifies that the variables are extremely correlated to one another and results in multicollinearity points. By multicollinearity, we imply dependency between the unbiased options and that could be a drawback. To keep away from this type of drawback we drop one of many dummy variable columns after which attempt to execute our Machine studying mannequin.


import pandas as pd
import numpy as np
df= pd.read_csv("Salary.csv")
dummies= pd.get_dummies(df.Country)
dummy 3


Use Label Encoding when you’ve ordinal options current in your knowledge to get greater accuracy and in addition when there are too many categorical options current in your knowledge as a result of in such eventualities One Hot Encoding could carry out poorly as a result of excessive reminiscence consumption whereas creating the dummy variables.

Use One Hot Encoding when you’ve non-ordinal options and when the explicit options in your knowledge are much less.



Please enter your comment!
Please enter your name here