Home › Fóruns › Fórum Processamento de Linguagem Natural com BERT e Python › Percorrer uma lista com o get_prediction › Responder a: Percorrer uma lista com o get_prediction
13 de abril de 2022 às 18:12 #34229
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 3 anos atrás por
Denny Ceccon.