Label encoding changes words or categories into numbers so computers can understand them.
0
0
Label encoding in ML Python
Introduction
When you have categories like colors or types and want to use them in a machine learning model.
When you need to convert text labels into numbers for classification tasks.
When preparing data for algorithms that only accept numbers.
When you want a simple way to turn categories into numbers without creating many new columns.
Syntax
ML Python
from sklearn.preprocessing import LabelEncoder le = LabelEncoder() encoded_labels = le.fit_transform(list_of_labels)
fit_transform learns the categories and converts them to numbers in one step.
The numbers start from 0 and go up to the number of categories minus one.
Examples
This example converts three color names into numbers: blue=0, green=1, red=2.
ML Python
from sklearn.preprocessing import LabelEncoder labels = ['red', 'green', 'blue'] le = LabelEncoder() encoded = le.fit_transform(labels) print(encoded)
Repeated categories get the same number: bird=0, cat=1, dog=2.
ML Python
from sklearn.preprocessing import LabelEncoder le = LabelEncoder() labels = ['cat', 'dog', 'cat', 'bird'] encoded = le.fit_transform(labels) print(encoded)
Sample Model
This program turns fruit names into numbers so a computer can use them. It also shows which number matches each fruit.
ML Python
from sklearn.preprocessing import LabelEncoder # List of fruit names fruits = ['apple', 'banana', 'cherry', 'banana', 'apple', 'cherry'] # Create LabelEncoder object le = LabelEncoder() # Fit and transform the fruit list encoded_fruits = le.fit_transform(fruits) print('Original labels:', fruits) print('Encoded labels:', encoded_fruits) # Show the mapping from labels to numbers for label, code in zip(le.classes_, range(len(le.classes_))): print(f"'{label}' is encoded as {code}")
OutputSuccess
Important Notes
Label encoding assigns numbers based on alphabetical order of categories.
It is best for categories without order or when the model can handle numeric labels properly.
For categories with no natural order, consider one-hot encoding to avoid implying order.
Summary
Label encoding changes categories into numbers so machines can understand them.
It is simple and useful for many classification problems.
Always check if label encoding fits your data type and model needs.