Change language

ML | Label Encoding datasets in Python

Label encoding refers to converting labels to a numeric form in order to convert it to a machine-readable form. Machine learning algorithms can then better figure out how these labels should work. This is an important preprocessing step for a structured dataset in supervised learning.

Example:
Suppose we have a column in some dataset. 

After applying the label encoding, the Height column is converted to:

where 0 — label for tall, 1 — label for middle and 2 — for short stature.

We are applying the tag encoding to the iris dataset in the destination column, which is the View. Contains three species Iris-setosa, Iris-versicolor, Iris-virginica .

# Library import

import numpy as np

import pandas as pd

 
# Dataset Import

df = pd. read_csv ( ’../../ data / Iris.csv’ )

 

df [ ’species’ ]. unique ()

Exit:

 array ([’Iris-setosa’,’ Iris-versicolor’, ’Iris-virginica’], dtype = object)  

After applying Label Encoding —

# Import the label encoder

from sklearn import preprocessing

 
# label_encoder object knows how to understand word labels.

label_encoder = preprocessing.LabelEncoder ()

 
# Encode labels in the views column.

df [ ’species’ ] = label_encoder.fit_transform (df [ ’species’ ])

 

df [ ’species’ ]. unique ()

Exit:

 array ([0, 1, 2], dtype = int64) 

Label constraint Encoding
An encoding label converts data into machine-readable form, but assigns a unique number (starting at 0) to each data class. This can lead to the formation of a priority problem when training datasets. A high value label is considered to have higher priority than a lower value label.

example

Attribute having output classes Mexico , Paris , Dubai . On the Coding label of this column, let mexico be replaced with 0 , Paris replaced with 1, and Dubai is replaced by 2.
It can be interpreted that Dubai has a higher priority when training the model than Mexico and Paris , but in fact there is no such priority relationship between these cities.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically