How to deal with string labels in multi-class classification with keras? I am newbie on machine learning and keras and now working a multi-class image classification problem using keras. The input is tagged image. After some pre-processing, the training data is represented in Python list as:
[[&quot;dog&quot;, &quot;path/to/dog/imageX.jpg&quot;],[&quot;cat&quot;, &quot;path/to/cat/imageX.jpg&quot;], 
 [&quot;bird&quot;, &quot;path/to/cat/imageX.jpg&quot;]]

the &quot;dog&quot;, &quot;cat&quot;, and &quot;bird&quot; are the class labels. I think one-hot encoding should be used for this problem but I am not very clear on how to deal it with these string labels. I've tried sklearn's LabelEncoder() in this way:
encoder = LabelEncoder()
trafomed_label = encoder.fit_transform([&quot;dog&quot;, &quot;cat&quot;, &quot;bird&quot;])
print(trafomed_label)

And the output is [2 1 0], which is different that my expectation output of somthing like [[1,0,0],[0,1,0],[0,0,1]]. It can be done with some coding, but I'd like to know if there is some &quot;standard&quot; or &quot;traditional&quot; way to deal with it?
Sklearn's LabelEncoder module finds all classes and assigns each a numeric id starting from 0. This means that whatever your class representations are in the original data set, you now have a simple consistent way to represent each. It doesn't do one-hot encoding, although as you correctly identify, it is pretty close, and you can use those ids to quickly generate one-hot-encodings in other code.

If you want one-hot encoding, you can use LabelBinarizer instead. This works very similarly:



 from sklearn.preprocessing import LabelBinarizer
 encoder = LabelBinarizer()
 transfomed_label = encoder.fit_transform(["dog", "cat", "bird"])
 print(transfomed_label)


Output:

[[0 0 1]
 [0 1 0]
 [1 0 0]]