Search code examples
pythonvectorkerasdeep-learningcategorical-data

converting numerical output labels to categorical vectors in keras


I'm creating a Deep learning multi-classification model in Keras and I have converted my outputlabel training set y_train from numerical values ranging from 1 to 14 to output vectors looking like this [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0] => representing the number 2 . This is the code I used in python (keras):

from keras.utils import to_categorical
y_train = to_categorical(y_train)

However, because it converts these outputlabels into a vector of length 15 instead of 14 because it adds zero as a potential output as well. My original numpy array y_train looked like this: [1,8,9,7,2,2,8...] and it should be converted to a vector of length 14 instead of 15 to avoid extra loss when training the model. Is there a simpel way to avoid using zero as a potential output class?

(num_classes = 14 as a parameter of to_categorical gives an error message)


Solution

  • if your labels are from 1 to 14, try this simple trick:

    y_train = to_categorical(np.asarray(y_train)-1)