我试图了解如何根据获得的分数构建甜甜圈图或饼图。以下是我的代码
from nltk.sentiment.vader import SentimentIntensityAnalyzer
paragraph = "I loved the movie"
sid = SentimentIntensityAnalyzer()
ss = sid.polarity_scores(paragraph)
print(ss)
if ss["compound"] >= 0.5:
print("positive")
elif ss["compound"] <= -0.5:
print("negative")
else:
print("neutral")
# myresults
{'neg': 0.033, 'neu': 0.834, 'pos': 0.132, 'compound': 0.9936}
positive
如何使用复合分数将所有这些值计算为百分比?现在,我只能给它一个正面,中性或负面的标签,但我希望根据复合得分来分解所有值。在此示例中,积极得分应为99%,而不是61.2%中立,0%消极和38.8%积极
答案 0 :(得分:0)
您有ss = {'neg': 0.033, 'neu': 0.834, 'pos': 0.132, 'compound': 0.9936}
,并且
您想使用neg
,neu
和pos
的值生成饼图。如果我错了请纠正我。
尝试一下
labels = ['negative', 'neutral', 'positive']
sizes = [ss['neg'], ss['neu'], ss['pos']]
plt.pie(sizes, labels=labels, autopct='%1.1f%%') # autopct='%1.1f%%' gives you percentages printed in every slice.
plt.axis('equal') # Ensures that pie is drawn as a circle.
plt.show()