smart_toy
Machine Learning
keyboard_arrow_down 29 guides
1. ML models
2. Feature engineering
3. Optimization
4. Model evaluation
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings
Comprehensive Guide on sklearn's LabelEncoder
schedule Mar 5, 2023
Last updated local_offer
Tags Machine Learning●Python
tocTable of Contents
expand_more Check out the interactive map of data science
The LabelEncoder
module in Python's sklearn
is used to encode the target labels into categorical integers (e.g. 0, 1, 2, ...).
Encoding numerical target labels
Suppose our target labels are as follows:
raw_y = [6,9,2,5,6]
Our objective is to apply the following mapping:
2: 05: 16: 29: 3
Here, the value 2 gets mapped to 0, the value 5 gets mapped to 1, and so on.
We can make use of LabelEncoder
like so:
from sklearn.preprocessing import LabelEncoder
raw_y = [6,9,2,5,6]encoder = LabelEncoder()y = encoder.fit_transform(raw_y) # returns a NumPy arrayy
array([2, 3, 0, 1, 2])
Here, y
is the encoded values.
We can access the classes, that is, the unique values in our target like so:
encoder.classes_
array([2, 5, 6, 9])
We can also get the original raw_y
using the inverse_transform(~)
function:
encoder.inverse_transform(y)
array([6, 9, 2, 5, 6])
Encoding categorical string target labels
The LabelEncoder
also works when the target label is categorical:
from sklearn.preprocessing import LabelEncoder
raw_y = ["A","B","A","C"]encoder = LabelEncoder()y = encoder.fit_transform(raw_y)y
array([0, 1, 0, 2])
We can see all our classes like so:
encoder.classes_
array(['A', 'B', 'C'], dtype='<U1')
We can retrieve the original raw_y
like so:
encoder.inverse_transform(y)
array(['A', 'B', 'A', 'C'], dtype='<U1')
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!