0% found this document useful (0 votes)
13 views6 pages

Data Encoding

Data encoding is a crucial pre-processing step in machine learning that converts categorical or textual data into numerical format for algorithm processing. It includes methods such as one-hot encoding, dummy encoding, ordinal encoding, binary encoding, and count encoding, each with its own advantages and use cases. Proper encoding is essential for model performance and helps prevent bias by ensuring equal feature weighting.

Uploaded by

RESHMY
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Data Encoding

Data encoding is a crucial pre-processing step in machine learning that converts categorical or textual data into numerical format for algorithm processing. It includes methods such as one-hot encoding, dummy encoding, ordinal encoding, binary encoding, and count encoding, each with its own advantages and use cases. Proper encoding is essential for model performance and helps prevent bias by ensuring equal feature weighting.

Uploaded by

RESHMY
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Encoding is an important pre-processing step in Machine Learning.

It refers to the
process of converting categorical or textual data into numerical format, so that it can be used
as input for algorithms to process. The reason for encoding is that most machine learning
algorithms work with numbers and not with text or categorical variables.
Categorical variables are usually represented as ‘strings’ or ‘categories’ and are finite in
number.
we can see there are two kinds of categorical data-
Ordinal Data: The categories have an inherent order
Nominal Data: The categories do not have an inherent order
This categorical data encoding method transforms the categorical variable into a set
of binary variables (also known as dummy variables).
In the case of one-hot encoding, for N categories in a variable, it uses N binary
variables.
The dummy encoding is a small improvement over one-hot-encoding.
Dummy encoding uses N-1 features to represent N labels/categories.

 .
What is Categorical Data?
When we collect data, we often encounter different types of variables. One such type is
categorical variables. Categorical variables are usually represented as ‘strings’ or
‘categories’ and are finite in number.
There are two types of categorical data -
• Ordinal Data
• Nominal Data
Here are a few examples of categorical variables:
• Places: Delhi, Mumbai, Ahmedabad, Bangalore, etc.
• Departments: Finance, Human resources, IT, Production.
• Grades: A, A-, B+, B, B- etc.
Ordinal Data:
The categories of ordinal data have an Inherent Order. This means that the categories can
be Ranked or ordered from highest to lowest or vice versa.
For example, the variable “highest degree a person has” is an ordinal variable. The
categories (High school, Diploma, Bachelors, Masters, PhD) can be ranked in order of the
level of education attained.
Nominal Data:
The categories of nominal data do not have an Inherent Order. This means that the
categories cannot be ranked or ordered.
For example, the variable “city where a person lives” is a nominal variable. The categories
(Delhi, Mumbai, Ahmedabad, Bangalore, etc.) cannot be ranked or ordered.
Why it is Important?
• Most machine learning algorithms work only with numerical data, so categorical variables
(such as text labels) must be transformed into numerical values.
• This allows the model to identify patterns in the data and make predictions based on those
patterns.
• Encoding also helps to prevent bias in the model by ensuring that all features are equally
weighted.
• The choice of encoding method can have a significant impact on model performance, so it
is important to choose an appropriate encoding technique based on the nature of the data
and the specific requirements of the model.
There are several methods for encoding categorical variables, including
1. One-Hot Encoding
2. Dummy Encoding
[Link] Encoding(Label)
4. Binary Encoding
5. Count Encoding
Let’s take a closer look at each of these methods.
One-Hot Encoding:
• One-Hot Encoding is the Most Common method for encoding Categorical variables.
• a Binary Column is created for each Unique Category in the variable.
• If a category is present in a sample, the corresponding column is set to 1, and all other
columns are set to 0.
• For example, if a variable has three categories ‘A’, ‘B’ and ‘C’, three columns will be
created and a sample with category ‘B’ will have the value [0,1,0].

# One-Hot Encoding:
# create a sample dataframe with a categorical variable
df = [Link]({'color': ['red', 'green', 'blue', 'red']})
# perform one-hot encoding on the 'color' column
one_hot = pd.get_dummies(df['color'])

# concatenate the one-hot encoding with the original dataframe


df1 = [Link]([df, one_hot], axis=1)

# drop the original 'color' column


df1 = [Link]('color', axis=1)
Dummy Encoding
• Dummy coding scheme is similar to one-hot encoding.
• This categorical data encoding method transforms the categorical variable into a set of
binary variables [0/1].
• In the case of one-hot encoding, for N categories in a variable, it uses N binary variables.
• The dummy encoding is a small improvement over one-hot-encoding. Dummy encoding
uses N-1 features to represent N labels/categories.

One-Hot Encoding vs Dummy Encoding:


One-Hot Encoding — N categories in a variable, N binary variables.
Dummy encoding — N categories in a variable, N-1 binary variables.
# Create a sample dataframe with categorical variable
data = {'Color': ['Red', 'Green', 'Blue', 'Red', 'Blue']}
df = [Link](data)

# Use get_dummies() function for dummy encoding


dummy_df = pd.get_dummies(df['Color'], drop_first=True, prefix='Color')

# Concatenate the dummy dataframe with the original dataframe


df = [Link]([df, dummy_df], axis=1)
Label Encoding:
 Each unique category is assigned a Unique Integer value.
 This is a simpler encoding method, but it has a Drawback in that the assigned
integers may be misinterpreted by the machine learning algorithm as having an
Ordered Relationship when in fact they do not.
from [Link] import LabelEncoder

# Create a sample dataframe with categorical data


df = [Link]({'color': ['red', 'green', 'blue', 'red', 'green']})

print(f"Before Encoding the Data:\n\n{df}\n")

# Create a LabelEncoder object


le = LabelEncoder()

# Fit and transform the categorical data


df['color_label'] = le.fit_transform(df['color'])
Ordinal Encoding:
• Ordinal Encoding is used when the categories in a variable have a Natural Ordering.
• In this method, the categories are assigned a numerical value based on their order,
such as 1, 2, 3, etc.
For example, if a variable has categories ‘Low’, ‘Medium’ and ‘High’, they can be assigned
the values 1, 2, and 3, respectively.

# Ordinal Encoding:
# create a sample dataframe with a categorical variable
df = [Link]({'quality': ['low', 'medium', 'high', 'medium']})
print(f"Before Encoding the Data:\n\n{df}\n")

# specify the order of the categories


quality_map = {'low': 0, 'medium': 1, 'high': 2}

# perform ordinal encoding on the 'quality' column


df['quality_map'] = df['quality'].map(quality_map)
Binary Encoding:
• Binary Encoding is similar to One-Hot Encoding, but instead of creating a separate
column for each category, the categories are represented as binary digits.
 For example, if a variable has four categories ‘A’, ‘B’, ‘C’ and ‘D’, they can be
represented as 0001, 0010, 0100 and 1000, respectively.

# Binary Encoding:

import pandas as pd

# create a sample dataframe with a categorical variable


df = [Link]({'animal': ['cat', 'dog', 'bird', 'cat']})
print(f"Before Encoding the Data:\n\n{df}\n")

# perform binary encoding on the 'animal' column


animal_map = {'cat': 0, 'dog': 1, 'bird': 2}
df['animal'] = df['animal'].map(animal_map)
df['animal'] = df['animal'].apply(lambda x: format(x, 'b'))

# print the resulting dataframe


print(f"After Encoding the Data:\n\n{df}\n")
Count Encoding:
• Count Encoding is a method for encoding categorical variables by counting the number
of times a category appears in the dataset.
 For example, if a variable has categories ‘A’, ‘B’ and ‘C’ and category ‘A’ appears 10
times in the dataset, it will be assigned a value of 10.

You might also like