Responder a: Percorrer uma lista com o get_prediction

#34229
Profile photo ofdennyDenny Ceccon
Moderador

Olá Micael,

Você pode primeiro fazer uma pequena modificação na função get_predictions, para retornar o valor ao invés de exibi-lo na tela:

def get_prediction(sentence):
  tokens = encode_sentence(sentence)
  inputs = tf.expand_dims(tokens, 0) # (batch_size) (1,...)
  output = Dcnn(inputs, training=False)
  sentiment = math.floor(output*2)
  if sentiment == 0:
    return 'negative'
  elif sentiment == 1:
    return 'positive'

Agora, pode organizar suas frases em um dataframe e adicionar o resultado desta forma:

texts = ["This movie was pretty interesting", "I'd rather not do that again", "What a great time to be alive", "I wish this never happened"]
df = pd.DataFrame({'text': texts})
df['emotion'] = df['text'].map(get_prediction)
print(df)

<out> 
  text                              emotion
--------------------------------------------
0 This movie was pretty interesting positive
1 I'd rather not do that again      negative
2 What a great time to be alive     positive
3 I wish this never happened        negative

  • Esta resposta foi modificada 11 meses, 2 semanas atrás por Profile photo ofdennyDenny Ceccon.