Label encoding is also known as integer encoding. Integer encoding replaces categorical values with numeric values. Here, the unique values in variables are replaced with a sequence of integer values. For example, let's say there are three categories: red, green, and blue. These three categories were encoded with integer values; that is, red is 0, green is 1, and blue is 2.
Let's take a look at the following label encoding example:
# Import pandas
import pandas as pd
# Read the data
data=pd.read_csv('employee.csv')
# Import LabelEncoder
from sklearn.preprocessing import LabelEncoder
# Instantiate the Label Encoder Object
label_encoder = LabelEncoder()
# Fit and transform the column
encoded_data = label_encoder.fit_transform(data['department'])
# Print the encoded
print(encoded_data)
This results in the following output:
[2 1 0 0 2 1 2 1 0 2]
In the preceding example, we performed simple label encoding.
In the following example, we are encoding the...