DEV Community

Nitish Kushwaha
Nitish Kushwaha

Posted on

Label Encoding in ML

Label Encoding is one of the most used techniques in machine learning. It is used to convert the categorial data in numerical form. So, data can be fitted into the model.

Let us understand why we use the Label Encoding. Imagine having the data, containing the essential columns in the form of string. But, you cannot fit this data in the model, because modelling only works on numerical data, what do we do? Here comes the life-saving technique which is evaluated at the preprocessing step when we ready the data for fitting, which is Label Encoding.

We will use the iris dataset from Scikit-Learn library, to understand the workings of Label Encoder. Make sure you have the following libraries installed.

pandas
scikit-learn
Enter fullscreen mode Exit fullscreen mode

For installing as libraries, run the following command:

$ python install -U pandas scikit-learn
Enter fullscreen mode Exit fullscreen mode

Now open Google Colab Notebook, and dive into coding and learning Label Encoder.

Let's Code

  • Start with importing the following libraries:
import pandas as pd
from sklearn import preprocessing
Enter fullscreen mode Exit fullscreen mode
  • Import the iris dataset, and initialize it for usage:
from sklearn.datasets import load_iris
iris = load_iris()
Enter fullscreen mode Exit fullscreen mode
  • Now, we need to select the data that we want Encode, we will be encoding the species names for the irises.
species = iris.target_names
print(species)
Enter fullscreen mode Exit fullscreen mode

Output:

array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
Enter fullscreen mode Exit fullscreen mode
  • Let's instantiate the class LabelEncoder from preprocessing:
label_encoder = preprocessing.LabelEncoder()
Enter fullscreen mode Exit fullscreen mode
  • Now, we are ready to fit the data using the label encoder:
label_encoder.fit(species)
Enter fullscreen mode Exit fullscreen mode

You will output similar to this:

Label encoder fit output

If you get this output, you have successfully fitted the data. But, the question is how you will find out what values are assigned to each species and in which order.

The order in which Label Encoder fits the data is stored in classes_ attribute. Encoding starts from 0 to data_length-1.

label_encoder.classes_
Enter fullscreen mode Exit fullscreen mode

Output:

array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
Enter fullscreen mode Exit fullscreen mode

The label encoder will automatically sort the data, and start the encoding from the left side. Here:

setosa -> 0
versicolor -> 1
virginica -> 2
Enter fullscreen mode Exit fullscreen mode
  • Now, let's test the fitted data. We will transform the iris species setosa.
label_encoder.transform(['setosa'])
Enter fullscreen mode Exit fullscreen mode

Output: array([0])

Again, if you transform the specie virginica.

label_encoder.transform(['virginica'])
Enter fullscreen mode Exit fullscreen mode

Output: array([2])

You can also input the list of species, such as ["setosa", "virginica"]

Scikit Learn documentation for label encoder >>>

Top comments (0)