2017-01-05 3 views
2

Ich versuche, ein Array von One-Hot-Vektor von ganzen Zahlen in ein Array von einem heißen Vektor, Keras wird in der Lage sein zu verwenden, um mein Modell passen. Hier ist der relevante Teil des Codes:Probleme mit Keras np_utils.to_categorical

Y_train = np.hstack(np.asarray(dataframe.output_vector)).reshape(len(dataframe),len(output_cols)) 
dummy_y = np_utils.to_categorical(Y_train) 

Unten ist ein Bild zeigt, was Y_train und dummy_y tatsächlich sind.

Ich konnte keine Dokumentation für to_categorical finden, die mir helfen könnten.

Vielen Dank im Voraus.

+1

Y_train ist bereits ein One-Hot-Vektor, können Sie das direkt verwenden und es gibt keine Notwendigkeit, to_categorical zu verwenden, was ist das eigentliche Problem? –

Antwort

10

np.utils.to_categorical wird verwendet, um ein Array markierter Daten (von 0 bis nb_classes-1) in einen Hot-Vektor zu konvertieren.

Das offizielle Dokument mit einem Beispiel.

In [1]: from keras.utils import np_utils 
Using Theano backend. 

In [2]: np_utils.to_categorical? 
Signature: np_utils.to_categorical(y, nb_classes=None) 
Docstring: 
Convert class vector (integers from 0 to nb_classes) to binary class matrix, for use with categorical_crossentropy. 

# Arguments 
    y: class vector to be converted into a matrix 
    nb_classes: total number of classes 

# Returns 
    A binary matrix representation of the input. 
File:  /usr/local/lib/python3.5/dist-packages/keras/utils/np_utils.py 
Type:  function 

In [3]: y_train = [1, 0, 3, 4, 5, 0, 2, 1] 

In [4]: """ Assuming the labeled dataset has total six classes (0 to 5), y_train is the true label array """ 

In [5]: np_utils.to_categorical(y_train, nb_classes=6) 
Out[5]: 
array([[ 0., 1., 0., 0., 0., 0.], 
     [ 1., 0., 0., 0., 0., 0.], 
     [ 0., 0., 0., 1., 0., 0.], 
     [ 0., 0., 0., 0., 1., 0.], 
     [ 0., 0., 0., 0., 0., 1.], 
     [ 1., 0., 0., 0., 0., 0.], 
     [ 0., 0., 1., 0., 0., 0.], 
     [ 0., 1., 0., 0., 0., 0.]])