0
0
Data Analysis Pythondata~5 mins

Label encoding in Data Analysis Python

Choose your learning style9 modes available
Introduction

Label encoding changes words or categories into numbers so computers can understand them easily.

When you have categories like colors or types and want to use them in math or machine learning.
When you want to convert text labels into numbers for sorting or grouping.
When preparing data for models that only accept numbers, like many machine learning algorithms.
When you want to quickly assign a unique number to each category in your data.
Syntax
Data Analysis Python
from sklearn.preprocessing import LabelEncoder

encoder = LabelEncoder()
encoded_labels = encoder.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 unique categories minus one.

Examples
This converts the colors into numbers. 'blue' becomes 0, 'green' 1, and 'red' 2.
Data Analysis Python
from sklearn.preprocessing import LabelEncoder

labels = ['red', 'green', 'blue', 'green']
encoder = LabelEncoder()
encoded = encoder.fit_transform(labels)
print(encoded)
Each animal name is turned into a number. The same animal gets the same number.
Data Analysis Python
from sklearn.preprocessing import LabelEncoder

labels = ['cat', 'dog', 'cat', 'bird']
encoder = LabelEncoder()
encoded = encoder.fit_transform(labels)
print(encoded)
Sample Program

This program changes fruit names into numbers. It also shows which number matches which fruit.

Data Analysis Python
from sklearn.preprocessing import LabelEncoder

# List of fruit names
fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'kiwi']

# Create the encoder
encoder = LabelEncoder()

# Fit and transform the fruit list
encoded_fruits = encoder.fit_transform(fruits)

# Show the original and encoded lists
print('Original:', fruits)
print('Encoded:', encoded_fruits)

# Show the mapping from numbers back to fruit names
print('Mapping:')
for i, fruit in enumerate(encoder.classes_):
    print(f'{i} -> {fruit}')
OutputSuccess
Important Notes

Label encoding assigns numbers based on alphabetical order of categories.

It is best for categories without order; for ordered categories, consider other methods.

Label encoding can cause problems if the model thinks numbers have order or size meaning.

Summary

Label encoding turns categories into numbers so computers can use them.

It assigns numbers starting from 0 based on alphabetical order.

Useful for preparing data for machine learning models that need numbers.