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.
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.
Related reading
- Getting wrong prediction after loading a saved model
- Ghost line in Tensorboard scalar plot
- Given a tensor flow model graph, how to find the input node and output node names
- Gomoku array-based AI-algorithm?
- Git - fatal Unable to create '/path/my_project/.git/index.lock' File exists
- Git - fatal Unable to create '/path/my_project/.git/index.lock' File exists
- Good implementations of reinforcement learning?
- Good performance with Accuracy but not with Dice loss in Image Segmentation
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.