首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示文本分类中的其他情感分数?

如何显示文本分类中的其他情感分数?
EN

Stack Overflow用户
提问于 2021-06-15 13:59:16
回答 1查看 421关注 0票数 0

我正在做情绪分析,我想知道如何在上显示我的句子中的其他情绪分数:“特斯拉的股票增长了20%。”

我有三种情绪:阳性阴性中性

这是我的代码,其中包含了我想分类的句子:

代码语言:javascript
复制
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%")

这是结果代码和输出:

代码语言:javascript
复制
print(result)

TextClassificationResult(label='positive', score=0.929110586643219)

这是情绪评分,它只显示正面的分数:

代码语言:javascript
复制
print(result.label)
print(result.score)

positive
0.92

现在,我如何使它显示的情绪得分的消极和中立,以及积极?

像这样的东西:

代码语言:javascript
复制
positive
0.92

negative
0.05

neutral
0.03

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-15 14:44:34

因为HappyTransformer不支持多类概率,所以我建议使用另一个库。库flair提供了更多的功能,可以提供所需的多类概率,如下所示:

代码语言:javascript
复制
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)

仅供使用的pip install flair

注意,我们使用的模型与BERT不同,并且只返回两个标签,而不是三个。

另一种选择是使用HuggingFace库。它允许使用自定义标签。

代码语言:javascript
复制
from transformers import pipeline

classifier = pipeline("zero-shot-classification")
classifier(
    "This is a course about the Transformers library",
    candidate_labels=["education", "politics", "business"],
)
代码语言:javascript
复制
{'sequence': 'This is a course about the Transformers library',
 'labels': ['education', 'business', 'politics'],
 'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}

在您的例子中,您将标签切换到["positive", "negative", "neutral"]

示例摘自:https://huggingface.co/course/chapter1/3?fw=pt

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67987738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档