![]() |
VOOZH | about |
Ordinal encoding assigns a unique integer to each category in a feature, reflecting their order. For example, in a dataset containing shirt sizes (small, medium, large) it can assign 1, 2 and 3 respectively. The point is not just to encode but to preserve the inherent ranking among categories. It is used in machine learning as various algorithms work best with numerical data only.
Let's see the implementation of Ordinal Encoding using Sklearn with the help of examples,
Step 1: Import libraries
Import Pandas and Scikit learn
Step 2: Creating a Dataset
Output:
Step 3: Initialize and apply Ordinal Encoder
OrdinalEncoder and explicitly sets the order: 'A' < 'B' < 'C'.Grade_encoded.Step 1: Import the required Libraries
Step 2: Load the Titanic Dataset
Step 3: Encode the "Sex" column.
OrdinalEncoder with the order: 'female' = 0, 'male' = 1.Sex_encoded with the results.Step 4: Visualize the Encoded Feature
Plots the new encoded values (0 and 1), confirming our transformation.
By using scikit-learn's OrdinalEncoder, we can easily encode features that have a natural hierarchy, ensuring our models interpret the underlying order correctly.