Search code examples
machine-learningone-hot-encodingkaggle

One hot encoded output to categorical value from a ML model


In my one .py file I created a model and saved the .pkl file of it to use afterward for analysis. The model is formed using the code from this kaggle emotional data set https://www.kaggle.com/shivamburnwal/speech-emotion-recognition The issue is that when I am using this code's model to detect the emotion of new audio then the output is in one hot encoded format. Is there any way using which I can get the actual emotion('happy','fear' etc.) instead of 1's and 0's.


Solution

  • prediction output is a list like this:

    prediction = [0,0,0,0,1,0,0] , [0,1,0,0,0,0,0], [0,0,0,1,0,0,0]
    

    Then you can change them by using this code:

    pred = []
    
    for x in prediction:
        if x == [0,0,0,0,1,0,0]:
            pred.append('Sad')
        
        elif x == [0,1,0,0,0,0,0]:
            pred.append('Happy')
            
        elif x == [0,0,0,1,0,0,0]:
            pred.append('disgust')
    
    print(pred)
    

    output =

    ['Sad', 'Happy', 'disgust']
    

    Please add more elif statement according to your need.