← Back to list

What is One Hot Encoding and When do We Use it?

One hot encoding is a popular technique in data science and machine learning used to convert categorical variables into a numerical format…

Hadid Younas · 2023-02-26 21:56 · 0 claps · 3.4 min read
#one-hot-encoding #one-hot-encoder #hot-encoder #hot-encoded-label
Open on Medium ↗
Wiki topics: ML · Machine Learning EDU · Education & Learning 💻 · Programming 🔬 · Science · General

What is One Hot Encoding and When do We Use it?

One hot encoding is a popular technique in data science and machine learning used to convert categorical variables into a numerical format for analysis. Categorical variables are variables that represent a limited number of values or categories, such as colors, types of food, or even the name of a city. These variables cannot be used directly in mathematical computations as they are, and therefore require conversion into numerical form. One hot encoding is one of the most common and simple ways to achieve this.

In one hot encoding, each category is assigned a unique number, and a binary vector is created for each observation in the dataset. The length of the vector is equal to the total number of categories. Each position in the vector represents a specific category, and the value in that position is either 1 or 0, depending on whether the category is present or not.

For example, suppose we have a dataset that includes information about different types of fruits, such as apples, oranges, and bananas. We want to use this data to predict whether a fruit is ripe or not based on its color. We can represent the color of each fruit using one hot encoding. The colors in our dataset are red, orange, yellow, and green. We assign each color a unique number, as follows:

  • Red: 1
  • Orange: 2
  • Yellow: 3
  • Green: 4

We then create a binary vector for each fruit, where the length of the vector is equal to the total number of colors. Let’s take the example of an apple, which is red in color. The binary vector for the apple would look like this:

  • Red: 1
  • Orange: 0
  • Yellow: 0
  • Green: 0

This vector indicates that the apple is red in color, and all other colors are absent. We can create similar vectors for all other fruits in our dataset.

One hot encoding has several advantages over other encoding techniques. One of the most significant advantages is that it does not introduce any ordinal relationship between the categories. This means that the numerical representation of each category is independent of the others and does not imply any order or ranking. This property is essential when dealing with categorical variables that do not have any natural order, such as colors or types of food.

Another advantage of one hot encoding is that it can be easily implemented in most machine learning algorithms. Many popular libraries and frameworks, such as sci-kit-learn and TensorFlow, have built-in support for one hot encoding, making it easy to use in data analysis and modeling.

In conclusion, one-hot encoding is a simple yet powerful technique for converting categorical variables into the numerical format. It is widely used in data science and machine learning and has several advantages over other encoding techniques. If you’re working with categorical data, it’s a good idea to consider one hot encoding as an option for preprocessing your data.

Here we will write a code for it

from tensorflow.keras.utils import to_categorical 

y_train_encoded = to_categorical(y_train)
y_test_encoded = to_categorical(y_test)

Now we are validated the shape data

print('y_train_encoded shape:', y_train_encoded.shape)
print('y_test_encoded shape:', y_test_encoded.shape)

Now we display the encoded version of the training dataset

y_train_encoded[0]

There are several situations in which one hot encoding can be beneficial. Some of these include:

  1. Classification problems: One hot encoding is often used in classification problems where the target variable is categorical. For example, predicting whether a customer will buy a product or not based on their age, gender, and purchase history.
  2. Natural language processing: One hot encoding is commonly used in natural languages processing tasks, such as sentiment analysis or document classification. In these tasks, one hot encoding can be used to represent words or phrases as numerical vectors.
  3. Recommender systems: One hot encoding can be useful in recommender systems, where the goal is to recommend items to users based on their preferences. One hot encoding can be used to represent user preferences or item features as numerical vectors.
  4. Feature selection: One hot encoding can be helpful in feature selection, where the goal is to identify the most relevant features for a given task. One hot encoding can be used to represent categorical variables in a format that can be easily used in feature selection algorithms.

It’s important to note that one hot encoding has some limitations, particularly in cases where the number of categories is large. One hot encoding can lead to high-dimensional data, which can be computationally expensive and lead to overfitting. In such cases, other encoding techniques, such as ordinal encoding or target encoding, may be more appropriate.

In summary, one hot encoding is a useful technique for converting categorical variables into the numerical format, particularly in cases where the data is nominal and there is no natural order between categories. One hot encoding is commonly used in classification problems, natural language processing, recommender systems, and feature selection. However, it’s important to be mindful of the limitations of one hot encoding, particularly in cases where the number of categories is large.


메타데이터
post_id
6e91d5e91dee
slug
what-is-one-hot-encoding-and-when-do-we-use-it-6e91d5e91dee
url
https://medium.com/@hadeedyounas819/what-is-one-hot-encoding-and-when-do-we-use-it-6e91d5e91dee
canonical_url
https://medium.com/@hadeedyounas819/what-is-one-hot-encoding-and-when-do-we-use-it-6e91d5e91dee
author_url
https://medium.com/@hadeedyounas819
status
ok
fetched_at
2026-08-30 06:53:19