Your original approach, without one-hot encoding, was doing what you wanted.

One-hot encoding is meant for inputs to many models, but outputs for only a few (e.g. training a neural network with cross-entropy loss). So these are only needed for some algorithm implementations, while others can do fine without it.

For output labels, a classifier like RandomForest is just fine with strings and multiple classes.

Answer from mcskinner on Stack Overflow
🌐
PyTorch Forums
discuss.pytorch.org › t › one-hot-encoding-for-multi-label-classification-using-bcewithlogitsloss-loss › 79262
One hot encoding for multi label classification using BCEWithLogitsLoss() loss - PyTorch Forums
May 1, 2020 - I am using resnet18 with BCEWithLogitsLoss() and i am encoding my labels using y_onehot = nn.functional.one_hot(labels, num_classes=3) y_onehot = y_onehot.float() Which is I think not true for multi label da…
Discussions

python - Multi-label one-hot encoding - Data Science Stack Exchange
So im having this paticular problem triying to do one hot encoding on multilabel data, the encoder is reading more classes than it should, and i dont know why. let me show you: Here's my data (17 c... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
machine learning - One hot encoding for multiple label(trainy) in .fit() method? - Data Science Stack Exchange
I have a mobile price classification dataset in which I have 20 features and one target variable called price_range. I need to classify mobile prices as low, medium, high, very high. I have applie... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
python - One-Hot Encoding of label not needed? - Stack Overflow
No @ArvindRaghavan , for multiclass classification, with other models you'll generally have to use a LabelEncoder, which encodes the classes into integers not a OneHotEncoder 2020-07-16T07:02:35.65Z+00:00 ... As far as I know, one hot encoding is never done on the output. More on stackoverflow.com
🌐 stackoverflow.com
What exactly is multi-hot encoding and how is it different from one-hot? - Cross Validated
In one-hot encoding there is one bit reserved for each word we desire to encode. How is multi-hot encoding different from one-hot? In what scenarios would it make sense to use it over one-hot? More on stats.stackexchange.com
🌐 stats.stackexchange.com
May 21, 2020
🌐
ProjectPro
projectpro.io › recipes › one-hot-encoding-with-multiple-labels-in-python
One hot encoding for multi label classification - Projectpro
December 20, 2022 - We have created a arrays of differnt labels with few of the labels in common. y = [('Raj', 'Penny'), ('Amy', 'Raj'), ('Sheldon', 'Penny'), ('Leonard', 'Amy'), ('Amy', 'Leonard')] Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro · We have created an object for MultiLabelBinarizer and using fit_transform we have fitted and transformed our data. Finally we have printed the classes that has been make by the function. one_hot = MultiLabelBinarizer() print(one_hot.fit_transform(y)) print(one_hot.classes_) So the output comes as:
🌐
Reddit
reddit.com › r/neuralnetworks › does multilabel classification require one-hot encoding?
r/neuralnetworks on Reddit: Does multilabel classification require one-hot encoding?
February 28, 2025 - Also I'd prefer the "simplicity" of only having to care fore one neural network as opposed to 8 classifiers. As a result, I have built a neural network with a multi-label output layer that produces a one-hot encoded output. The problem I'm now identifying is that this neural net does not seem to take stock that labels are mutually exclusive within classes (e.g.
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to perform one-hot encoding for multi categorical variables
How to Perform One-Hot Encoding For Multi Categorical Variables
February 3, 2025 - Now, we will go for our technique to apply one-hot encoding on multi categorical variables. The technique is that we will limit one-hot encoding to the 10 most frequent labels of the variable.
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.preprocessing.MultiLabelBinarizer.html
MultiLabelBinarizer — scikit-learn 1.9.1 documentation
Otherwise it corresponds to the sorted set of classes found when fitting. ... Encode categorical features using a one-hot aka one-of-K scheme. ... >>> from sklearn.preprocessing import MultiLabelBinarizer ...
Find elsewhere
Top answer
1 of 2
5

I think you might be confusing a multiclass (your case) with a multioutput classification.

In multiclass classification problems, your output should only be a single target column, and you'll be training the model to classify among the classes in that column. You'd have to split into separate target columns, in the case you had to predict n different classes per sample, which is not the case, you only want one of the targets per sample.

So for multiclass classification, there's no need to OneHotEncode the target, since you only want a single target column (which can also be categorical in SVC). What you do have to encode, either using OneHotEncoder or with some other encoders, is the categorical input features, which have to be numeric.

Also, SVC can deal with categorical targets, since it LabelEncode's them internally:

from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
y_train_categorical = load_iris()['target_names'][y_train]
# array(['setosa', 'setosa', 'versicolor',...

sv = SVC()
sv.fit(X_train, y_train_categorical)
sv.classes_
# array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
2 of 2
-1

As far as I know, one hot encoding is never done on the output. You need to do one hot encoding on a feature so that the model never confuses that some color is greater than other colors. When you are computing the output the models use probability distributions based on classes. So there won't be any problem here.

In a nutshell, you should do one hot encoding only on the input features and not on the output classes.

Top answer
1 of 3
40

Imagine your have five different classes e.g. ['cat', 'dog', 'fish', 'bird', 'ant']. If you would use one-hot-encoding you would represent the presence of 'dog' in a five-dimensional binary vector like [0,1,0,0,0]. If you would use multi-hot-encoding you would first label-encode your classes, thus having only a single number which represents the presence of a class (e.g. 1 for 'dog') and then convert the numerical labels to binary vectors of size .

Examples:

'cat'  = [0,0,0]  
'dog'  = [0,0,1]  
'fish' = [0,1,0]  
'bird' = [0,1,1]  
'ant'  = [1,0,0]   

This representation is basically the middle way between label-encoding, where you introduce false class relationships (0 < 1 < 2 < ... < 4, thus 'cat' < 'dog' < ... < 'ant') but only need a single value to represent class presence and one-hot-encoding, where you need a vector of size (which can be huge!) to represent all classes but have no false relationships.

Note: multi-hot-encoding introduces false additive relationships, e.g. [0,0,1] + [0,1,0] = [0,1,1] that is 'dog' + 'fish' = 'bird'. That is the price you pay for the reduced representation.

2 of 3
30

The accepted answer seems rather eccentric to me. I think that is rarely done, if ever, and will usually yield bad results.

There's a much more common, sensible use case for this. "Multi-hot encoding" doesn't seem to be a standard term, but I'm not sure there's any standard term. scikit-learn refers to a multi label binarizer.

This is simply used for multi label problems. That is, problems where more than one label can be associated with each example.

For example, say you are trying to detect whether certain types of animal are in a photo. Note that multiple types of animal can be in a single photo. Say the possible types of animal are ['cat', 'dog', 'fish', 'bird', 'ant']. A photo containing cats and dogs would be represented as [1, 1, 0, 0, 0].

🌐
KDnuggets
kdnuggets.com › 2023 › 01 › encoding-categorical-features-multilabelbinarizer.html
Encoding Categorical Features with MultiLabelBinarizer - KDnuggets
January 20, 2023 - Transform multi-label format into a binary matrix for multi-label classification. By Abid Ali Awan, KDnuggets Assistant Editor on January 20, 2023 in Natural Language Processing ... In the past, you might have converted categorical features into numerical ones using One Hot, Label, and Ordinal encoder...
🌐
Medium
medium.com › @irvan.rahadhian › one-hot-encoding-what-and-why-f22d11a7602a
One hot encoding ? What and why ? | by irvan rahadhian | Medium
September 15, 2019 - So you have to convert categorical data to form data or in other words is categorical data must be converted to numbers that could be provided to ML algorithms to do a better job in classifying data. And that’s what one hot encoding do. Oke for example, let says we have a simple sequence of labels “animals” with the values “otter”, “owl” and “cat”. We can use integer encoding for these data but it is not enough, because it has no ordinal relationship just like “first”, “second”,”third”, etc.
🌐
PyTorch Forums
discuss.pytorch.org › vision
One hot encode label for multi-label classification - vision - PyTorch Forums
May 18, 2021 - Dear all, im try to prepare dataset for multi-label classification with pytorch, there is an example with pytorch (dataloader) for multi-label classification? thanks :slight_smile:
🌐
Medium
medium.com › analytics-vidhya › multi-label-classification-a9643d221954
Multi-Label Classification. Multi-label classification sounds… | by Gautam Sharma | Analytics Vidhya | Medium
February 9, 2021 - The answer is that you need to convert these labels to something that the machine understands. One hot encoding is the process with which you will convert these labels to something that the machine understands i.e.
🌐
Fast.ai
forums.fast.ai › fastai
Multi-class image classification with one-hot-encoding - fastai - fast.ai Course Forums
August 4, 2022 - I was trying to use a Resnet to solve a multi-class task with a one-hot encoded dataset, and chose the nn.CrossEntropyLoss() function instead of the nn.BCEWithLogitLoss to avoid the sigmoid layer. However, when I try tu use fastai’s metrics, I cannot choose the single-label metrics as they expect same-sized predictions and targets, and according to the docs I should use the multi-label metrics: “Warning: All functions defined in this section are intended for single-label classification and ta...
🌐
Aionlinecourse
aionlinecourse.com › blog › one-hot-encoding-with-multiple-labels-in-python
One-Hot Encoding with Multiple Labels in Python | Aionlinecourse
Drawbacks: This may cause sparsity and high storage constraints in instances of a large number of label set. The one-hot encoding technique is used to transform categorical variables into a binary (0-1) format.
🌐
Medium
ujangriswanto08.medium.com › how-to-implement-multi-label-classification-in-python-or-r-994cb9bf8e1b
How to Implement Multi-Label Classification in Python (or R) | by Ujang Riswanto | Medium
May 11, 2025 - ✔ Multi-label classification ≠ multi-class classification! It’s all about assigning multiple labels to each sample instead of just one. ✔ Data preprocessing is crucial — one-hot encoding, text vectorization, and handling missing data can make a big difference in model performance.
🌐
James D. McCaffrey
jamesmccaffreyblog.com › home › pytorch multi-class classification with one-hot label encoding and softmax output activation
PyTorch Multi-Class Classification With One-Hot Label Encoding and Softmax Output Activation - James D. McCaffreyJames D. McCaffrey
November 4, 2020 - All you need to know is, “use ordinal encoding on the class labels, don’t apply any activation on the output nodes in the forward() method, and use CrossEntropyLoss() as the loss function.” · Anyway, one rainy Sunday afternoon in the Pacific Northwest, I sat down and decided to code up a demo of multi-class classification using Python with the old approach: one-hot encoding of the class labels, softmax activation on the output nodes, and mean squared error for training.