![]() |
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 Ordinal Encoding?Imagine you’re helping a friend sort their movie collection. Three categories are available to you : “terrible,” “okay,” and “awesome.” Each category is given a number by ordinal encoding, such as “terrible” = 1, “okay” = 2, and “awesome” = 3. In this manner “awesome” movies come after “okay” ones so the computer can comprehend. One way to transform categorical data into numerical data is to use ordinal encoding. ‘Red, Green and Blue ‘ are examples of categories or groups that are represented by categorical data. Each category in ordinal encoding is given an integer, such as 1 for “Red,” 2 for “Green,” and 3 for “Blue.” The important thing to remember is that these categories have a purposeful hierarchy. Here are some key terms to remember:
Why Use Ordinal Encoding?Numerous algorithms in machine learning function best with numerical data. To assist the algorithms in processing the data and producing predictions, ordinal encoding converts categories into a number format. In cases when the categories are naturally arranged like grades (A, B, C) or levels (Low, Medium, High) it is extremely helpful. Preparing the DataBefore we can perform ordinal encoding, we need to have some data to work with. Let’s consider a simple dataset:
In this dataset, ‘Grade’ is a categorical variable that we want to encode. Implementing Ordinal Encoding in SklearnNow, let’s move on to the actual implementation using Sklearn. Step 1: Install SklearnFirst, ensure you have Sklearn installed. You can install it using pip:
Step 2: Import Necessary LibrariesNext, we’ll import the required libraries.
Step 3: Create the DataFrameWe’ll create a DataFrame using the sample data.
Output: Student Grade Step 4: Initialize and Apply OrdinalEncoderWe’ll initialize the OrdinalEncoder and apply it to the ‘Grade’ column.
Output: Student Grade Grade_encoded Verifying the ResultsTo verify our results, we can check that the ‘Grade’ column has been correctly encoded. Each category ‘A’, ‘B’, and ‘C’ has been replaced with 0.0, 1.0, and 2.0, respectively. This confirms that our ordinal encoding has been applied successfully. Example 2: Ordinal Encoding with a Public DatasetStep 1: Import Necessary LibrariesWe’ll start by importing the necessary libraries.
Step 2: Load the Public DatasetWe’ll load the “Titanic” dataset directly from a URL.
Output: Original Data: The Titanic dataset contains various features about passengers, such as ‘Pclass’ (passenger class), ‘Sex’, ‘Age’, etc. Step 3: Visualize the ColumnWe’ll focus on the ‘Sex’ column and visualize its distribution.
Output: ![]() The plot shows the frequency of male and female passengers in the dataset. Step 4: Initialize and Apply OrdinalEncoderWe’ll initialize the OrdinalEncoder and apply it to the ‘Sex’ column.
Output: Encoded Data: We specify the order of the categories as ‘female’ and ‘male’. The OrdinalEncoder assigns 0 to ‘female’ and 1 to ‘male’. Step 5: Visualize the Encoded DataLet’s visualize the encoded data to see the distribution of numerical values.
Output: ![]() ConclusionOrdinal encoding is a handy way to prepare your data for machine learning tasks. The method is simple and seamless thanks to Sklearn’s OrdinalEncoder. You can now use order to your advantage in your data analysis endeavors! When the categories have a natural order, ordinal encoding is a simple yet effective method for turning categorical data into numerical representation. This procedure is significantly more accessible, when Sklearn is used. You may use ordinal encoding into your machine learning projects with ease by following the instructions provided in this article. FAQs1. What if my categories do not have a natural order?
2. Can I encode multiple columns at once?
3. How do I handle data that has missing values?
You can improve the effectiveness and precision of your models by preparing your categorical data for a variety of machine learning techniques by comprehending, and using ordinal encoding. Have fun with coding ! |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Related |
---|
![]() |
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |