在本文中,我们将使用 Matplotlib 添加情感分析结果的图形表示。目标是可视化多个句子的情绪分数,并使用条形图使用不同的颜色区分积极和消极情绪。
确保您安装了以下库:
pip install transformers torch matplotlib
这是更新后的 Python 代码,它将情感分析与数据可视化相集成。
import matplotlib.pyplot as plt from transformers import pipeline from transformers import AutoTokenizer, AutoModelForSequenceClassification # Load pre-trained model and tokenizer model_name = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # Initialize the sentiment-analysis pipeline classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) # List of 10 sentences for sentiment analysis sentences = [ "I love you! I love you! I love you!", "I feel so sad today.", "This is the best day ever!", "I can't stand the rain.", "Everything is going so well.", "I hate waiting in line.", "The weather is nice, but it's cold.", "I'm so proud of my achievements.", "I am very upset with the decision.", "I am feeling optimistic about the future." ] # Prepare data for the chart scores = [] colors = [] for sentence in sentences: result = classifier(sentence) sentiment = result[0]['label'] score = result[0]['score'] scores.append(score) # Color bars based on sentiment: Positive -> green, Negative -> red if sentiment == "POSITIVE": colors.append("green") else: colors.append("red") # Create a bar chart plt.figure(figsize=(10, 6)) bars = plt.bar(sentences, scores, color=colors) # Add labels and title with a line break plt.xlabel('Sentences') plt.ylabel('Sentiment Score') plt.title('Sentiment Analysis of 10 Sentences\n') # Added newline here plt.xticks(rotation=45, ha="right") # Adjust spacing with top margin (to add ceiling space) plt.subplots_adjust(top=0.85) # Adjust the top spacing (20px roughly equivalent to 0.1 top margin) plt.tight_layout() # Adjusts the rest of the layout # Display the sentiment score on top of the bars for bar in bars: yval = bar.get_height() plt.text(bar.get_x() + bar.get_width() / 2, yval + 0.02, f'{yval:.2f}', ha='center', va='bottom', fontsize=9) # Show the plot plt.show()
导入必要的库:
我们导入 matplotlib.pyplot 来创建绘图和转换器来执行情感分析。
import matplotlib.pyplot as plt from transformers import pipeline from transformers import AutoTokenizer, AutoModelForSequenceClassification
加载预训练模型:
我们加载针对 SST-2 数据集进行情感分析而微调的 DistilBERT 模型。我们还加载关联的标记生成器,将文本转换为模型可读的标记。
model_name = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name)
初始化情感分析管道:
分类器管道是为情感分析而设置的。该管道负责对输入文本进行标记、执行推理并返回结果。
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
情感分析句子:
我们创建一个包含 10 个句子的列表来进行分析。每句话都是一种独特的情感表达,从非常积极到消极。
sentences = [ "I love you! I love you! I love you!", "I feel so sad today.", "This is the best day ever!", "I can't stand the rain.", "Everything is going so well.", "I hate waiting in line.", "The weather is nice, but it's cold.", "I'm so proud of my achievements.", "I am very upset with the decision.", "I am feeling optimistic about the future." ]
处理情绪并准备数据:
对于每个句子,我们对其情感进行分类并提取分数。根据情绪标签(积极或消极),我们为图表中的条形指定颜色。积极的句子将是绿色的,而消极的句子将是红色的。
scores = [] colors = [] for sentence in sentences: result = classifier(sentence) sentiment = result[0]['label'] score = result[0]['score'] scores.append(score) if sentiment == "POSITIVE": colors.append("green") else: colors.append("red")
创建条形图:
我们使用 matplotlib 创建条形图。每个条形的高度代表一个句子的情感分数,颜色区分积极和消极情绪。
plt.figure(figsize=(10, 6)) bars = plt.bar(sentences, scores, color=colors)
添加标签并调整布局:
我们通过旋转 x 轴标签以提高可读性、添加标题以及调整布局以获得最佳间距来自定义绘图的外观。
plt.xlabel('Sentences') plt.ylabel('Sentiment Score') plt.title('Sentiment Analysis of 10 Sentences\n') # Added newline here plt.xticks(rotation=45, ha="right") plt.subplots_adjust(top=0.85) # Adjust the top spacing plt.tight_layout() # Adjusts the rest of the layout
在条形顶部显示情绪分数:
我们还在每个条形的顶部显示情绪得分,以使图表提供更多信息。
pip install transformers torch matplotlib
显示图:
最后,使用 plt.show() 显示图表,它渲染绘图。
import matplotlib.pyplot as plt from transformers import pipeline from transformers import AutoTokenizer, AutoModelForSequenceClassification # Load pre-trained model and tokenizer model_name = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # Initialize the sentiment-analysis pipeline classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) # List of 10 sentences for sentiment analysis sentences = [ "I love you! I love you! I love you!", "I feel so sad today.", "This is the best day ever!", "I can't stand the rain.", "Everything is going so well.", "I hate waiting in line.", "The weather is nice, but it's cold.", "I'm so proud of my achievements.", "I am very upset with the decision.", "I am feeling optimistic about the future." ] # Prepare data for the chart scores = [] colors = [] for sentence in sentences: result = classifier(sentence) sentiment = result[0]['label'] score = result[0]['score'] scores.append(score) # Color bars based on sentiment: Positive -> green, Negative -> red if sentiment == "POSITIVE": colors.append("green") else: colors.append("red") # Create a bar chart plt.figure(figsize=(10, 6)) bars = plt.bar(sentences, scores, color=colors) # Add labels and title with a line break plt.xlabel('Sentences') plt.ylabel('Sentiment Score') plt.title('Sentiment Analysis of 10 Sentences\n') # Added newline here plt.xticks(rotation=45, ha="right") # Adjust spacing with top margin (to add ceiling space) plt.subplots_adjust(top=0.85) # Adjust the top spacing (20px roughly equivalent to 0.1 top margin) plt.tight_layout() # Adjusts the rest of the layout # Display the sentiment score on top of the bars for bar in bars: yval = bar.get_height() plt.text(bar.get_x() + bar.get_width() / 2, yval + 0.02, f'{yval:.2f}', ha='center', va='bottom', fontsize=9) # Show the plot plt.show()
此代码的输出将是一个显示 10 个句子的情绪分数的条形图。积极的句子将用绿色条表示,而消极的句子将显示为红色条。情绪得分将显示在每个条形上方,显示模型的置信水平。
通过将情感分析与数据可视化相结合,我们可以更好地解读文本数据背后的情感基调。本文中的图形表示可以让您更清楚地了解情绪分布,使您可以轻松发现文本中的趋势。您可以将此技术应用于各种用例,例如分析产品评论、社交媒体帖子或客户反馈。
通过 Hugging Face 的转换器和 matplotlib 的强大组合,可以扩展和定制此工作流程以适应各种 NLP 任务。
以上是使用 Matplotlib 在 Python 中可视化情感分析结果的详细内容。更多信息请关注PHP中文网其他相关文章!