The Wayback Machine - https://web.archive.org/web/20241129060234/https://www.geeksforgeeks.org/guided-ordinal-encoding-techniques/
Open In App

Guided Ordinal Encoding Techniques

Last Updated : 27 Sep, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

There are specifically two types of guided encoding techniques for categorical features, namely – target guided ordinal encoding & mean guided ordinal encoding.

Tools and Technologies needed:

  1. Understanding of pandas library
  2. Basic knowledge of how a pandas Dataframe work.
  3. Jupyter Notebook or Google Collab or any similar platform.

What is encoding?

 Encoding is the technique we use to convert categorical entry in a dataset to a numerical data. Let say we have a dataset of employees in which there is a column that contains the information about the city location of an employee. Now we want to use this data to form a model which could predict the salary of an employee based upon his/her other details. Obviously, this model doesn’t understand anything about the city name. So how will you make the model know about it? For example, an employee who lives in a metropolitan city earns more than employees of a small city. Someway we need to make the model know about this . Yes, the way you are thinking in your mind is what we will do through code. As obvious we are thinking to rank the city based upon some spec . These ways of converting a categorical data to a numerical data are our target. 

What is target guided encoding technique?

In this technique we will take help of our target variable to encode the categorical data . lets understand by an example,

Employee Id City  Highest Qualification Salary
A100 delhi Phd 50000
A101 delhi bsc 30000
A102 mumbai msc 45000
B101 pune bsc 25000
B102 kolkata phd 48000
C100 pune msc 30000
D103 kolkata msc 44000

Lets try to encode the city column using the target guided encoding. Here our target variable is salary.

step 1: sort the cities based upon the corresponding salary. Now to do this we will take mean of all the salaries of that particular city.

step 2: Based upon the mean of the salary  the descending order of the city is :

                                                         kolkata>mumbai>delhi>pune

step3: Based upon this order we will rank the cities.

City Rank
kolkata 4
mumbai 3
delhi 2
pune 1

(note: you can rank them in the opposite order too)

step 4 : we will use this information to encode the City column of the dataset.

Employee Id  City Highest Qualification Salary
A100 2 phd 50000
A101 2 bsc 30000
A102 3 msc 45000
B101 1 bsc 25000
B102 4 phd 48000
C100 1 msc 30000
D103 4 msc 44000

This is all what target guided encoding is! simple right? Lets now explore about mean guided encoding.

What is mean guided encoding technique?

We will encode the Highest qualification column using the mean guided encoding technique.

step 1: For each highest qualification we will find the mean of all the corresponding salary.

step 2 : Instead of ranking them based upon the mean value , we will encode this mean value corresponding to the respective highest qualification

Highest Qualification Mean Salary
Phd 49000
Msc 39666.67
Bsc 27500

step 3 : We will use this to encode the Highest Qualification column

Employee Id City Highest Qualification Salary
A100 2 49000 50000
A101 2 27500 30000
A102 3 39666.67 45000
B101 1 27500 25000
B102 4 49000 48000
C100 1 39666.67 30000
D103 4 39666.67 44000

Hence we are ready with our dataset to prepare our model.



Previous Article
Next Article

Similar Reads

