Search code examples
pythonmachine-learningscikit-learnone-hot-encoding

How to reverse one-hot encoding in Python?


I am currently creating a CNN where the main task the network has is to classify input information into different classes. These classes are exact values of the predicted frequencies.

This is what I have built so far:

def evaluate_model(X_train, Y_train, X_test, Y_test,n_filters):
    verbose, epochs, batch_size = 1, 10, 3
    n_timesteps, n_features = X_train.shape[1], X_train.shape[2]
    model = Sequential()
    model.add(Conv1D(filters=n_filters, kernel_size=8, activation='relu', input_shape=(n_timesteps,n_features)))
    model.add(Conv1D(filters=n_filters, kernel_size=8, activation='relu'))
    model.add(Dropout(0.5))
    model.add(MaxPooling1D(pool_size=2))
    model.add(Flatten())
    model.add(Dense(100, activation='relu'))
    model.add(Dense(50, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())
    # fit network
    history=model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)
    # evaluate model
    _, accuracy = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1) 
    return accuracy, model

predict=model.predict(amplitude_t)
print(predict)

I am trying to predict the values of some new signals that I created which works perfectly. Although my output is a probability output and I want to convert this back into the actual frequency values. Is there a way to do this?


Solution

  • This is what you need to do:

    predicted_labels = np.argmax(predict, 0)
    

    For further clarification, refer to this answer:

    https://stackoverflow.com/a/52361283/7185112