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

Understanding Ordinal Encoding Methods

The document discusses the encoding of categorical variables in machine learning, highlighting the necessity of converting these variables into numerical formats for algorithm compatibility. It explains different encoding techniques such as Ordinal Encoding, One Hot Encoding, and Label Encoding, detailing their applications and limitations. Additionally, it provides a comparison between Label Encoding and One-Hot Encoding, emphasizing their respective uses and impacts on data dimensions.

Uploaded by

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

Understanding Ordinal Encoding Methods

The document discusses the encoding of categorical variables in machine learning, highlighting the necessity of converting these variables into numerical formats for algorithm compatibility. It explains different encoding techniques such as Ordinal Encoding, One Hot Encoding, and Label Encoding, detailing their applications and limitations. Additionally, it provides a comparison between Label Encoding and One-Hot Encoding, emphasizing their respective uses and impacts on data dimensions.

Uploaded by

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

Machine Learning | YBI Foundation

Encoding Concept Notes

Categorical variables are usually represented as ‘strings’ or ‘categories’ and are finite in number.
The variables only have definite possible values. Many machine learning algorithms cannot work
with categorical data directly. The categories must be converted into numbers. This is required for
both input and output variables that are categorical.

Further, categorical variables can be divided into two categories: Nominal (No particular order) and
Ordinal (some ordered).

1. Ordinal Data: The categories have an inherent order. In Ordinal data, while encoding, one
should retain the information regarding the order in which the category is provided. Like
qualification as schooling, graduate, post graduate etc. of a person possesses decides
whether a person is suitable for a post or not. Also, these qualifications are ordered from
schooling being least to post graduation being maximum qualification.
2. Nominal Data: The categories do not have an inherent order. While encoding Nominal data,
we have to consider the presence or absence of a feature. In such a case, no notion of order
is present. In such a case, no notion of order is present. For example, the city a person lives
in. For the data, it is important to retain where a person lives. Here, we do not have any order
or sequence. It is equal if a person lives in Delhi or Bangalore.

Ordinal Encoding

We do Ordinal encoding to ensure the encoding of variables retains the ordinal nature of the variable.
If we consider the temperature scale as the order, then the ordinal value should be from cold to
“Very Hot. “Ordinal encoding will assign values as ( Cold(0) <Warm(1)<Hot(2)<Very Hot(3)). Usually,
Ordinal Encoding is done starting from 0. Whereas, as per alphabetically sorted order Scikit-learn
ordinal encoding function assignee Cold(0), Hot(1), Very Hot (2) and Warm (3).

One Hot Encoding or Dummy Variable


In this method, we map each category to a vector that contains 1 and 0, denoting the presence or
absence of the feature. The number of vectors depends on the number of categories for features.
[Link] (+91) 9667987711 support@[Link] g

Page 2|5
Machine Learning | YBI Foundation

