首页 > 后端开发 > Python教程 > 使用 Matplotlib 在 Python 中可视化情感分析结果

使用 Matplotlib 在 Python 中可视化情感分析结果

Barbara Streisand
发布: 2025-01-05 12:38:41
原创
679 人浏览过

在本文中,我们将使用 Matplotlib 添加情感分析结果的图形表示。目标是可视化多个句子的情绪分数,并使用条形图使用不同的颜色区分积极和消极情绪。

先决条件

确保您安装了以下库:

pip install transformers torch matplotlib
登录后复制
登录后复制
  • Transformer:用于处理预先训练的 NLP 模型。
  • torch:用于运行模型。
  • matplotlib:用于创建情感分析结果的图形表示。

可视化 Python 代码

Visualizing Sentiment Analysis Results in Python using 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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板