I am doing sentiment analysis, and I was wondering how to show the other sentiment scores from classifying my sentence: "Tesla's stock just increased by 20%."
I have three sentiments: positive, negative and neutral.
This is my code, which contains the sentence I want to classify:
pip install happytransformer
from happytransformer import HappyTextClassification
happy_tc = HappyTextClassification("BERT", "ProsusAI/finbert", num_labels=3)
result = happy_tc.classify_text("Tesla's stock just increased by 20%")
This is the result code and output:
print(result)
TextClassificationResult(label='positive', score=0.929110586643219)
This is the sentiment score, which only shows the score for positive:
print(result.label)
print(result.score)
positive
0.92
Now, how do I make it so that it shows the sentiment scores for negative and neutral as well as the positive?
Something that looks like this:
positive
0.92
negative
0.05
neutral
0.03
Thanks.
Because HappyTransformer does not support multi class probabilities I suggest to use another library. The library flair
provides even more functionality and can give you your desired multi class probabilities, with something like this:
from flair.models import TextClassifier
from flair.data import Sentence
tc = TextClassifier.load('en-sentiment')
sentence = Sentence('Flair is pretty neat!')
tc.predict(sentence, multi_class_prob=True)
print('Sentence above is: ', sentence.labels)
Just pip install flair
for usage.
Note that we use a different model than BERT and only returns two labels not three.
Another option is to use the HuggingFace
library. It allows for using self defined labels.
from transformers import pipeline
classifier = pipeline("zero-shot-classification")
classifier(
"This is a course about the Transformers library",
candidate_labels=["education", "politics", "business"],
)
{'sequence': 'This is a course about the Transformers library',
'labels': ['education', 'business', 'politics'],
'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}
In your case, you switch out the labels to ["positive", "negative", "neutral"]
.
The examples are taken from: https://huggingface.co/course/chapter1/3?fw=pt