If a sklearn.LabelEncoder has been fitted on a training set, it might break if it encounters new values when used on a test set.
sklearn.LabelEncoder
The only solution I c
If it is just about training and testing a model, why not just labelencode on entire dataset. And then use the generated classes from the encoder object.
encoder = LabelEncoder() encoder.fit_transform(df["label"]) train_y = encoder.transform(train_y) test_y = encoder.transform(test_y)