How to "desipher" Keras predictions?

python
r-machine-learning
keras

#1

I went thorugh this great article:

And when I tried to predict and recognise one sound I’ve receive an answer like that:
0.0
0.0
0.0
0.02
0.01
0.0
0.96
0.0
0.0
0.0

Problem is I miss the lables of categories, so can’t actually say what is the prediction. So how to get correct linkage between prediction, and category labels? Thanks.


#2

Hi @StanKo,

You can use model.predict_classes() function to predict the classes.


#3

Thanks for the answer. Now I get outcome like that:
[2]
or
[5]
or
[0]

So in fact, I don’t know if outcome [0] is for siren, dog_bark or other class.

The problem is, that I dont know the order of classes. I miss that linkage between order of class and label i put in the excel file. I can make the order by myself by listening to sounds, but I would like to make it clean way for future projects. Thanks.


#4

Hi @StanKo,

You have to do the labeling part manually. You can label encode the target variable or manually give the numbers to different classes.


#5

Is there any tutorial how to do that? Thank you!


#6

Hi @StanKo,

You have to do that manually. I am not aware of any tutorial related to that but if I came across any such tutorial, I will share it with you.

You just have to label the same type of audios to a particular class. In this way, there will be different classes based on the number of different types of audios you have in training data.


#7

Yes, that’s what I exactly did (use audio from training to guess which number reffers to which class). But I wouldn’t expect that. Tensorflow (visual recognition) predictions gives me back class name automatically. So this is kind of surprise for me with keras.

Thanks a lot for your time and expertise on this!


#8

You can use inverse_transform feature of LabelEncoder

For example:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
species = [‘setosa’,‘virginica’,‘versicolor’,‘versicolor’,‘virginica’,‘setosa’]
species = le.fit_transform(species)
list(le.classes_)
list(le.inverse_transform(species))

or

list(le.inverse_transform([0,1,2]))
print(species)


#9

Perfect, this worked for me. Thanks!