Home > Enterprise >  return tensorflow predict by string not an array
return tensorflow predict by string not an array

Time:11-08

So i build a tensorflow for sentimen analysis with 3 multiple class with y_label = {0:"negative",1:"neutral,2:"positive") . When I try to predict a sentences it returns an array,how to change it so it will return string based on predicted result?.In my understanding does the predicted value is the max of the array?

sentence = ["kurir keren"]
sequences = tokenizer.texts_to_sequences(sentence)
padded = pad_sequences(sequences,maxlen=max_length,padding=padding_type,truncating=trunc_type)
predic = np.rint(model.predict(padded))
predic2 = model.predict(padded)
print(predic)
print(predic2)

Output

[[0. 0. 1.]]
[[0.3224687  0.45956263 0.5116373 ]]

CodePudding user response:

It seems like you are using one-hot encoded labels where [1. 0. 0.] represents negative , [0. 1. 0.] represents neutral, and [0. 0. 1.] represents positive, so maybe just use np.argmax to get the index of the highest prediction and use that integer to get the corresponding string:

import numpy as np

y_label = {0:"negative",1:"neutral",2:"positive"}

prediction = np.array([[0.3224687,  0.45956263, 0.5116373 ]])
print(y_label[np.argmax(prediction)])
positive
  • Related