VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/encoding-categorical-data-in-sklearn/

⇱ Encoding Categorical Data in Sklearn - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Encoding Categorical Data in Sklearn

Last Updated : 17 Sep, 2025

Unlike numerical data, categorical data represents discrete values or categories such as gender, country or product type. Machine learning algorithms require numerical input, making it essential to convert categorical data into a numerical format. This process is known as encoding. Categorical data is a common in many fields like marketing, finance and social sciences

In this article we will use different encoding techniques to convert categorical data.

Step 1: Loading the Dataset

Here we will load pandas and scikit learn library. After that we can load our dataset.

We can download dataset from here.

👁 Screenshot-2025-07-23-181738
Dataset

Step 2: Label Encoding

Here we will use Label encoding converts each category into a unique integer, making it suitable for ordinal data or when models need numeric input.

  • fit_transform: Learns and applies the mapping.
  • .classes_: Shows the mapping order.
👁 Screenshot-2025-07-23-181738
Label Encoding

Step 3: One-Hot Encoding

Now we will use One-Hot encoding which creates separate binary columns for each category, ideal for nominal data with no natural order.

  • fit_transform: Finds all unique categories and encodes them to binary columns.
  • df_ohe.drop(columns=categorical_cols, inplace=True): Drop original categorical columns if you proceed with encoded values only
👁 Screenshot-2025-07-23-181708
One-Hot Encoding

Step 4: Ordinal Encoding

Ordinal encoding is used for features where order matters like low < med < high. Explicitly supplies category order to ensure model sees the true underlying order.

👁 Screenshot-2025-07-23-181654
Ordinal Encoding

Step 5: Putting Data Together with ColumnTransformer

  • This approach cleanly manages both ordinal and nominal encoding and fits directly into any sklearn modeling pipeline.
  • Suitable for any supervised learning (classification/regression) with categorical inputs.

Transformed shape: (1728, 19)

Step 6: Inspection and Resulted Dataset

  • Always use the same encoder objects on train and test data to ensure consistency.
  • For categorical variable exploration and encoding in a deployed or production ML pipeline, prefer maintaining category order explicitly for any ordinal features.

Output:

👁 Screenshot-2025-07-23-181623
Output

Difference between Each Encoding Technique

Here we will see a quick difference between Label Encoding, One-Hot Encoding and Ordinal Encoding.

ParameterLabel EncodingOne-Hot EncodingOrdinal Encoding
DefinitionConverts each category into a unique integerConverts categories into binary vectorsAssigns integers to categories based on order
Output TypeInteger valuesBinary vector (0/1)Integer values respecting order
Suitable ForOrdinal dataNominal dataOrdinal data
Introduces OrdinalityYes (may be misleading for nominal data)NoYes
DimensionalityLow (single column)High (number of unique categories becomes columns)Low (single column)
WorkingEach category become unique integerEach category become new binary columnCategories become ordered integers
Use CaseAlgorithms that can handle ordinal integersAlgorithms that cannot handle categorical dataOrdered categories (e.g., rating levels)
Speed / EfficiencyFastSlower for high cardinalityFast
Comment