Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

LabelEncoder

LabelEncoder converts target labels into integer codes from 0 to n_classes - 1 and can convert those codes back. It is for y, not input features X.

Since
0.12

How it works

LabelEncoder learns the distinct values in a target array and stores them in classes_. It maps each value to an integer from 0 through the number of classes minus 1, and inverse_transform converts those integers back to the original labels.

Syntax

class sklearn.preprocessing.LabelEncoder()

Parameters

NameTypeDescription
yarray-like of shape (n_samples,)Target values used to fit the encoder; an array-like collection with one value per sample.

Returns

fit returns the fitted LabelEncoder; transform and fit_transform return an ndarray of encoded labels; inverse_transform returns the original labels.

Key points

  • The class converts target labels into consecutive integer codes beginning at zero.
  • After fitting, classes_ records the distinct target labels known to the encoder.
  • fit_transform learns the label mapping and returns the encoded target values in one step.
  • Labels such as strings can be encoded as well as numeric values.
  • inverse_transform can recover original labels from their encoded integer representations.

Common mistakes

  • Use LabelEncoder for target values rather than feature columns; feature encoding requires a different preprocessing approach.
  • Non-numeric labels are supported only when their values can be both hashed and compared.

Example: Encode and decode target labels

from sklearn.preprocessing import LabelEncoder

encoder = LabelEncoder()
encoded = encoder.fit_transform(["paris", "tokyo", "paris"])
print(encoded.tolist())
print(encoder.classes_.tolist())
print(encoder.inverse_transform(encoded).tolist())

Fits the encoder, converts target labels to integer codes, and restores the original labels.

When to use it

Normalize classification targets

A classifier expects target labels in a compact integer representation.

Round-trip target labels

A pipeline needs to store and later restore human-readable target names.

Encode non-numeric targets

Targets are strings or other comparable, hashable values.

Interview questions

QWhat problem does LabelEncoder solve?

LabelEncoder is intended for encoding the prediction target y. It learns the unique target values and represents them with integers from 0 to n_classes - 1.

Follow-up: How would you encode categorical input features? Use a feature-oriented encoder such as OneHotEncoder or OrdinalEncoder instead of LabelEncoder.

QExplain the main LabelEncoder operations.

fit learns the labels and stores them in classes_. transform maps labels to their learned integer codes, while fit_transform performs fitting and encoding in one operation.

Follow-up: What does inverse_transform do? It maps encoded integers back to the corresponding original labels using the learned class mapping.

QWhat does classes_ represent?

The classes_ attribute contains the label for each class, and the encoded values correspond to the positions of those classes. The documentation's examples show both numeric and non-numeric labels being converted and then recovered.

Follow-up: What must hold for string labels? They must be hashable and comparable so the encoder can process them.

  • sklearn.preprocessing.OrdinalEncoder
  • sklearn.preprocessing.OneHotEncoder
  • sklearn.preprocessing.LabelBinarizer

Source: LabelEncoder. Checked 2026-10-15.