scikit-learn
LabelEncoder
ValueError
machine learning
error handling

Getting ValueError y contains new labels when using scikit learn's LabelEncoder

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

LabelEncoder is simple when every category you will ever see is already known at fit time. The ValueError: y contains new labels appears when you call transform() on values the encoder never learned. The fix depends on what you are encoding, because LabelEncoder is appropriate for target labels in some cases and the wrong tool for feature columns in many others.

Why The Error Happens

LabelEncoder creates a fixed mapping during fit().

python
1from sklearn.preprocessing import LabelEncoder
2
3encoder = LabelEncoder()
4encoder.fit(["cat", "dog", "fox"])
5
6print(encoder.classes_)
7print(encoder.transform(["dog", "cat"]))

This works because both values were seen during fitting.

The error appears as soon as you ask it to encode an unseen label:

python
1from sklearn.preprocessing import LabelEncoder
2
3encoder = LabelEncoder()
4encoder.fit(["cat", "dog", "fox"])
5
6encoder.transform(["dog", "wolf"])

"wolf" was never in encoder.classes_, so scikit-learn raises the exception instead of guessing.

For Feature Columns, Prefer OrdinalEncoder Or OneHotEncoder

Many developers hit this error because they use LabelEncoder on input features. That is usually not the best choice. For feature columns, OrdinalEncoder or OneHotEncoder is normally safer.

Here is OrdinalEncoder with an explicit unknown-value strategy:

python
1import numpy as np
2from sklearn.preprocessing import OrdinalEncoder
3
4train = np.array([["red"], ["blue"], ["green"]])
5test = np.array([["blue"], ["yellow"]])
6
7encoder = OrdinalEncoder(
8    handle_unknown="use_encoded_value",
9    unknown_value=-1,
10)
11
12encoder.fit(train)
13print(encoder.transform(test))

That produces a numeric code for known values and -1 for unknown ones instead of throwing an exception.

If your model benefits from one-hot features, OneHotEncoder(handle_unknown="ignore") is often even better because it preserves category separation without imposing an arbitrary ordering.

For Target Labels, Unknown Classes Are A Real Problem

If the encoded values are your target y, unseen labels usually mean something deeper: the model is being asked to predict or evaluate a class it was never trained on.

In that case, the right answer is often one of these:

  • fit the encoder on the complete known class list before training
  • retrain the model when a genuinely new class appears
  • reject or quarantine rows that contain unsupported target labels

A simple pattern is to define the full allowed label set up front:

python
1from sklearn.preprocessing import LabelEncoder
2
3all_classes = ["bronze", "silver", "gold"]
4encoder = LabelEncoder()
5encoder.fit(all_classes)
6
7print(encoder.transform(["silver", "bronze"]))

That works only if the class set is genuinely known in advance.

Manual Fallback Mapping

If you truly need a fallback for unpredictable categories in a feature-like workflow, a plain dictionary can be clearer than forcing LabelEncoder to do something it was not designed for.

python
1labels = ["cat", "dog", "fox"]
2label_to_id = {label: i for i, label in enumerate(labels)}
3unknown_id = -1
4
5values = ["dog", "wolf", "cat"]
6encoded = [label_to_id.get(value, unknown_id) for value in values]
7print(encoded)

This approach is explicit and easy to reason about, especially when -1 has a defined downstream meaning.

Common Pitfalls

The biggest mistake is using LabelEncoder for feature columns when scikit-learn already provides encoders designed for that job.

Another issue is fitting on training data and then assuming future data will never contain new categories. Real production inputs often violate that assumption.

It is also easy to fit separate encoders on train and test data, which creates inconsistent mappings even when no exception is raised.

Finally, if the unseen values are target labels, do not paper over the problem with a fake code unless the model and evaluation pipeline are explicitly designed to handle it.

Summary

  • 'LabelEncoder raises this error when transform() sees a label that was absent during fit().'
  • Use OrdinalEncoder or OneHotEncoder for feature columns instead of LabelEncoder.
  • For target labels, unseen classes usually mean the model or label vocabulary needs to change.
  • Fit on the complete known class set only when that class set is truly stable.
  • If you need a fallback code, a manual mapping can be clearer than misusing LabelEncoder.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.