Getting ValueError y contains new labels when using scikit learn's LabelEncoder
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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().
This works because both values were seen during fitting.
The error appears as soon as you ask it to encode an unseen label:
"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:
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:
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.
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
- '
LabelEncoderraises this error whentransform()sees a label that was absent duringfit().' - Use
OrdinalEncoderorOneHotEncoderfor feature columns instead ofLabelEncoder. - 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.