How to Perform Ordinal Encoding Using Sklearn
Have you ever played a game where you rank things, like your favorite pizza toppings or the scariest monsters? In the field of computers, it is essentially what ordinal encoding accomplishes! It converts ordered data, such as "small," "medium", and "large," into numerical values that a computer can comprehend. Understanding Ordinal EncodingWhat is
6 min read
What is difference between one hot encoding and leave one out encoding?
Answer: One-hot encoding represents each category with a binary vector, while leave-one-out encoding replaces a category with the mean of the target variable excluding the current observation.One-hot encoding and leave-one-out encoding are two different methods used in categorical variable encoding. Let's compare them in detail in tabular form: Cri
2 min read
CatBoost's Categorical Encoding: One-Hot vs. Target Encoding
CatBoost is a powerful gradient boosting algorithm that excels in handling categorical data. It incorporates unique methods for encoding categorical features, including one-hot encoding and target encoding. Understanding these encoding techniques is crucial for effectively utilizing CatBoost in machine learning tasks. In real-world datasets, we qui
6 min read
Feature Encoding Techniques - Machine Learning
As we all know that better encoding leads to a better model and most algorithms cannot handle the categorical variables unless they are converted into a numerical value. Categorical features are generally divided into 3 types: A. Binary: Either/or Examples: Yes, NoTrue, False B. Ordinal: Specific ordered Groups. Examples: low, medium, highcold, hot
5 min read
Nominal vs Ordinal Data
Data science revolves around the processing and analysis of data utilizing a range of tools and techniques. In today's data-driven world, we come across types of data each requiring handling and interpretation. It is important to understand different types of data for proper data analysis and statistical interpretation. The type of data determines
7 min read
What Is Ordinal Data?
Ordinal data is a form of categorical data that has a meaningful order among its categories. But, it lacks any numerical values or a fixed interval that can separate them from each other. In simple terms, ordinal data represents variables that can be ranked or ordered, but the precise difference between the ranks is not known. In this article, we w
7 min read
Label Encoding in Python
In machine learning projects, we usually deal with datasets having different categorical columns where some columns have their elements in the ordinal variable category for e.g a column income level having elements as low, medium, or high in this case we can replace these elements with 1,2,3. where 1 represents 'low' 2 'medium' and 3' high'. Throug
3 min read
Encoding Methods in Genetic Algorithm
Biological Background : Chromosome: All living organisms consist of cells. In each cell, there is the same set of Chromosomes. Chromosomes are strings of DNA and consist of genes, blocks of DNA. Each gene encodes a trait, for example, the color of the eye. Reproduction: During reproduction, combination (or crossover) occurs first. Genes from parent
3 min read
Mean Encoding - Machine Learning
During Feature Engineering the task of converting categorical features into numerical is called Encoding. There are various ways to handle categorical features like OneHotEncoding and LabelEncoding, FrequencyEncoding or replacing by categorical features by their count. In similar way we can uses MeanEncoding. Created a DataFrame having two features
2 min read
What is Internal Storage Encoding of Characters(ISCII)?
We all know, the computer does not directly store letters, numbers, and pictures directly. It converts them into small pieces called bits, which either have two values, 0 or 1. To represent each letter or number properly, we need some rules to correctly store them. These rules correspond to the encoding schema. We will look at the 3 most popular st
3 min read
Arithmetic Encoding and Decoding Using MATLAB
Arithmetic coding is a type of entropy encoding utilized in lossless data compression. Ordinarily, a string of characters, for example, the words "hey" is represented for utilizing a fixed number of bits per character. In the most straightforward case, the probability of every symbol occurring is equivalent. For instance, think about a set of three
6 min read
One Hot Encoding using Tensorflow
In this post, we will be seeing how to initialize a vector in TensorFlow with all zeros or ones. The function you will be calling is tf.ones(). To initialize with zeros you could use tf.zeros() instead. These functions take in a shape and return an array full of zeros and ones accordingly. Code: import tensorflow as tf ones_matrix = tf.ones([2, 3])
2 min read
Python – Categorical Encoding using Sunbird
The Sunbird library is the best option for feature engineering purposes. In this library, you will get various techniques to handle missing values, outliers, categorical encoding, normalization and standardization, feature selection techniques, etc. It can be installed using the below command: pip install sunbirdCategorical Encoding Categorical dat
5 min read
Categorical Encoding with CatBoost Encoder
Many machine learning algorithms require data to be numeric. So, before training a model, we need to convert categorical data into numeric form. There are various categorical encoding methods available. Catboost is one of them. Catboost is a target-based categorical encoder. It is a supervised encoder that encodes categorical columns according to t
5 min read
One-Hot Encoding in NLP
Natural Language Processing (NLP) is a quickly expanding discipline that works with computer-human language exchanges. One of the most basic jobs in NLP is to represent text data numerically so that machine learning algorithms can comprehend it. One common method for accomplishing this is one-hot encoding, which converts category variables to binar
9 min read
Why Tree Ensembles Don't Require One-Hot-Encoding
Answer: Tree ensembles, unlike linear models, inherently handle categorical data without requiring one-hot encoding due to their split-based nature.Tree ensembles, such as Random Forests and Gradient Boosting Machines (GBMs), have a unique capability to handle categorical data without the need for one-hot encoding. This ability stems from the funda
2 min read
Encoding Before vs After Train_Test_Split?
Answer: Encode after train_test_split to prevent data leakage and ensure the model generalizes well to unseen data.When preparing data for machine learning models, the timing of encoding categorical variables in relation to splitting the dataset into training and testing sets can significantly impact the model's performance. Encoding can be perform
2 min read
Is It Effective to Use One-Hot Encoding When Its Dimension Is as Large as Thousands?
Answer: It lead to high memory usage and model complexity, often making it less effective without dimensionality reduction techniques.One-hot encoding transforms categorical variables into a form that algorithms can work with more effectively. However, when the dimensionality reaches the thousands, it introduces challenges that can affect the effic
2 min read
Byte-Pair Encoding (BPE) in NLP
Natural Language Processing (NLP) is a subfield of artificial intelligence that gives the ability to machine understand and process human languages. Tokenization is the process of dividing the text into a collection of tokens from a string of text. Tokenization is often the first step in natural language processing tasks such as text classification
6 min read
Positional Encoding in Transformers
In the domain of natural language processing (NLP), transformer models have fundamentally reshaped our approach to sequence-to-sequence tasks. .However, unlike conventional recurrent neural networks (RNNs) or convolutional neural networks (CNNs), Transformers lack inherent awareness of token order. In this article, we will understand the significan
8 min read
Target encoding using nested CV in sklearn pipeline
In machine learning, feature engineering plays a pivotal role in enhancing model performance. One such technique is target encoding, which is particularly useful for categorical variables. However, improper implementation can lead to data leakage and overfitting. This article delves into the intricacies of target encoding using nested cross-validat
7 min read
Why One-Hot Encoding Improves Machine Learning Performance?
One-hot encoding is a crucial step in data preparation for machine learning algorithms. It involves converting categorical data into a numerical format that can be effectively processed by these algorithms. This technique has been widely adopted due to its ability to significantly improve the performance of machine learning models. In this article,
8 min read
Difference Between Manchester and Differential Manchester Encoding
Manchester and Differential Manchester encoding are two line coding techniques that are used in digital communication to encode binary data. Both methods employ a technique of converting data to form so that it can detect errors when in transit. Manchester encoding encodes data bits with a transition in the middle of the bit period which restricts
4 min read
Label Encoding Across Multiple Columns in Scikit-Learn
Preprocessing data is a crucial step that often involves converting categorical data into a numerical format. One of the most common techniques for this conversion is label encoding. This article delves into the intricacies of applying label encoding across multiple columns using Scikit-Learn, a popular machine learning library in Python. Table of
7 min read
One-Hot-Encoding from a Pandas Column Containing a List
One-hot encoding is a crucial preprocessing step in data science, especially when dealing with categorical data. It converts categorical variables into a binary matrix representation, where each category is represented by a separate column. This article will guide you through the process of one-hot encoding a Pandas column containing a list of elem
6 min read
One Hot Encoding in Machine Learning
While developing Data Science Projects, we may find datasets containing mixed data types consisting of both categorical as well as numerical columns. However, various Machine Learning models do not work with categorical data and to fit this data into the machine learning model it needs to be converted into numerical data. For example, suppose a dat
10 min read
Encoding Categorical Data in Sklearn
Categorical data is a common occurrence in many datasets, especially in fields like marketing, finance, and social sciences. Unlike numerical data, categorical data represents discrete values or categories, such as gender, country, or product type. Machine learning algorithms, however, require numerical input, making it essential to convert categor
6 min read
Feature Extraction Techniques - NLP
Introduction : This article focuses on basic feature extraction techniques in NLP to analyse the similarities between pieces of text. Natural Language Processing (NLP) is a branch of computer science and machine learning that deals with training computers to process a large amount of human (natural) language data. Briefly, NLP is the ability of com
11 min read
Optimization techniques for Gradient Descent
Gradient Descent is a widely used optimization algorithm for machine learning models. However, there are several optimization techniques that can be used to improve the performance of Gradient Descent. Here are some of the most popular optimization techniques for Gradient Descent: Learning Rate Scheduling: The learning rate determines the step size
4 min read
Data Mining Techniques
Data mining refers to extracting or mining knowledge from large amounts of data. In other words, Data mining is the science, art, and technology of discovering large and complex bodies of data in order to discover useful patterns. Theoreticians and practitioners are continually seeking improved techniques to make the process more efficient, cost-ef
15+ min read
three90RightbarBannerImg