This method produces many columns that slow down the learning significantly if the number of the
category is very high for the feature. Pandas has get_dummies function, which is quite easy to use.
Scikit-learn has OneHotEncoder for this purpose, but it does not create an additional feature column
(another code is needed.

One Hot Encoding is very popular. We can represent all categories by N-1 (N= No of Category) as
sufficient to encode the one that is not included. Usually, for Regression, we use N-1 (drop first or
last column of One Hot Coded new feature). Let’s explain, If the model includes an intercept and
contains dummy variables, then the columns would add up (row-wise) to the intercept and this linear
combination would prevent the matrix inverse from being computed (as it is singular).

Still, for classification, the recommendation is to use all N columns without as most of the tree-
based algorithm builds a tree based on all available variables. One hot encoding with N-1 binary
variables should be used in linear Regression to ensure the correct number of degrees of freedom
(N-1). The linear Regression has access to all of the features as it is being trained and therefore
examines the whole set of dummy variables altogether. This means that N-1 binary variables give
complete information about (represent completely) the original categorical variable to the linear
Regression. This approach can be adopted for any machine learning algorithm that looks at ALL the
features simultaneously during training—for example, support vector machines and neural networks
as well as clustering algorithms.

We will never consider that additional label in tree-based methods if we drop. Thus, if we use the
categorical variables in a tree-based learning algorithm, it is good practice to encode it into N binary
variables and don’t drop.

[Link] (+91) 9667987711 support@[Link] g

Page 3|5
Machine Learning | YBI Foundation

Label Encoding
Labels can be words or numbers. Usually, the training data is labeled with words to make it readable.
Label encoding converts word labels into numbers to let algorithms work on them.

[Link] (+91) 9667987711 support@[Link] g

Page 4|5
Machine Learning | YBI Foundation

Encoding Interview Preparation

Q. What is the difference between Label Encoding and One-Hot Encoding?

Label Encoding One-Hot Encoding


How is the Converts the data into dummy
categorical Labels the data into numbers variables, i.e., binary having 1 or 0 as
data treated? values.
Var_Male: 1 and 0 / Var_Female: 0 and
Example Male: 1 Female: 2
1
Dummies can be created by either
It can be used via the sklearn
How to use it in sklearn’s function: OneHotEncoder or
package’s function called
Python? Python’s inbuilt function:
LabelEncoder
pd.get_dummies
Changes the nominal data into ordinal
The method creates extra redundant
making the values given to the
Limitation of columns for each category, and a
categories as weights, and hence the
the method different column is generated. This
machine accordingly gives those
increases the dimensions of the data.
values importance.
Solution Employ Dummy creation or One-Hot Use the various methods available for
available encoding technique dimensionality reduction
Label Encoding One-Hot Encoding

[Link] (+91) 9667987711 support@[Link] g

Page 5|5

Common questions

Powered by AI

One-Hot Encoding converts categorical variables into a set of binary vectors, thus treating each category as a distinct feature without implying ordinal relationships. It is suitable for nominal data and results in the creation of additional columns, with one column for each category . Conversely, Ordinal Encoding assigns a numerical value to each category according to their order, maintaining and utilizing the ordinal nature of the data . The implications of using One-Hot Encoding are increased dimensionality, which may require more computational resources and can introduce redundancy , whereas Ordinal Encoding risks introducing bias by assuming implicit ordering when it doesn't exist .

When using tree-based algorithms with encoded categorical data, potential errors include biased splits due to misleading interpretations of encoded values, especially when Ordinal Encoding misrepresents Nominal data by its numeric value . Using One-Hot Encoding may mitigate this by enabling decision trees to consider each category individually, preventing unequal treatment of equivalent categories . However, One-Hot Encoding increases feature space complexity, which can affect performance or introduce noise if not properly handled . Proper encoding ensures that the tree structure generated reflects true data characteristics, thereby enhancing predictive accuracy and minimizing model bias.

One-Hot Encoding can slow down learning algorithms because it increases the dimensionality of the data significantly, especially if a categorical variable has many levels . This increase in columns, also known as the curse of dimensionality, can degrade model performance and computational efficiency. To mitigate this, dimensionality reduction techniques can be employed, or for models with inherent difficulty handling high-dimensional spaces, using N-1 binary variables instead of N may reduce computational load without losing information . For linear regression, dropping one category (encoded as N-1) helps avoid multicollinearity .

In regression models, One-Hot Encoding typically uses N-1 variables to prevent multicollinearity, where the columns sum to an intercept . This ensures that the matrix is invertible by maintaining the correct degrees of freedom for accurate regression analysis. However, in classification models, it is often recommended to use all N dummy variables since many tree-based algorithms, and classifiers do not calculate an intercept but rather use all available information in feature construction . The implication is that while N-1 encoding is efficient and necessary for avoiding multicollinearity in regression, using all N encoded columns in classification can provide richer feature interactions without computational pitfalls.

Choosing between Ordinal and Nominal encoding depends on the inherent characteristics of categorical data. Ordinal encoding is suitable when there is an inherent order among categories, as it preserves the order information of the data . For instance, educational levels such as schooling, graduate, and post-graduate should be encoded ordinally to maintain the order relationship . On the other hand, Nominal encoding is appropriate for unordered categories, emphasizing the discrete nature of each category without implying any rank. Cities like Delhi or Bangalore, which have no meaningful order, should be categorized nominally . Therefore, understanding the nature of the categorical data is crucial for effective encoding.

The primary limitation of Label Encoding is that it can falsely impose an ordinal relationship on categorical data, which may lead to biased results in algorithms interpreting these labels as ranked features . This is particularly problematic for nominal data, where categories are independent of one another. To overcome these limitations, alternatives like One-Hot Encoding are employed, which treat each category as a separate binary vector, preserving the non-ordinal nature of data . Furthermore, dimensionality reduction methods can help manage the increased data complexity resulting from One-Hot Encoding .

The choice of encoding strategy significantly impacts the interpretability of machine learning models because it determines how categorical information is represented numerically, affecting model transparency and feature importance calculation. Using Ordinal Encoding can mislead models by assigning unwarranted weights to specific categories due to falsely induced order . On the other hand, One-Hot Encoding tends to maintain the individual feature identity of each category, enhancing clarity but at the cost of increased dimensionality . Thus, selecting the appropriate encoding depends on the nature of data and the need for justification behind predictions, balancing simplicity and informativeness in model interpretations.

Storing categorical data as strings or categories is important because many machine learning algorithms operate natively on numerical data, hence requiring conversion of string categories into numerical form through processes like encoding . This consideration impacts data preprocessing by necessitating the transformation of categorical data into a numerical format that the algorithms can interpret, affecting data integrity and the machine learning model's performance. Appropriate encoding methods—such as One-Hot Encoding for nominal data or Ordinal Encoding for ordered data—are adopted to preserve critical categorical relationships while converting them into a machine-readable format . This step ensures that the linguistic or ordinal nature of the data isn't lost, thus maintaining dataset richness.

Label Encoding is unsuitable for scenarios where categorical data does not possess an inherent order. This is because Label Encoding assigns integers to categories, which might imply a false ordinal relationship and inadvertently lead the algorithm to interpret them as ranked entities . For instance, using Label Encoding on nominal data like country names or gender can mislead the model into attributing unwarranted importance to certain categories, which might impact the predictive performance by introducing a bias based on the assigned numbers . Hence, Label Encoding should be avoided for purely nominal data, favoring techniques like One-Hot Encoding that preserve the categorical nature without implying order.

One challenge with using One-Hot Encoding in regression models is the potential for multicollinearity, where the inclusion of an intercept term and all dummy variables results in a singular matrix, preventing it from being inverted . This can be addressed by dropping one category (represented by N-1 variables instead of N), hence avoiding multicollinearity while retaining the necessary degrees of freedom for accurate regression analysis . This approach helps the model effectively utilize categorical features without redundancy.

You might also like